How do I assign a keyboard shortcut to an AppleScript? I found a script for automatically going back to the inbox in Office 2011.
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.
Now go to System Preferences > Keyboard > Shortcuts. Select Services from the sidebar and find your service. Add a shortcut by double clicking
Finally go to System Preferences > Security > Privacy > Accessibility and add Automator and the preferred app to run the shortcut.
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.
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.
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"
- 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
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.
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.
You can go to System Preferences -> Keyboard -> Shortcuts -> add a new shortcut. You can then select your script.