Periodic Execution of Apple Shortcuts with launchd

reflect a 1990s GeoCities style, featuring vibrant, nostalgic graphics and pixelated details that capture the essence of early internet design. You can view the updated avatar above. Created by DALL-E

To test the new Chat­G­PT 4.0, I want­ed to cre­ate a short­cut that would save my Obsid­i­an Vault to my Syn­ol­o­gy NAS at the push of a but­ton. Once we had a solu­tion, I won­dered why this back­up process should­n’t be auto­mat­ed and peri­od­ic.

Chat­G­PT 4.0 sug­gest­ed using a shell script called via the launchd sys­tem ser­vice to run the short­cut. launchd auto­mat­i­cal­ly starts pro­grams or scripts on a sched­ule. While it might seem over-engi­neered to wrap a com­mand-line com­mand in a short­cut and then call it via a shell script, I found the process inter­est­ing. Since there’s not much infor­ma­tion avail­able online, I cre­at­ed this gen­er­al guide.

Step 1: Creating a shortcut

For this arti­cle, I have cre­at­ed a very sim­ple short­cut that dis­plays the cur­rent time in the noti­fi­ca­tion cen­ter:

A screenshot of the Apple Shortcuts app showing a shortcut named "notify." The shortcut has two actions: "Current Date" and "Show notification Date." The details of the "Date" action are displayed, with settings for the variable name, type (Date), and custom format (HH:mm:ss).

I have slight­ly adapt­ed the out­put for­mat for the date, as can be seen on the screen­shot.

You can now test in the ter­mi­nal whether the short­cut works as desired:

shortcuts run notify

You should see the cur­rent time as noti­fi­ca­tion.

Once this has worked, the sec­ond step can be tack­led.

Step 2: Create a Shell Script

To exe­cute the short com­mand with launchd, it is eas­i­est to wrap the call in a shell script that looks like this:

#!/bin/bash 
/usr/bin/shortcuts run notify

I saved this shell script as notify.sh in the fold­er /User/leif/Applications and make it exe­cutable:

chmod +x /User/leif/Applications/notify.sh

Step 3: Create a launchd plist File

A plist file is required to start the script with launchd. This file must be cre­at­ed in the direc­to­ry ~/Library/LaunchAgents. For my exam­ple it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.leif.notify.plist</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/leif/Applications/notify.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>15</integer>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/tmp/notify.log</string>
    <key>StandardErrorPath</key>
    <string>/tmp/notify.err</string>
</dict>
</plist>

Expla­na­tion of the entries:

  • Label: The name of the job.
  • Pro­gra­mAr­gu­ments: The path to the shell script that runs the short­cut.
  • RunAt­Load: Ensures the job is loaded into the job list at login.
  • Start­In­ter­val: The inter­val in sec­onds at which the job runs (here: 15 sec­onds).
  • StandardOutPath/StandardErrorPath: Paths to the files for log­ging out­put infor­ma­tion.

Step 4: Start the Job

With the fol­low­ing com­mand, the plist file is reg­is­tered with launchd, so the job can be start­ed:

launchctl load ~/Library/LaunchAgents/com.leif.notify.plist

After that, the job can be start­ed, which usu­al­ly hap­pens at sys­tem start­up due to the RunAtLoad para­me­ter:

launchctl start com.leif.notify.plist

To check if the job is run­ning, type this:

launchctl list | grep com.leif.notify.plist

If some­thing does­n’t work, you can find rel­e­vant infor­ma­tion in the log files defined above.

Some additional notes:

To stop a job, it can be “unloaded” with the fol­low­ing com­mand:

launchctl unload ~/Library/LaunchAgents/com.leif.notify.plist

This com­mand stops the job and removes it from the list of active launchd jobs.

After­ward, the job can be com­plete­ly delet­ed with the com­mand:

rm ~/Library/LaunchAgents/com.leif.notify.plist

It is rec­om­mend­ed to make a back­up of the plist file before­hand.

For more con­fig­u­ra­tion options, such as run­ning a job at a spe­cif­ic time, refer to resources like A launchd Tuto­r­i­al or the detailed About Dae­mons and Ser­vices.

Conclusion

I hope this guide has been help­ful and inspir­ing for fur­ther ideas. I will present my solu­tion for back­ing up my vault, which will run with­out using Short­cuts, in anoth­er post. If you have any errors to report, addi­tions, or oth­er remarks, feel free to leave a com­ment.

2 responses to “Periodic Execution of Apple Shortcuts with launchd”

  1. @leif wait this sounds inter­est­ing but there’s no URL

    1. You are right, I still strug­gle with the ActivePub Plu­g­in. Cur­rent­ly I see no option to cor­rect that post. Hope you find it via the URL of the image. Sor­ry, Leif

Leave a Reply

Your email address will not be published. Required fields are marked *