44

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.

3
  • 1
    My question is different. I don't want a terminal window to open up at all.
    – z-buffer
    Feb 23, 2012 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, 2012 at 5:34
  • It looks like there's no way to achieve this in 2023 Jun 23 at 22:37

7 Answers 7

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

8
  • Bingo. Odd this functionality isn't already built into OSX. Thanks so much!
    – Adam Flott
    Sep 3, 2011 at 21:30
  • @Adam Yeah, it is a little odd.
    – daviesgeek
    Sep 3, 2011 at 22:36
  • 10
    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, 2012 at 13:05
  • 2
    Also, AppleScript and Automator are part of OS X.
    – Daniel
    Dec 11, 2012 at 17:53
  • 3
    -1 for using 3rd parties without any good reasoning.
    – cregox
    Aug 27, 2015 at 22:56
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.

7
  • 1
    It's not clear to me after saving the service how you set up a keyboard shortcut. Feb 6, 2018 at 18:28
  • @LironYahdav See above edit
    – Daniel
    Feb 7, 2018 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 3, 2020 at 0: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 5, 2021 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. Jul 5, 2021 at 20:41
18

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.

4
  • 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, 2018 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, 2018 at 0:27
  • 1
    This worked beautifully. Apr 27, 2018 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: howtogeek.com/286332/… May 22, 2020 at 1: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.

2
  • 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, 2011 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, 2014 at 16:34
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".

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

2
  • Thank you, ended up going down this route since I have BTT. Jun 22, 2019 at 20:34
  • How could I make this script output text, whatever the cursor happens to be atm?
    – rsalmei
    Oct 28, 2021 at 21:29
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!)

2
  • 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, 2011 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, 2011 at 19:12

You must log in to answer this question.

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