Use Shortcuts.app to run System commands through Spotlight

Did you know, that you can use Shortcuts, Spotlight and some AppleScript to build an Alfred replacement (sort of).

run system commands with
spotlight
run system commands with spotlight

Alfred is such a great application, and I am a Mega-Supporter, but I also like to use "native" tools as much as possible - like I do when I build things for the web... and I also like to tinker with Software. So one day I go a little bored and clicked my way through the shortcuts.app and recognized, that you are able to run AppleScript with it. Since Spotlight can also run commands from the Shortcuts.app I was just wondering if I could use it to run system commands.

Why would I do that you might ask. You know Alfred is great and super-fast but I often use it for quite a limited amount of things:

  • run system commands
    • empty the trash, hibernate the mac and much more
  • open applications
  • find files

These are my main use cases, but when I work with Jira I often add Emojis (like the warning sign). For that use-case you need a plugin as you do for other tasks, like converting units. And that is the nice thing with Alfred, you can extend it to your liking.

Spotlight on the other side supports a lot of features out ot the box (like converting units), but it takes a little more effort to extend it. You need to build functionality with the Shortcuts.app - and since the Shortcuts.app actually supports AppleScript, you are able to do things like restarting your Mac, like you can do with Alfred. I know, I could run AppleScripts directly with Spotlight when they are converted into an app, but if you run a script, that restarts you Mac, you simply get into a loop, if reopen applications is not disabled, which is pretty annoying.

System commands inside
Shortcuts
System commands as shortcuts

The System commands

So I've built some shortcuts, that simply run AppleScript and do things I could only do with Alfred before. Here is a list of them:

Quit all Apps

I often run it at the end of the working day, before shutting down the device.

- get list of open apps
tell application "System Events"
	set allApps to displayed name of (every process whose background only is false) as list
end tell

-- leave some apps open
set exclusions to {"AppleScript Editor", "Automator", "Finder", "LaunchBar"}

-- quit each app
repeat with thisApp in allApps
	set thisApp to thisApp as text
	if thisApp is not in exclusions then
		tell application thisApp to quit
	end if
end repeat

Shutdown

Because the MacBook doesn't have an eject key anymore.

tell application "Finder"
	shut down
end tell

Restart

Quickly restart your device without using the menu and without being stuck in an endless reboot loop.

tell application "Finder"
	restart
end tell

Sleep

Send the Mac to hibernation mode.

tell application "Finder"
	sleep
end tell

Eject all drives

If you want to take away your device when it is still mounted to some drives.

tell application "Finder" to eject (every disk whose ejectable is true)

Mount Drives

The downside is you need to manually edit the list of drives (myDrives) in the script, but then it works perfectly fine.

set myDrives to {"Time Machine", "Games"}
repeat with drive in myDrives
	set driveName to drive
	set driveInfo to do shell script "diskutil list | grep \"" & driveName & "\""
	set driveID to last word of driveInfo
	do shell script "diskutil mount " & driveID & ""
end repeat

Empty trash

Because who wants to right-click on the bin to empty it.

tell application "Finder"
	set warns before emptying of trash to false
	empty trash
end tell

What you need to do

If you don't want to install Alfred or a similar alternative like Quicksilver or Launchbar, you can add the scripts listed above to shortcuts like this:

  • open the shortcuts.app
  • click the +-sign to add a new shortcut
  • change color and icon
  • add a name in the opening window
  • search for Applescriptin the searchbox on the right
  • double-click run AppleScript
  • paste the script from this page "below the hammer"
  • click the "hammer" to format/build the script
  • close the window

This way you can extend spotlight to run system commands - which is quite nice.

add applescript
to shortcuts
add applescript to shortcuts

The downside

The downside currently is, that the shortcuts are pretty much hidden down in the list of spotlight, but that is about to getting changed with macOS Ventura, where you can reorder the results (again).