Convert Shortcuts to run with Applescript via Fastscripts

Who needs another menubar app to run Shortcuts when you already have Fastscripts? I made this to write applescripts to run shortcuts which are compiled using Script Editor and saved in Scripts folders so they are accessible from Fastscripts.

The first dialog which pops up will let you browse every shortcut and select what you want. If you want to narrow it down, click “–Search—” at the top of the list and OK out of the dialog and you can then enter a search term and the Shortcuts list will be filtered. I was thinking a “Search” button, but Choose from List only seems to let you have an OK and Cancel button, so improvisation.

Still thinking about the most efficient way to do this. Right now I am thinking maybe label new shortcuts on the Mac with the name of the program so you can filter them out for conversion with this script to quickly save them in their Scripts folder in one blast. After that you can bulk rename them in the Finder to keep the names shorter in the Fastscripts menu.

Needs a little beta testing, I have only run it a few times and it seems to be working as far as I can tell. :smiley:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
(*
tell application "Shortcuts Events"
	run shortcut "Check apple stock"
end tell
*)
set myshortcut to {}
tell application "Shortcuts"
set browseAll to name of every shortcut
end tell
set ShortcutList to choose from list ({"-- Search --"} & browseAll) with title "Pick Shortcuts" with prompt "Select shortcuts to convert:" default items 2 OK button name "OK" cancel button name "Cancel" with multiple selections allowed and empty selection allowed

repeat 1 times -- use this to allow script to "end" if user selects nothing
if (count ShortcutList) = 0 then -- user hit OK without a selection
	exit repeat -- essentially terminates the script as everything beyond this point is contained in this repeat handler
else
	if item 1 of ShortcutList is "-- Search --" then --user selects "Search" by itself to get the search field to pop up next
		set tvalue to display dialog ¬
			"Search for shortcuts with word:" default answer ¬
			"test" hidden answer false ¬
			buttons {"OK", "Cancel"} ¬
			default button ¬
			"OK" cancel button ¬
			"Cancel" with title ¬
			"Shortcuts Name Search" with icon 1
		tell application "Shortcuts"
			set mychoices to name of (every shortcut whose name contains text returned of tvalue)
		end tell
		set myshortcut to choose from list (mychoices) ¬
			with title ¬
			"Shortcut Picker" with prompt ¬
			"What shortcut do you want?" default items 1 ¬
			OK button name ¬
			"OK" cancel button name ¬
			"Cancel" multiple selections allowed true ¬
			without empty selection allowed
	else
		set myshortcut to ShortcutList
	end if
end if

repeat with x from 1 to (count myshortcut)
	set item x of myshortcut to (item x of myshortcut) as text -- convert to text for easier handling
end repeat
-- pick path where you want Fastscripts to access this shortcut
set myFolder to choose folder with prompt ¬
	"Pick folder to save to:" default location ¬
	alias (((path to library folder from user domain) as text) & "Scripts:") invisibles false ¬
	multiple selections allowed false ¬
	without showing package contents
-- now use Script editor to compile and save the scripts
tell application "Script Editor"
	launch
	repeat with x in myshortcut
		set _path to (myFolder as text) & x & ".scpt" -- add "Shortcut" to the filename if you wish to identifiy Shortcuts in Fastscripts
		set myNewScript to "use AppleScript version \"2.4\"" & return & "tell application \"Shortcuts Events\"" & return & "run shortcut \"" & x & "\"" & return & "end tell"
		make new document at end of documents with properties {description:"Applescript to run Shortcut via Fastscripts", contents:myNewScript}
		tell front document
			compile
			save in _path
			close saving no
		end tell
	end repeat
	quit
end tell
end repeat
2 Likes

Can you post an example of the (AppleScript) output?

I can kind of guess it but:

  1. People will appreciate a sample.
  2. We might want to suggest tweaks to what’s generated.

Thanks!

The block comment at the top of the script is a very basic example of what is necessary to run a shortcut using applescript. The script outputs an applescript that looks like this:

 use AppleScript version "2.4"
tell application "Shortcuts Events"
run shortcut "Check apple stock"
end tell

There really isn’t much to tweak. You tell “Shortcut Events” to avoid opening the Shortcuts app every time you run this.

1 Like

What a neat idea. Thanks.