43

How do I assign a keyboard shortcut to an AppleScript? I found a script for automatically going back to the inbox in Office 2011.

  • 1
    As pointed out by this answer keyboard shorcuts assigned through services are not always consistent. – William Mar 20 '17 at 0:14
48

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.

  • 2
    That 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 6 '17 at 5:56
  • Figured out the problem the last step is different on my OS mizage.com/help/accessibility.html#10.7 – William Mar 13 '17 at 0:59
  • 4
    What do you mean by the preferred app? – William Mar 17 '17 at 19:42
  • 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 8: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
15
+50

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

  1. Launch Automator (usually found within your Applications folder)
  2. Go to File > New
  3. Select Service and click Choose
  4. In the top right hand of the window that appears, ensure that "No input" is selected from the Service receives drop-down list
  5. Ensure the In drop-down list is showing "Any application"
  6. In the second column containing the long list, find "Run AppleScript" and double-click on it
  7. This will add the Run AppleScript window on the right
  8. Now, replace the (* Your script goes here *) with the script you want
  9. Save the service using a meaningful name (e.g. GoToInbox).

Now to the next step.

2. Creating your shortcut

  1. Go to System Preferences > Keyboard > Shortcuts
  2. Select Services from the sidebar
  3. Find your service (it'll be in the list on the right-hand side)
  4. Add a shortcut by double clicking on the service name
  5. Now go to System Preferences > Security & Privacy > Privacy
  6. Select Accessibility in the sidebar
  7. Click on the + sign (you may need to unlock the padlock if it's locked)
  8. Add Automator.
  9. Add Finder (to find this you will need to navigate to /System/Library/CoreServices/Finder.app).
  10. 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.

  • 1
    Why 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
  • 2
    Yes, 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 0: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 0: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 6:05
9

You cannot assign keyboard shortcuts to AppleScripts out of the box. However, you can:

  1. use a launcher that supports setting global hot keys and executing AppleScripts (Alfred, Keyboard Maestro, FastScripts - whatever takes your fancy), or
  2. 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.
  • Just follow the steps in @MegaCookie’s answer. – kopischke Jan 29 '17 at 22:57
  • 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
  • 1
    It’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
3

Note: In macOS Mojave it was renamed from "Service" to "Quick Action" in Automator.

  1. cmd + space and type "automator"
  2. Select: File > New
  3. Select "Quick Actions"
  4. Search in the top of the left section for "applescript" or "shellscript" or "javascript"
  5. Drag desired action to right side workflow
  6. File > Save with desired name

To add keystroke to your workflow:

  1. Open System Preferences
  2. Select Keyboard > Shortcuts
  3. Select Services on left
  4. Find newly created service and double-click on "none" to add keyboard shortcut
  • 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 yesterday
-2

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

https://support.apple.com/kb/PH18418?locale=en_US

  • 3
    That 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 3 '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 1 at 3:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .