Running a Shell Script in a Shortcut

I wanted to explore a bit further how a shell script might be run in a shortcut, and the shortcut included below was my most recent test. It gets information about the current app (which cannot be the Shortcuts app) and creates a list for further use in the shortcut. The Run Shell Script action will only output text, so I created a newline-separated string, which I then made into a list with the Split Text action. In the following screenshot, Safari was the active app.

Current App Info.shortcut (22.4 KB)

This looks very interesting. So, in my possible case, I would like to run an rsync shell script that does a backup. Would my active application then need to be Terminal?

Why do you need Terminal at all?

Terminal is just an application used to invoke shell commands.
Just like Shortcuts’ Run Shell Script
Just like AppleScript’s do shell script

What is it about your rsync command that makes you think you need Terminal?

Camelot, I have a number (seven all total, just different source and destination folders) of backups that use the below rsync routine, I now have them running via an Automator do shell script, but would love to transition them to a “do shell script” AppleScript. Just haven’t found a way to do that, yet. The rsync does the backup and then moves any changed/deleted files into an Archive folder. I then have an AppleScript that goes through the Archive folders and deletes any files over 90 days old.

rsync -a -b --backup-dir=/Volumes/MacBook\ Pro\ Local\ Backups/Sync\ Folders\ Local\ Backups/Local\ Documents\ Archive/Documents_$(date +\%Y-\%m-\%d) --delete /Users/homer/Documents/ /Volumes/MacBook\ Pro\ Local\ Backups/Sync\ Folders\ Local\ Backups/Local\ Documents

Homer712. You can run the rsync command in a Run Shell Script action in a shortcut. You would probably also want to add a notification, and I’ve included that in the shortcut. I simplified your rsnc command for testing on my computer, and it worked as expected when run from within the Shortcuts app, which has Full Disk Access and Accessibility permissions and has permission to access the shell. However, when run from the Shortcuts app’s menu-bar menu, the shortcut fails with the message shown below. I suspect that whatever app runs shortcuts by way of the Shortcuts app’s menu-bar menu needs to be given the necessary permissions. I’ll see if I can find that app.

rsync (2227): warning: /Users/robert/Desktop/Test Folder: unreadable directory: Operation not permitted rsync(2228): error: /Volumes/Backup 1/Home/Test/Documents: open: Operation not permitted
rsync (2227): error: unexpected end of file

Rsync Shortcut.shortcut (21.9 KB)

I downloaded the shortcut and only copied the very first of my seven rsync backup routines. And, it ran perfectly. I’ll be interested in what you find out on the menu-bar error.

1 Like

The following are now present in my Accessibility and Full Disk Access permissions, but the rsync shortcut will not run from the menu bar. It does run fine from within the Shortcuts app and by way of an AppleScript or shell script. The rsync shortcut also works if the destination is on my boot drive (other than a protected folder like Desktop).

  • bash
  • Finder
  • rsync
  • Shortcuts
  • ShortcutDroplet
  • Shortcuts Events
  • ShortcutsActions
  • Siri
  • zsh

I’m going to set this one aside for another day. :frowning:

I’ve given up on this for now. A workaround is to run the shortcut by way of an AppleScript in the AppleScript Menu:

tell application "Shortcuts Events" to run shortcut named "my shortcut"

For FastScripts users, another workaround is to run the shortcut by way of a Bash script. This can be run from the FastScripts Menu or by way of a keyboard shortcut.

#!/bin/bash

shortcuts run "My Shortcut"

A third option is to run the rsync shortcut by way of the Dock. To do this, simply right click on the shortcut in the Shortcuts app and select Add to Dock. This creates a shortcut application in your ~/Applications folder and places an icon for the shortcut in your dock.

Thought I’d give it a try, but after downloading the shortcut, all attempts to install it via double-clicking brought up an error. See screenshot.
Screenshot

1 Like

Same error, although the additional one you posted “Folders Rsync.shortcut” installed perfectly. I’ll spend some time tomorrow looking at that one.

Just in case it would make a difference, I’m on Sonoma 14.7.5.

1 Like

The following shortcut creates a backup of folders selected by the user and creates a dated backup in the path specified by the user. The number of files transferred is shown in a notification, although the notification may not be shown when the shortcut is run in the Shortcuts app.

Rsync Folders.shortcut (23.2 KB)

The Get Device Details action returns a limited amount of information about the current computer. If additional information is needed, the system_profiler shell utility can be used.

In the following example, the resolution of the current display–which may or may not be the same as the UI resolution–is returned.

This shortcut uses a Match Text action to parse the output.

The following is a list of some of the categories of information that can be gotten.

SPApplicationsDataType, SPAudioDataType, SPDisplaysDataType, SPHardwareDataType, SPInstallHistoryDataType, SPNetworkLocationDataType, SPLogsDataType, SPPrintersDataType, SPSoftwareDataType, SPStartupItemDataType, SPStorageDataType

The system_profiler utility tends to be slow, typically taking from 100 to 300 milliseconds to run, depending on the requested information.

This shortcut uses the mdls shell utility to get metadata for a file selected in a Finder window and saves the metadata in a text file in the folder that contains the selected file. The shortcut reformats the mdls output, but this is easily changed by deleting the Replace Text actions. The drive that contains the selected file must be indexed by Spotlight for the shortcut to work.

MDLS Metadata.shortcut (23.3 KB)