Backup and Restore Shortcuts

I recently did a clean install of Sequoia and had to deal with the chore of restoring over 140 shortcuts. I had hoped they would be restored by way of iCloud, but that didn’t happen.

I keep regular backups of my shortcuts using the shortcut included below, which creates one file for each shortcut. Restoring one or a few shortcuts is a simple matter. Restoring all shortcuts can be done with the Shortcuts app’s File > Import… menu item, but you have to manually scroll through every shortcut and select the Add Shortcut button at the bottom. Afterwards you have to recreate the folder system. All told, this took 30 minutes or so, and I’ve only had to do this once, but I wish there was a simpler approach to restore all shortcuts at once. Google did not provide an easy solution.

Shortcuts Backup.shortcut (22.4 KB)

1 Like

Read through your post here, and although not something that I would need (I have a grand total of 8 shortcuts) one search proved helpful.

This first link is to the page that lists the shortcut: https://www.reddit.com/r/shortcuts/comments/s3q0r4/backup_on_mac_backup_shortcuts_locally_on_mac/

This second link is to the download/explanation page: RoutineHub • Backup on Mac

I’d be interested in your thoughts.

Update: The shortcut was created by a member here at MacScripter. One “gluebyte” who’s active here.

1 Like

Homer712. Thanks for the link–that’s very helpful.

I tested gluebyte’s shortcut, and it worked great except when restoring a shortcut. I suspect that’s because AppleScript GUI scripting is used, which often doesn’t work in a shortcut on my Sequoia computer. I did verifiy that all permissions were set.

I included the GUI scripting in the script included below. I separately used this script in Script Debugger then Script Editor to restore 20 shortcuts, and the script completed without issue. This doesn’t deal with the folder issue, but it’s way better then manually installing 140 shortcuts. Thanks gluebyte!

--This script restores shortcut files selected in a Finder window.
--A duplicate shortcut is created if a shortcut already exists in the Shortcut app.
--The installShortcut handler is an edit of AppleScript code in a shortcut posted by gluebyte on reddit.

set theDelay to 0.5 --test different values

tell application "Finder" to set theShortcuts to selection as alias list
if theShortcuts is {} then display dialog "A shortcut must be selected in a Finder window" buttons {"OK"} cancel button 1 default button 1 with title "Shortcuts Restore"

tell application "Shortcuts"
	activate
	delay theDelay
	set theCounter to 0
	repeat with aShortcut in theShortcuts
		if (aShortcut as text) ends with "shortcut" then
			open aShortcut
			delay theDelay
			my installShortcut(theDelay)
			delay theDelay
			set theCounter to theCounter + 1
		end if
	end repeat
end tell

tell me to activate --force dialog to front when run in a script editor
display dialog (theCounter as text) & " shortcuts were restored" buttons {"OK"} cancel button 1 default button 1 with title "Shortcuts Restore"

on installShortcut(theDelay)
	tell application "System Events" to tell process "Shortcuts"
		tell window 1
			if (exists scroll area 1) then
				set sa_ to scroll area 1
			else
				set sa_ to scroll area 1 of group 1
			end if
			if (exists sa_) and (count of buttons of sa_) = 2 then
				click button 2 of sa_
				if (exists sheet 1) and (count of buttons of sheet 1) < 3 then
					click button 1 of group 1 of sheet 1
				end if
				delay theDelay
				if exists sheet 1 then
					click button 2 of sheet 1 --keep existing shortcut and create duplicate
					--click button 1 of sheet 1 --replace existing shortcut
				end if
			end if
		end tell
	end tell
end installShortcut

This is really great. It’s simple and reliable.
It worked perfectly without any issues.
I really like it! :blush:

The shortcut is similar to that in post 1 except that the shortcut files are saved in subfolders that correspond to the folders in the Shortcuts app.

Shortcuts Backup by Folder.shortcut (22.9 KB)

With as many shortcuts that you have, possibly you haven’t noticed, but the Shortcuts app seems to have an issue counting the shortcuts. I have only 9 shortcuts, the app insists that I have 10 shortcuts.

Homer712. That’s very odd. I counted all my shortcuts and everything added up correctly. I wonder if there’s a hidden folder with the tenth shortcut, although I don’t know how to hide or unhide a folder in the Shortcuts app.

Somehow, magically the listing has corrected itself, so I’m now back to the correct shortcuts count, 9.

1 Like

There is an oddity that impacts both of the above backup shortcuts, and it occurs when:

  • The backup folder is on an external drive;
  • the backup folder set in the File action is the actual backup folder; and
  • the Overwrite if File Exists option is enabled in the Save File action.

When all of the above are true, the shortcut either reports an error or does nothing when run. For example, the following reported an error:

A workaround that’s effective on my Sequoia computer is to set the File action to the volume that contains the backup folder and then to specify the path to the backup folder in the Save File action, as in the following:

In my second backup shortcut above, the subpath would be set in the Create Folder action:

The simplest solutions, of course, are to save the backups on the boot drive or to disable the overwrite option.

In retrospect, I think the following is a simpler workaround to the file-overwrite issue. The user only needs to set the first action to either their home folder or to the volume that will contain the backup folder. The second action specifies the subpath to the backup folder and can be edited if desired.

Shortcuts Backup.shortcut (23.1 KB)