Running a Shell Script in a Shortcut

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)

The documentation for the Run Shell Script action contains the following:

  • Pass input as arguments: The input will be converted to a list of strings and passed as arguments to the script.

  • Pass input to stdin: The input will be converted to a file and directed to the stdin pipe of the script.

If you need to pass a value to a shell script, the simplest approach is often to use a variable. However, in the following example, an error is reported if there are two folders.

The following is an example of the as arguments option. This works with multiple folders because the values are passed as a list.

The following is an example of the as stdin option.

This shortcut uses the defaults shell command to save and read a string in a plist file . The normal use of this shortcut would be to save a persistent value from one running of a shortcut to the next. An alternative is to save the persistent value to a text file.

The timing results to read and write the plist file were each 72 milliseconds. The timing result to read and write the text file were 6 and 22 milliseconds.

Save Plist.shortcut (22.5 KB)

Several years ago I got an AppleScript from this or the Script Debugger forum. I can’t find it now, but the script was not mine (in its original form).

The purpose of the AppleScript was to display a PDF copy of the man page of a specified shell command. I converted that script to a shortcut and have included it below. At the top of the shortcut, the user must set the location of a temp folder, which should be in their home folder.

The shortcut creates a temporary PDF file that is overwritten every time the shortcut is run. This could be changed to set the name of the PDF to the name of the shell command. Over time, this would yield a collection of PDF’s of interest.

The following screenshot only shows a portion of the shortcut.

Man Page Viewer.shortcut (23.8 KB)

And without any regular expressions…

Is the user ‘kai’ script herein what you’re referring to:

https://www.macscripter.net/t/scripting-system-preferences/47660

It is a long post so look for this: Creating Unix PDF Manuals

I found this —I think— around 2016 and have been using it since then to save a pdf for many shell commands (over 200 now). I don’t use them as often as I’ve become more conversant with the shell and can do most reading within the terminal. In 2021 I modified the script to use the palatino font which makes the files more readable for me (and also changed it to use ps2pdf). I’d do more with formatting but I’ve fallen into despair every time I’ve looked into the great roff-troff-groff abyss and since palatino is the only type 1 font I have that I like for this purpose…

Thanks Mockman. The one I remember included several posts by Nigel, who I seem to recall kept PDFs for frequently-used shell commands (much as you do). It may have also included a discussion concerning my use of the man shell command with the -w option to test for the existence of a man page. It’s probably not important, but I always like to credit other people for their work.

Didn’t find anything that matched but there are several that refer to Nigel’s script.

For example, this one has some back and forth on the subject: Handy Hint for Man Pages

Didn’t see any such threads containing any of your posts, though.

As an aside, the shell command itself appears to have first been posted on the old macosxhints site. It’s not available anymore. The site was purchased by macworld magazine and they maintained it for some years, moving it to their own domain, but it seems to be gone now. It is archived on the internet archive though.

1 Like

Thanks Mockman.

I edited my Man Page Viewer shortcut to work a bit differently:

  • The file names of created PDF man pages are the name of the shell command, and these PDFs are not removed or overwritten at any point.

  • The shortcut checks to see if there is an existing PDF for a specified shell command, and, if there is, the existing PDF is opened.

The folder that will contain the PDF man pages must be set at the beginning of the shortcut.

Man Page Viewer.shortcut (24.4 KB)

I found that I often want to view the man page of a shell command several times in succession, so I wrote a version of the shortcut that remembers the previously entered shell command. The user has to set the folders for the PDF and a preference file at the top of the shortcut. I tested using a plist instead of a text file to save the name of the shell command, but this was slower.

Man Page Viewer.shortcut (25.4 KB)

BTW, the default paper size of the PDF is letter, but this can be changed to one of several predefined paper sizes or to specific dimensions (in millimeters). The following is an example:

Shell scripts tend to be very fast, and there’s seldom a need to be concerned with execution speed. However, when that’s not the case, the following is a simple approach that measures the time it takes for a portion or all of a shell script to run.

Time Shell Script.shortcut (21.6 KB)

The Run Shell Script action allows the user to specify one of several shells, although most users will probably select Bash or Zsh. FWIW, I’ve been using Bash but decided to switch to Zsh because:

  • The Bash version shipped with macOS 26 was released in 2007.

  • Zsh is the default in macOS, and it seems desirable to stick with one shell.

  • I find the The Zsh shell easier to use in a few respects.

  • I’ve run timing tests and did not find any difference in the time it takes to run bash and zsh scripts.

On the other hand, AppleScript’s do shell script command uses Bash, and some users might want to maintain consistency between the shell used by this command and shortcuts.

Other than the man pages, there are two sources of zsh documentation that I find helpful. The first is a fairly technical manual:

zsh: The Z Shell Manual.

The second is a user guide with lots of good examples:

https://zsh.sourceforge.io/Guide/zshguide.html

Some notes:

In 2007, the GPLv3 licence (GNU General Public License) was introduced. Apple apparently does not like the GPLv3.

Until then, bash was released under the GLPv2, which apple does not seem to mind.

zsh is released under a permissive licence that doesn’t seem to place any burden on anyone.

Not only is apple’s bash itself stagnant but all of the provided gnu utilties are. Only the BSD ones are updated. Of course you can generally install newer versions if they exist.

do shell script provides, by default, a ‘sh’ environment, albeit emulated in ‘bash’. But ‘sh’ is what is meaningful to any script compatibility unless you change the shell used by the script. At least, this is how it worked for many years (eg around when TN2065 was published). From your screenshots, it looks like Shortcuts has its own options.

1 Like