26

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.

  • 1
    My question is different. I don't want a terminal window to open up at all. – z-buffer Feb 23 '12 at 5:31
  • 1
    If you don't want a Terminal window to open at all, you are asking the same thing as the linked post. – Daniel Feb 23 '12 at 5:34
7

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

  • Bingo. Odd this functionality isn't already built into OSX. Thanks so much! – Adam Flott Sep 3 '11 at 21:30
  • @Adam Yeah, it is a little odd. – daviesgeek Sep 3 '11 at 22:36
  • 8
    Except that it is built into OS X through Automator Services, @AdamFlott, it's just that the answer you accepted doesn't use that method. – Daniel Feb 19 '12 at 13:05
  • 2
    Also, AppleScript and Automator are part of OS X. – Daniel Dec 11 '12 at 17:53
  • 1
    -1 for using 3rd parties without any good reasoning. – cregox Aug 27 '15 at 22:56
31

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.

  • It's not clear to me after saving the service how you set up a keyboard shortcut. – Liron Yahdav Feb 6 '18 at 18:28
  • @LironYahdav See above edit – Daniel Feb 7 '18 at 19:29
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.

  • 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 9 '11 at 7: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 8 '14 at 16:34
7

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.

  • 1
    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! – Stijn Martens Feb 21 '18 at 21:05
  • @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 0:27
  • This worked beautifully. – Brian Schermerhorn Apr 27 '18 at 14:54
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".

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!)

  • 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 9 '11 at 7: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 9 '11 at 19:12
0

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...

You must log in to answer this question.

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