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)

This shortcut is very similar to that immediately above, and I edited the earlier shortcut just to make its operation a bit clearer. The forum software wouldn’t let me replace the earlier shortcut.

Backup Shortcuts.shortcut (23.2 KB)

I use the shortcut in the prior post to create backup copies of my shortcuts. It saves these shortcuts in subfolders that replicate those in the Shortcuts app.

When restoring shortcuts from a backup (normally when doing a clean install), I use the following AppleScript to recreate these subfolders (but not the actual shortcuts) in the Shortcuts app. This is only necessary when you have a lot of folders to restore.

--set the path to the shortcuts backup folder
set backupFolder to "Macintosh HD:Users:robert:Shortcuts:"

--get the names and count of subfolders in the backup folder
tell application "Finder" to set subfolderNames to name of every folder of folder backupFolder
set folderCount to (count subfolderNames)

--prompt user to proceed
display dialog "Do you want to create " & folderCount & " folders in the Shortcuts app? Existing folders will be skipped."

--create folders in the Shortcuts app
tell application "Shortcuts Events"
	repeat with aSubfolderName in subfolderNames
		if not (exists folder aSubfolderName) then
			make new folder with properties {name:aSubfolderName}
		end if
	end repeat
end tell

To restore the actual shortcuts, I simply drag them from the Finder window to the newly-created folders in the Shortcuts app. I considered writing an AppleScript to perform this task, but I couldn’t find an easy way to do this.

I tested the above procedure by deleting all 14 folders and 141 shortcuts in my Shortcut apps. Restoring them took perhaps 5 minutes, and I had to spend an additional 10 minutes recreating the shortcut icons in my Finder toolbar and the menu bar. The worst part of this was all the new Privacy & Security prompts that I had to respond to.

A few additional comments:

  • It goes without saying, but it’s important to have several tested backups of shortcuts.

  • The shortcuts in the Shortcuts app are stored in a database in ~/Library/Shortcuts, and it might be possible to restore this database with a Time Machine backup (according to Google AI).

  • I assume shortcuts can be restored from iCloud, but I’ve never done that.

  • The Shortcuts app has an Import menu item which works with a backup, but it imports the shortcuts to the All Shortcuts folder.

  • After restoring the shortcuts, a shortcut here can be used to sort them alphabetically or by date.