Clicky

Tweak Alfred to Run Shell Commands Without Opening Terminal

As I've written numerous times that I'm a keyboard person and a heavy Alfred app user.

I'll often run shell commands by using Alfred's ! feature. E.g. typing !ls -l will open a new tab in Terminal.app and run ls -l in it. Most of the time though, I just want to run the command and don't care about the results. The default behavior which switches to Terminal.app and opening a tab to run the command throws my focus away from the job at hand.

It turns out that we can do something about it.

Before we start, do this:

  1. Run this in Alfred !ls -l ~/.profile.
  2. Check if the Terminal output says “No such file or directory”. If it says that, use (5a) instead of (5b) below. If it doesn't say the file is missing, use (5b) instead of (5a) below.

Now do this:

  1. Open Alfred Preferences (type alfred into Alfred!)
  2. Click Features
  3. Click Terminal / Shell
  4. Click the dropdown beside Application and choose Custom.

5a. Paste the following into the text box:

on alfred_script(q)
    do shell script & q
end alfred_script

5b. Paste the following into the text box:

on alfred_script(q)
    do shell script "source ~/.profile; " & q
end alfred_script

Once you have done this, running shell commands with Alfred will no longer open a terminal tab. (Naturally, if there's an error in your shell command, you wouldn't be notified)

Buffer

Want more articles like this in your inbox? Join our newsletter to receive free email updates, tips and tricks related to iOS & OS X.