Automate macOS 12 with the Shortcuts app
Little Helpers
Sending Email with Apple Mail
For the next example, I'll duplicate the first short command again and modify it so that it doesn't write a ZIP archive to the filesystem, but emails a ZIP instead. To do this, delete the last action and drag the Send Email action from the Sharing category to the desktop instead. The action automatically accepts the archive from the previous action as its input, but it has two additional parameters.
One parameter defines the subject line for the new email and the other the recipient. However, the shortcuts being so closely linked to Apple's ecosystem turns out to be a disadvantage. You can only specify a receiver that you previously added as a contact in the macOS Contacts app. If you want to use an email address that does not exist in your contacts as free text instead, you can do so with a simple trick: Drag an action of type Text to the second-to-last position, in which you enter the target address. In the last action, you can then turn the recipient field into a magic variable and parse the text from the previous action.
If you now apply the action to files or folders in the Finder, an email with the ZIP archive as an attachment should automatically open. With the help of the text and magic variables, you have freed yourself from the dependency on Apple's Contacts app; however, Shortcuts inherently only looks to use Apple's native mail app for sending.
Scripts
If you prefer to use Microsoft Outlook, you can overcome the barriers, too, by just duplicating the original shortcut again and deleting the last action. In its place, drag in the Run AppleScript action from the Scripting category (Figure 3). If you now apply this command to files or folders in the Finder, a suitably parameterized email with a ZIP attachment is automatically opened in Outlook.
Shortcuts can also integrate shell scripts in a similar way. For example, create another shortcut and pin it to the menubar. You want this command to turn off the operating system's power-saving mode if it is on, and vice versa. To do this, drag the Run Shell Script action into the workspace. Configure Bash as the shell and enable the Run As Administrator action. In the text box, enter the code from Listing 1.
Listing 1
toggle_powermode.sh
POWERMODE=$(pmset -g | grep lowpowermode | tr -dc ,0-9') if [ $POWERMODE == 0 ] then sudo pmset -a lowpowermode 1 else sudo pmset -a lowpowermode 0 fi
You can then access your new command from the shortcut icon in the operating system's menubar and toggle your device's power-saving mode with just two clicks.
Executing Loops
The next two shortcuts receive images, scale them, and then write the modified images to a target folder. To do this, again create a new command to use as a quick action and restrict the input to images. Drag the Get Images from Input action to the workspace (Figure 4).
For the new construct, you need a loop that processes each image that is passed in. From the Scripting section, select Repeat with Each . A loop block appears that automatically accepts the retrieved images. Drag the Resize Image action into the loop block. You can choose between a fixed value for the width, height, or longest edge; alternatively, you could use a percentage. As a further building block you can add the Move File action to the loop to write the respective image to the desired target folder. Next, pass in some images to the command in the Finder to see whether it works.
Shortcuts also supports a simple Repeat loop construct with a selectable number of runs, If blocks, and a simple If – Otherwise query that checks for a configurable condition and executes one action or another, depending on the condition.
Buy this article as PDF
(incl. VAT)