How do I assign a keyboard shortcut to an AppleScript? I found a script for automatically going back to the inbox in Office 2011.
-
1As pointed out by this answer keyboard shorcuts assigned through services are not always consistent. – William Mar 20 '17 at 00:14
8 Answers
It is also possible without paid 3rd party apps!
1. Open Automator.
2. Make a new Quick Action.
3. Make sure it receives 'no input' at all programs.
4. Select Run Apple Script and type in your code.
5. Save!
Now go to System Preferences > Keyboard > Shortcuts. Select Services from the sidebar and find your service. Add a shortcut by double clicking (none)
.
Finally go to System Preferences > Security > Privacy > Accessibility and add Automator and the preferred app to run the shortcut.

- 103

- 1,350
-
3That last "Finally" is important – miss it, and you get a confusing "The action [...] encountered an error" dialog, just offering you to Show Workflow, likely not making you any wiser. – ecmanaut Mar 06 '17 at 05:56
-
Figured out the problem the last step is different on my OS http://mizage.com/help/accessibility.html#10.7 – William Mar 13 '17 at 00:59
-
7
-
It would be nice if there was a way to automate the step "now
go to System Preferences > Keyboard > Shortcuts. Select Services from the sidebar and find your service. Add a shortcut by double clicking (none).
' It must be possible, no idea how though... – Sancarn Aug 11 '17 at 08:19 -
3@William like if you wrote an AppleScript for in the Finder, just select both Automator and Finder in the Accessibility settings. – MegaCookie Sep 24 '17 at 11:05
-
I'm trying to assign F1, F2 etc to launch applications and this method almost works. For some reason, upon launching a new application, the F-key shortcuts don't register until I've first done cmd + F1. It's like the services aren't loading. Any ideas? – Dylan Hand Nov 05 '17 at 01:49
-
1For MacOS 10.13 ("High Sierra") users: "Accessibility" is found under "Security and Privacy" – asherbret Feb 27 '18 at 19:42
-
For certain versions of MacOS (e.g., my 10.13.6 "High Sierra"), the hotkey does not work if desktop or Finder is in focus, however, it works when any other application is in focus. Well, this is acceptable because MacOS is always full of bugs which mostly does not affect normal usage. – xuancong84 Jan 17 '19 at 10:33
-
I've received a request to leave a link to my answer to a related question. It involves using AppleScript in Automator to reposition/resize application windows. – Mentalist Nov 20 '19 at 12:36
-
3"It seems I sometimes have to run the script under services to get it to register before it will register the keyboard shorcut which is a shame." (services meaning go to your
App
, then go to menuApp > Services > Your script
, then give permission) - This comment by @William to the answer by monomeeth is important! – rfabbri Oct 24 '20 at 20:42
In summary you will need to use:
- Automator to create a service that runs an Apple Script
- System Preferences to assign a keyboard shortcut to the service
Below are the steps involved for each.
1. Create a service using Automator
- Launch Automator (usually found within your Applications folder)
- Go to File > New
- Select Service and click Choose
- In the top right hand of the window that appears, ensure that "No input" is selected from the Service receives drop-down list
- Ensure the In drop-down list is showing "Any application"
- In the second column containing the long list, find "Run AppleScript" and double-click on it
- This will add the Run AppleScript window on the right
- Now, replace the (* Your script goes here *) with the script you want
- Save the service using a meaningful name (e.g. GoToInbox).
Now to the next step.
2. Creating your shortcut
- Go to System Preferences > Keyboard > Shortcuts
- Select Services from the sidebar
- Find your service (it'll be in the list on the right-hand side)
- Add a shortcut by double clicking on the service name
- Now go to System Preferences > Security & Privacy > Privacy
- Select Accessibility in the sidebar
- Click on the + sign (you may need to unlock the padlock if it's locked)
- Add Automator.
- Add Finder (to find this you will need to navigate to /System/Library/CoreServices/Finder.app).
- Exit your System Preferences.
Now you should be able to do two things:
1. Run the service from any application by going to the Services list within any Application menu (e.g. Finder > Services, Safari > Services, TextEdit > Services, etc) and select the service you just created.
2. Use the keyboard shortcut to run the service.
NOTE: I am aware that for some users keyboard shortcuts do not seem to work even though they've been assigned to a service. This is a whole other topic, but if this happens in your case, at least you can run the service from within any application you're using.
Let me know how you go.

- 64,558
-
3Why must I add finder manually and what apps does this not work on in general? It seems I sometimes have to run the script under services to get it to register before it will register the keyboard shorcut which is a shame. – William Mar 19 '17 at 23:35
-
2Yes, the issue I referred in my Note at the end can often (but not always) be rectified by first running a service (and sometimes just accessing the Services menu is enough) before the keyboard shortcut will be recognised. This is an extremely annoying bug that has been around for a long time and I certainly recommend you send a bug report to Apple. As for why you have to add Finder manually, simply because it's not added by default. Finally, I didn't understand what you meant by what apps does this not work on in general? – Monomeeth Mar 19 '17 at 23:54
-
Ignoring my question about the app not working in some apps. It appears running the script once under the Service menu rectifies this issue and your explanation explains the issue I was having. Why must I add Automator Finder under the list Accessibility list it seems to not make a difference when running commands(That was my basic question)? – William Mar 20 '17 at 00:12
-
Good question! If you find steps 5 to 9 above for creating a shortcut aren't necessary for you, then that's great. However, I've helped a lot of people create keyboard shortcuts to run custom scripts and have found that including those steps often avoids an error message (don't recall what it is now or under which circumstances it would appear). Perhaps I shouldn't include those steps by default, but that's what I've been doing now for quite a few years (although the exact steps will differ depending on your version of macOS - above I included the steps for El Capitan as per your bounty). – Monomeeth Mar 20 '17 at 00:44
-
Steps 5 to 9 should be perfectly unnecessary for assigning a shortcut, though they might be needed to have the triggered script running smoothly if it uses UI Automation features (simulated mouse clicks and key presses). However, that last case is another issue entirely; you should not conflate two issues by including these steps in your answer. – kopischke Mar 29 '17 at 06:05
-
@kopischke Hmm, thanks for this! You may have just shed some light on why Steps 5-9 are needed sometimes and not other times. I'll keep this in mind for future testing and will update this answer (and others) if necessary. :) – Monomeeth Mar 29 '17 at 06:15
-
At least in MacOS 10.14.6, you have to approve every other application before it can control your browser. – th3an0maly Oct 17 '19 at 14:07
-
FWIW this only works for me if I choose a shortcut key combination that includes the command(apple) key. There are some reports of a bug related to this: https://apple.stackexchange.com/questions/260683/keyboard-shortcut-for-service-only-works-after-i-manually-run-the-service – Patrick Sanan Apr 15 '20 at 15:38
-
1
-
NOTE: For MacOS Monterey: If the Shortcut works and is non-conflicting it will appear behind the service in Finder > Services. So keep changing until you see it appearing there. – Ogier Schelvis Feb 21 '22 at 10:29
You cannot assign keyboard shortcuts to AppleScripts out of the box. However, you can:
- use a launcher that supports setting global hot keys and executing AppleScripts (Alfred, Keyboard Maestro, FastScripts - whatever takes your fancy), or
- put the script into a Run AppleScript action inside an Automator service. Note it has to be a service, not an application or an ordinary workflow: services can be assigned hotkeys in the System Preferences keyboard settings.

- 5,166
-
-
What OS version are you own my 10.6.5 mac doesn't seem to have the option "Select Run Apple Script and type in your code"? – William Jan 29 '17 at 22:59
-
1It’s not “Select Run AppleScript”, it’s – as noted in my answer – the Run AppleScript action you need to add to your Automator service; just drag it into the action area from the library, that will get you a text field with a function scaffold you can add your code to. Also, I doubt the accessibility settings in @MegaCookie’s answer are needed – these pertain to UI Automation. – kopischke Jan 29 '17 at 23:05
-
2After testing almost ~ALL~ solutions proposed in this stack, this is what I learned. Automator is very slow to execute scripts. It doesn't matter if it is a Python script or a Shell script. They both take 1-3 seconds to run on my Mac (script on simulates a click on the screen). I tested FastScripts and had the same issues, faster than Automator, but still slow (1-2 seconds). Couldn't test Alfred because it is a paid feature. However, I did test Keyboard Maestro, and it is blazing fast. No perceptible delay! The best solution hands down. – Guilherme Salomé Jun 16 '20 at 00:20
-
@GuilhermeSalomé Yeah Automator is insanely slow, thanks for specifying that Keyboard Maestro is faster, going to test it now! – Bohao LI Feb 27 '21 at 22:10
-
Note: In macOS Mojave it was renamed from "Service" to "Quick Action" in Automator.
- cmd + space and type "automator"
- Select: File > New
- Select "Quick Actions"
- Search in the top of the left section for "applescript" or "shellscript" or "javascript"
- Drag desired action to right side workflow
- File > Save with desired name
To add keystroke to your workflow:
- Open System Preferences
- Select Keyboard > Shortcuts
- Select Services on left
- Find newly created service and double-click on "none" to add keyboard shortcut

- 191
-
Note: if you don't make it a Quick Action (e.g. you make it a "Workflow") then the Shortcuts list won't detect it. – Sridhar Sarnobat Nov 13 '19 at 21:47
-
1I did it! It still does not detect the keyboard shortucts. I need to run the quick action manually inside automator! – chikitin Apr 14 '20 at 20:44
To bypass all the hassle with Accessibility settings I recumbent do run the script with the build-in Shortcuts app.
- Open Shortcuts app
- In the left pane choose Quickly actions or Menu bar (if you want to access this action from menu bar as well)
Click the + to add new action
- In the right menu and look for the Script Editor
- Double click on Run AppleScript
- Replace the (* Your script goes here *) with your code
- In the right panes with to the options tab. Check "Use as Quick actions " (Service Menu will be checked automatically) and click "Add keyboard shortcut"
- Press the key combination of your choice Your keyboard shortcut will trigger the AppleScript form any application without any rework of Accessibility options.

- 141
-
Two things: Is there a way to run an AppleScript file instead of pasting some code? And is there a way for AppleScript's
display notifications
to show when running from Shortcuts? – hobbes3 Jul 25 '23 at 00:20 -
@hobbes3 just write AppleScript to run your file! Something like: run script alias ((path to scripts folder as text) & "MyBestScript.scpt") – RyanCu Feb 22 '24 at 04:04
Just to reiterate a very important point first noted in the post by @MegaCookie:
When creating a script that emulates mouse or keyboard activity, you must add not only Automator, but also each and every app in which you wish to use your script to the System Preferences > Security > Privacy > Accessibility application list.
This is counterintuitive as you don't actually want those apps to control the computer, but this is what Apple requires. Otherwise, you will receive a popup like this (it will vary depending on what you're trying to do):
The action "Run AppleScript" encountered an error: "System Events got an error: com.automator.runner.xpc is not allowed to send keystrokes."
Clicking Show Workflow in the popup just opens up a Finder window to the location of the script. OK closes the popup.
This may not have always been necessary but I found it necessary in macOS 10.15 (Catalina).
As an example for when you'd need this, I created a script to emulate pasting the clipboard by simply "typing" the contents of the clipboard into an application that does not accept paste operations.
In my case, this is the remote console provided by VMware's vCenter when looking at VMs on ESXi hosts. I needed to add the browser being used to view vCenter, which in my case is Chrome. Definitely a security risk, IMHO.

- 141
I used BetterTouchTool which also works fast. It's also paid but it is popular.

- 100,768

- 31
You can go to System Preferences -> Keyboard -> Shortcuts -> add a new shortcut. You can then select your script.

- 13
- 1
-
3That won’t work. The document you are linking to describes how to (re-)assign a hotkey to a menu command of an application. – kopischke Mar 03 '15 at 20:59
-
Broken link, apple has long since disabled this one-click functionality, replacing it with a 90-click operation involving 3rd party apps. – Eric Leschinski Mar 01 '19 at 03:56
-
Do you mean to say that this entire method as outlined here by Arecibo won't work any more? I've gone down three rabbit holes on this, one with (unpaid) FastScripts (can't access preferences to set keyboard shortcut); one with Automator (Step 2.3 "Find your service in the list," and it's not in the list); and one here with the gnom1gnom method, in which in the end I can't add a keyboard shortcut for "⌘Q" because, well, it quits the Shortcuts app. The script is working fine, I just can't find how to map the shortcut to the script. Help? – Keith G Oct 24 '23 at 22:08