45

What steps do I need to run a command line program via a global shortcut?

I setup Automator to create a new Run Shell Script and tried to assign a shortcut in System Preferences -> Keyboard -> Keyboard Shortcuts, but nothing happened.

7 Answers7

47

Without knowing the specifics of what you are looking for, I'd suggest creating a new Service through Automator. Services can be bound to keystroke triggers, and can execute shell scripts (among other things). enter image description here

To set up the keyboard shortcut, go to System Preferences -> Keyboard -> Shortcuts -> Services, select the service you just saved, and then click the "Add Shortcut" button.

Daniel
  • 34,803
  • 1
    It's not clear to me after saving the service how you set up a keyboard shortcut. – Liron Yahdav Feb 06 '18 at 18:28
  • @LironYahdav See above edit – Daniel Feb 07 '18 at 19:29
  • On Mojave, my simple Terminal command wouldn't work. In order to get my shortcut to work, I had to change the "Workflow recieves" setting in the Workflow to "no input". – rinogo Jun 03 '20 at 00:58
  • Note that the shortcut you choose should be "free" (not used by any application you currently have active). I also experienced a bug where the service would not run until I manually ran it once as described here – grokpot Jun 05 '21 at 13:28
  • For anyone wondering how to edit an existing "Service": in finder, navigate to ~/Library/Services (or press Cmd-Shift-G and type the path directly), then double click on your "Service". It should open in automator. – Jay Sullivan Jul 05 '21 at 20:41
  • Another gotcha: it seems the .zshrc file doesn't get run as this is a "non-interactive shell". If you're setting any environment variables in your .zshrc file (like I was), move them to your .zshenv file instead. See https://apple.stackexchange.com/questions/388622/zsh-zprofile-zshrc-zlogin-what-goes-where – Jay Sullivan Jul 05 '21 at 22:46
  • How do you put here the path of a shell script which is already managed from ~/Documents/src and installed in /opt/bin? – dan May 23 '22 at 17:04
19

I tried all those options, but they're too complex and unreliable or really expensive. So i ended up writing my own solution (open-source and free).

iCanHazShortcut

It can't do much right now, but that's a start. Feel free to contribute by adding feature requests and suggestions.

deseven
  • 191
  • 2
    I've been trying to find an easy solution for this problem for a few hours now and your application is the only one that works and does what it's supposed to do without any complicated bells and whistles. Thanks! – seymar Feb 21 '18 at 21:05
  • 1
    @StijnMartens you are welcome! That's the whole idea behind it - if you just need a shortcut for a command, you get a shortcut for a command. Nothing more, nothing less. – deseven Feb 27 '18 at 00:27
  • 1
    This worked beautifully. – Brian Schermerhorn Apr 27 '18 at 14:54
  • 2
    I tried both Alfred and Automator and they were so slow. This one works like a dream. Article covering it here: https://www.howtogeek.com/286332/how-to-run-any-mac-terminal-command-with-a-keyboard-shortcut/ – Chris Redford May 22 '20 at 01:52
12

FastScripts includes all scripts in ~/Library/Scripts/, so you don't have to add files manually. It also supports application-specific scripts.

Other applications that can be used to assign shortcuts for running scripts:

You can also create an Automator service:

  1. Open Automator and choose the Service template
  2. Add a Run Shell Script or Run AppleScript action
  3. Check that the input type and application scope are correct
  4. Paste the script and save the service
  5. Assign a shortcut in the Keyboard preference pane

There's a bug in 10.7 and 10.8 where the shortcuts for services don't always work until the services menu has been shown from the menu bar. Shortcuts for services don't work when the frontmost application doesn't have a menu bar or a services menu. There's also a short delay before services are run.

Lri
  • 105,117
  • plus one for mentioning other apps that do it as well. I almost went to install spark when I already used QuickSilver. I didnt know it had that option. – Marlon Dec 09 '11 at 07:23
  • also, the section in the "Keyboard preferences" pane to add a shortcut will be under the "Services" section, if using the Automator approach. – Brad Parks Apr 08 '14 at 16:34
9

Applescript it:

do shell script "ls ~/Desktop"

Add what you want there, then you can run the Applescript via a global hot key application. I would recommend Spark

Oh, and I don't know about assigning an shortcut through System Preferences

nohillside
  • 100,768
daviesgeek
  • 38,901
  • 52
  • 159
  • 203
2

Quicksilver can be used to bind hotkeys to actions.

If you find yourself doing something frequently, you can speed up the task even more by assigning a “trigger” to it. Triggers can be activated by keyboard shortcuts or mouse movements

If it's a Unix command or shell script you want to execute, this Stackoverflow post gives some more information on using Quicksilver's "Terminal module".

binarybob
  • 9,875
1

I'll add my answer here using BTT, since it's easy and can actually be bound directly to a command line script:

  1. Select Keyboard from BTT

Select Keyboard from BTT

  1. Input your shortcut (or key sequence!)

Input your shortcut (or key sequence!)

  1. Select "Trigger Predefined Action" and type "shell" to find the action.

Select "Trigger Predefined Action" and type "shell" to find the action.

  1. You can paste your script and shell that you want to run in:

You can paste your script and shell that you want to run in:

Click save and you're finished! Now test your script with the keyboard trigger you set up.

  1. EXTRA: You may want to add notes to remember what the shortcut is for...

You may want to add notes to remember what the shortcut is for...

0

It appears as though this was available in 10.6, (see http://hints.macworld.com/article.php?story=20090903085255430) but something broke in 10.7.2 (and possibly earlier 10.7)

I have found though that the Services keyboard shortcut is honoured when System Preferences is at the forefront, but not in any other application, including Finder.

However, the earlier suggestion to use Spark worked perfectly in lion, except that I couldn't use Ctrl in my keyboard shortcut. (not a showstopper!)

adamrmcd
  • 1,540
  • only when the application menu is in focus. If you click the desktop and tried the shortcut it doesnt work. click on the menu for finder and then try it and it works. I have a service for Terminal that has a shortcut and shows up on all apps...but it doesnt work if I'm not inside an app and even then the menu needs to have focus. – Marlon Dec 09 '11 at 07:19
  • @Lri even after loggin out and back in, restarted the machine, etc. It still doesnt work unless I do as I described above. Setting it in Quicksilver works though. I would like to know how come QuickSilver can catch those "global" shortcuts and not the OS. – Marlon Dec 09 '11 at 19:12