Mac OSX

How to: Run script or shell command using keyboard shortcut in Mac OS

Users sometimes need to run particular scripts repetitively on Mac OS. Finding that script and running it after pointing your terminal to the correct path becomes a tedious job. It’s a wise choice to assign a particular keyboard shortcut for the desired script or shell command. It means saving a few seconds as you can run that script without much hassle and time-wastage.

Automator: Choose shortcuts for scripts, commands etc.

We shall use a built in app called Automator to accomplish our goal. There are other third party apps available to run a script using keyboard shortcut, but we’ll use an Apple original app.

1. Give automator required accessibility permissions

You need to give permission to automator within accessibility before adding any shortcuts.

  1. Open Security and Privacy in System Preferences
  2. Scroll down and click Accessibility, unlock padlock icon at the bottom left, enter password and hit enter
  3. Under panel, allowed apps to control your computer, tick Automator

Now, you can use automator for keyboard shortcut.

2. Adding a script in Automator to assign keyboard shortcut

The script we are dealing here is a simple bash script. It opens Google website on Chrome browser upon execution. Similarly, you can also use other script using the same method.

  1. Open Script Editor in applications.
  2. Write do shell script “$HOME/Documents/Dump/google.sh”.
  3. Save it as an application.
Create application to run a shell script on Apple

Now, to assign keyboard shortcut to your application using automator, follow this procedure:

  1. Open Automator in applications.
  2. Choose Quick Action.
  3. In Workflow receives choose No Input, search for Launch Application and drag drop it in empty space.
  4. Choose options, select others and locate your script AppleScript app (In case you can’t select your application, make sure you rename it and don’t use spaces, Automator doesn’t identify an app name with spaces).
  5. Save quick action and give it a proper name.
Launch app in automator Apple

Last, you need to assign a shortcut for the quick action you just added.

  1. Open System Preferences > Keyboard > Shortcuts.
  2. Scroll down and find Services, in the end you’ll find your recently added quick action.
  3. Select it and click Add Shortcut. Make sure you add a shortcut key which isn’t associated with any other program.
Add services shortcut in Apple

Now, whenever you’ll press the specific key combination, your intended script will run. In my case, whenever I press the shortcut keys, Google Chrome opens and it goes to Google.com.

Conclusion:

You can run any script or console command on Mac OS using a keyboard shortcut. It’ll save your time. All you need is to convert your script into an application using Apple Script and then attaching that application to a keyboard shortcut using Automator. The process is simple and you can extend it to any kind of command-line script or software.