Turn Any Action Into A Keyboard Shortcut On Your Mac

Macs have some great built-in keyboard shortcuts, but if you want to create custom or global shortcuts that perform more complicated tasks, you’ll need to do a little extra legwork. Here’s how to turn virtually any action into a keyboard shortcut.

Photo by Declan Jewell.

We’ve shown you how to turn any action into a keyboard shortcut on Windows using our favourite Windows scripting tool, AutoHotkey. In fact, we wish to death it was available on OS X, but it isn’t. We won’t be able to do everything we could with AutoHotkey on OS X, but we can get pretty close using OS X’s built-in scripting language AppleScript.

In this guide, we’re going to show you first how to save a script and use it as a trigger in Quicksilver (If you don’t have it, you’ll want to download it now — it really is an awesome piece of software). Then, we’re going to go through a few script templates that will demonstrate some of the ways you can use AppleScript to emulate other keyboard shortcuts, make certain shortcuts global, or perform more complicated actions.

Quivering at the words “scripting language”? Don’t be alarmed! AppleScript is extremely easy to use; once you learn a few basic commands, it’s just like speaking English. Furthermore, the types of scripts we’re going to cover here are very simple (just a few lines of code), so even folks with no coding experience whatsoever should be able to catch on quickly.

How to Assign a Trigger in Quicksilver

While Quicksilver is mainly used as a quick application launcher, it can do a lot of different things. One of those things is assigning what are called “Triggers” — basically a fancy term for keyboard shortcuts, or hotkeys. It has a lot of built-in triggers that launch apps, control iTunes, or do other things, but for the purpose of this article we’re mainly going to use these triggers to run our custom scripts.

In order for this whole thing to work, you’ll need to save your scripts in a safe place. If you don’t have a special folder for scripts already, I highly recommend creating one. I just have a folder in my Documents folder called “Scripts” in which I dump pretty much any script that doesn’t have a place, whether it be scripts for GeekTool, login items, or things like this that I’m going to pipe into Quicksilver.

Once you’ve written your script and saved it in its new home, open up Quicksilver and head into its Preferences (you may need to hit the arrow in the upper-right corner of its display, since it may not have a dock icon). Go to the Triggers tab. Most of your shortcuts will fall under the Custom Triggers heading on the left side. Hit the plus sign and select Hotkey to add a new shortcut.

You’ll then be prompted with a window in which you’ll describe what the shortcut does. In all these examples, we’re just going to be running a script, so just type in the name of the script you just wrote in the top box (labelled “Select an Item”). The Action box should say “Run” by default, so once you’ve got the right script in the “Select an Item” box, just hit Save. You’ll see it show up in the Triggers list.

To assign that script a hotkey, click on the rightmost part of that script’s entry, where it currently says “None”. In the new pane that pops up, hit the Settings tab and click on the box that says “Hot Key”. Type in the keyboard shortcut you want (make sure it isn’t already in use by something else, too), and exit Quicksilver’s preferences. You should now be able to run your script with your desired shortcut.

My favourite use of this feature is assigning actions to the top row of keys on my keyboard. If you aren’t happy with the options Apple gives you, you can always make them act as regular function keys by going to System Preferences > Keyboard and check “Use all F1, F2, etc keys as standard function keys”. That way, I can reassign certain function keys I don’t use (like F4, which triggers the Dashboard), to something useful (like putting my computer to sleep). These shortcuts are also really useful for assigning the usual actions (like controlling iTunes, launching Exposé, or changing the volume) to the function keys on non-Mac keyboards, since OS X won’t treat them the same as it does Apple keyboards. You can, of course, assign whatever actions whatever shortcut you want — that’s what customisation is all about!

Examples of Useful Scripts

While AppleScript can do a lot of things, there are a few common actions that lend themselves to Triggers in Quicksilver. You can use it to emulate other keyboard shortcuts, making existing shortcuts global, and or perform more complicated tasks with just a keystroke or two. Here are some example scripts that can act as templates for your own actions.

Emulate an Existing Keyboard Shortcut

Some of OS X’s keyboard shortcuts aren’t exactly friendly to the short-fingered (Control+Command+Option+Eject? Seriously?). Furthermore, if it’s a shortcut you need to run often, you probably don’t want to be stretching and hitting multiple keys day in and day out. Luckily, we can just whip up a little script that presses those keys for us and assign it to a simpler hotkey of our own.

Since I have to take a lot of screenshots working at Lifehacker, I’ve changed the Print Screen (aka F15) button on my Windows-based keyboard to a quick screenshot key on my Hackintosh. Since there’s already a keyboard shortcut for taking a screenshot (Shift+Command+4), we can just tell OS X to press those keys for us using the following script:

tell application “System Events”
key code 21 using {shift down, command down}
end tell

System Events is what handles things like keystrokes, and key code 21 refers to the “4” key. Sometimes, you’ll just be able to type in the key you want (see the mini player script in the section below), but some keys require you to use their codes. Check out a full list of key codes here, or download Full Key Codes to find them yourself. You can also find Apple’s full list of existing keyboard shortcuts at their web site.

Turn App-Specific Shortcuts Into Global Ones

Some apps have nice shortcuts built-in, but don’t apply globally — meaning the app has to be in focus for you to be able to use the shortcut. This isn’t ideal for a lot of apps — say, music players — so we’re going to make those shortcuts global by writing a short script that executes them for us no matter what window has focus.

A good example of this is if you’re using a music player other than iTunes. iTunes hijacks your Mac’s Play and Pause buttons for itself, so there aren’t any good global keyboard shortcuts to play, pause or skip tracks in an app like previously mentioned Clementine. So, we’ll just use AppleScript to call on its menu options for us using the following script:

tell application “System Events”
tell application “Clementine” to activate
tell process “Clementine” to click menu item “Previous Track” of menu “Music” of menu bar 1
end tell

Since that menu bar option will only be available if Clementine is the active window, we first have to tell OS X to focus it with the activate command. Then, we just pick our menu bar item from Clementine we want to assign to the shortcut. In this case, the “Previous Track” item from the “Music” menu.

Since Clementine Labels the Play/Pause shortcut as Play or Pause, depending on whether the music’s playing or not, the above approach won’t work (since a script can only tell it to pick the “Play” or “Pause” menu item). Instead, we’ll use Clementine’s built-in, non-global keyboard shortcuts to activate the Play/Pause function. So, instead of telling it to pick a specific menu item, we’ll just tell OS X to focus Clementine and hit F6, its shortcut for Play/Pause:

tell application “System Events”
tell application “Clementine” to activate
tell process “Clementine” to key code 97
end tell

For the sake of being thorough, here’s a similar script I use to quickly switch between the mini-player and full player versions of iTunes. It focuses iTunes and calls on its built-in shortcut Shift+Command+M:

tell application “System Events”
tell application “iTunes” to activate
keystroke “m” using {shift down, command down}
end tell

Notice that since “m” is actually recognised by AppleScript, I don’t need to find its key code. As such, the command I use to emulate hitting that key is keystroke as opposed to key code, as we used in the above two examples.

Start, Focus or Quit Apps with a Keystroke

The activate command we used above does two things: it focuses an app if it’s open, or it starts an app if it isn’t open. Thus, if you have apps you open and close a lot, we can give them a global keyboard shortcut by creating a simple AppleScript:

tell application “Address Book” to activate

While you could use Quicksilver’s built-in “open” function to start an app with a keystroke, this does a bit more. For example, your shortcut key will not only open the app, it will also focus or unminimise it if its already running — so you still only have one place to go on the keyboard to get access to that specific app.

This command is more useful when you couple it with other things, though. For example, there are other times where we may want to start up an app and close it quickly. For example, I’m not using an AirPort card on my computer — I’m using a USB receiver. Which is fine, except you need to start up its driver app to use Wi-Fi. You don’t need to keep it open, you just have to start it up once and then quit it. So, I created this script to start it up, wait a few seconds to let it register, and then quit. It works great for a quick keyboard shortcut or a login item.

tell application “ASUSTek WLAN Client Utility” to activate
delay 5
tell application “ASUSTek WLAN Client Utility” to quit

Another great use for AppleScript in this scenario is to start up a nagware app and hit the “Demo” button for you, so you don’t have to. For example, previously mentioned TextExpander shows an annoying window that makes you hit “Demo” to continue using the app when you first start it up unless you buy it. To bypass this, just use this AppleScript:

tell application “System Events”
tell application “TextExpander” to activate
key code 48
key code 48
key code 48
key code 48
key code 49
keystroke “w” using command down
end tell

This will hit Tab four times to navigate to the Demo button, hit Space to select it, and then close the window. Note that you’ll need the “All Controls” button turned on in System Preferences > Keyboard for this to work.

For other apps that require you to wait before hitting the button, you can just add in a line that waits the necessary number of seconds. For example, for previously mentioned Divvy, which makes you wait three seconds, use the following script:

tell application “System Events”
tell application “Divvy” to activate
delay 4
key code 48
key code 48
key code 48
key code 49
end tell

You’ll never have to click a demo button yourself again!

Assign Shortcuts to Functions That Don’t Currently Have Them

The last, and most complicated function here is assigning shortcuts to actions that don’t currently have them (or that have shortcuts that can’t be emulated with the above methods). For example. I want to be able to put my computer to sleep with a keystroke, but the shortcut for doing so is Option+Command+Eject. That’s not a nice shortcut, but there is no key code for the Eject key, so we can’t use the above methods to emulate it. Instead, we create an AppleScript that sleeps the computer directly:

tell application “Finder” to sleep

This method will require a bit more AppleScripting knowledge (or some nice Google-fu — I found this by searching sleep computer with an applescript — but it’s worth mentioning for those times that just automating simple tasks won’t cut it.

You can do quite a bit with AppleScript if you feel like learning the code, but even if you don’t, it’s really easy to learn a few key commands that can get you pretty far. If you’ve been unhappy with OS X’s keyboard shortcut support up until now, this method should make you much happier. Got any of your own favourite custom shortcuts for OS X? Share them with us in the comments.


The Cheapest NBN 50 Plans

Here are the cheapest plans available for Australia’s most popular NBN speed tier.

At Lifehacker, we independently select and write about stuff we love and think you'll like too. We have affiliate and advertising partnerships, which means we may collect a share of sales or other compensation from the links on this page. BTW – prices are accurate and items in stock at the time of posting.

Comments


Leave a Reply