Assign a shortcut to running a script in OS X

33

27

Does OS X provide an easy way to bind key combinations to commands which are typically run on the command line?

For example, is there some way to assign ⌘ Cmd+L as a global shortcut to quickly lock my screen, possibly using /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine?

Stefan Lasiewski

Posted 2010-06-17T23:03:59.650

Reputation: 2 881

Answers

21

Yes, you can do this with Applescript or Automator.

The way I've done things like this is to use Automator to create a Service that is available in all applications. A service can be made to run a bash/zsh/ruby/whatever script. Then you can bind a key combination to the service in the Keyboard pane of system preferences.

Near the end of this MacBreak podcast this procedure is shown in detail.

Edit: I just noticed you're not using Snow Leopard. I don't think creating a system-wide service is as easy with 10.5.8. I guess you can't upgrade since it's a Powerbook. You might want to look into FastScripts which allows you to assign keyboard shortcuts to applescripts (free to use for up to 10 keyboard shortcuts) and use the following Applescript:

do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine"

ghoppe

Posted 2010-06-17T23:03:59.650

Reputation: 6 124

36

I currently use FastScripts. It includes all scripts in ~/Library/Scripts/, so you don't have to add scripts manually. It also supports application-specific scripts, and it has a few features that are useful with AppleScripts, like it stops a script before a keystroke or key code command until modifier keys are released.

If you have bought the Powerpack for Alfred 2, you can create workflows like this:

Hotkeys have a short delay by default, but you can reduce it by changing the trigger behavior:

KeyRemap4MacBook also supports running shell scripts:

<?xml version="1.0"?>
<root>
  <vkopenurldef>
    <name>KeyCode::VK_OPEN_URL_SHELL_say_a</name>
    <url type="shell">say a</url>
  </vkopenurldef>
  <item>
    <name>test</name>
    <identifier>test</identifier>
    <autogen>__KeyToKey__ KeyCode::A, KeyCode::VK_OPEN_URL_SHELL_say_a</autogen>
  </item>
</root>

Other applications that support assigning shortcuts to scripts:

Another option is to create an Automator service:

  1. Open Automator and choose the Service template.
  2. Check that the input type and application scope are correct, or change "selected text" to "no input" if the service does not receive any input.
  3. Add a Run Shell Script or Run AppleScript action for the script.
  4. Save the service and give it a shortcut from System Preferences.

There has been a bug since 10.7 where the shortcuts for services don't always work until you hover over the services menu from the menu bar. Shortcuts for services don't work when an application that doesn't have a services menu (like Tower) is front-most. There is also a noticeable delay before services are run.

Lri

Posted 2010-06-17T23:03:59.650

Reputation: 34 501

Quicksilver instructions: http://www.macosxtips.co.uk/index_files/run-applescripts-with-keyboard-shortcuts.php

– paragbaxi – 2015-02-06T19:45:24.247

FastScripts is $10 shareware but well worth it. – a paid nerd – 2015-04-02T17:50:09.247

Do you have a basic FastScript that does something like bind F1 to open a terminal or whatever? – Nick T – 2016-02-05T20:14:48.847

1Please add Hammerspoon to the list :) – vault – 2017-02-01T14:36:44.097

open -a ScreenSaverEngine is even a tad shorter. – Jari Keinänen – 2011-04-13T20:13:35.327

1

If you are using BetterTouchTool you can assign a shortcut to shell command by selecting a following action: Controlling Other Applications > Execute Terminal Command

alexeit

Posted 2010-06-17T23:03:59.650

Reputation: 451

0

Here is an example KeePass.sh script I made. I just did Get Info on it and set it to run with Terminal. After that, I created an alias to it on my Desktop. It runs the KeePass.sh script inside a Terminal and then after you exit the application, it kills any open Terminal windows. You may want to tweak that last line :-)

cd ~/Documents/KeePass
/Library/Frameworks/Mono.framework/Versions/current/bin/mono KeePass.exe 1>/dev/null 2>/dev/null
cd ~
killall Terminal

You don't need to use anything special like in everyone else's answers here.

anon

Posted 2010-06-17T23:03:59.650

Reputation: 1