Jump to content
vitor

RunCommand — Run shell commands without opening a terminal

Recommended Posts

Provides both a File Action to run shell commands on selected files/directories and a Keyword to run them on the current directory. It will detect your default shell, and if it’s bash, zsh, tcsh, csh, or fish, it’ll load the appropriate startup files and your aliases.
 
For the File Action, use Alfred to pick what you want to run a command on and choose Run command. Alfred’s main window will appear and you simply type the command you want.


uFwqIxg.png
 
For the Keyword, type . followed by a command. A space between the period and the command is not necessary. This will run the command on the active Finder window (e.g. .touch this_file). You can also easily chain commands (.cd Desktop && rm that_file).


RwM3Rl5.png


For commands that would benefit from the files not being at the end (like cp and mv), you can use {} as a placeholder for the files. So if you select some files and want to copy them to ~/Desktop/ you’d type cp {} ~/Desktop/.


To get the output of a command as Large Type, actioning it with ⌘↵ instead of ↵.

 
Download | Source

Edited by vitor

Share this post


Link to post

This is a file action to run a terminal command on selected files/directories.

 

Just use Alfred to pick what you want to run a command on and pick “RunCommand”, as the action. Alfred’s main window will appear and you simply type the command you want.

 

Extra notes

The workflow actually consists of two parts. The first one is the only one you’ll want to call explicitly; “RunCommand”, which calls “runcommand”. I’d prefer to have done this a bit differently, but Alfred doesn’t allow connections to the “File Action” trigger that’d allow to do it (at least not without adding a lot more).

 

Download

https://github.com/vitorgalvao/alfred-workflows/raw/master/bin/RunCommand.alfredworkflow

 

Source

https://github.com/vitorgalvao/alfred-workflows/tree/master/RunCommand

 

License

I don’t care, do whatever you want with this. If you make any changes that could benefit the script, I’d certainly appreciate if you’d share them (perhaps I could incorporate them), but it’s not at all required.

 

Nice. You might want to change to xargs to support spaces.

 

So, your perl becomes this: echo "{query}" | perl -pe 's/[\t\n]/\0/g' > file_list

And your bash becomes this: cat file_list | xargs -0 {query}

Share this post


Link to post

You might want to change to xargs to support spaces.

 

You’re right, I thought I had tested for that. Thank you, it’s now fixed.

Share this post


Link to post

Update.

Previously this would get your command and run it with the files as arguments at the end. You can now pick where the file arguments should go in the command (this is optional), by using “{}” (original post updated to reflect this information).

 

Great workflow!
I personally added a "Large Type" at the end of the runcommand, otherwise I had no idea how the command ended

 
Thank you. It now outputs a notification by default, with the output of the command. This way it can show a notification when you ask (when using “‐v”, for example), and not when you don’t.

Edited by Vítor

Share this post


Link to post

Could you expand this for using PathFinder? I am trying to get the workflows I use a lot moved to workable with that app.

 

There’s no reason it shouldn’t work with PathFinder — this does not depend on Finder in any way, and it works for me (I’m also a PathFinder user), what command are you trying to run?

Share this post


Link to post

I am trying to use it with ImageMagick convert routine, but nothing ever happens. I assumed it was the use of PathFinder because so many do not work with it. Upon examining the workflow more, I see that it doesn't use Finder. I can not figure out why it will not work for me. Any idea?

Share this post


Link to post

I am trying to use it with ImageMagick convert routine, but nothing ever happens. I assumed it was the use of PathFinder because so many do not work with it. Upon examining the workflow more, I see that it doesn't use Finder. I can not figure out why it will not work for me. Any idea?

 

Yes, I think I might know what’s wrong. Did you install ImageMagick via homebrew? Alfred “Run Script” doesn’t read you shell’s profile, so that’s probably the issue. Two very quick fixes. You can simply run (in the terminal)

ln -s /usr/local/bin/convert /usr/bin

which works, but then you'd have to do that for every command you want to use. Or you could open the workflow, and on the last “Run Script” (the one that connects to the notification), you add at the top of the code something like

export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
Edited by Vítor

Share this post


Link to post

Update.

 

It will now detect your default shell, and if it is bash or zsh, it will source the appropriate files (which means it should now get your export PATH, alias, and what else you might have there).

Share this post


Link to post

Big update.

CommandRun is now consolidated into RunCommand.

Support for csh and tcsh was dropped, and support for bash and zsh was greatly improved, including loading of aliases.

Share this post


Link to post

I can show you how to do it, but I won’t change it officially without a strong reason. I’ve picked {} because it’s unlikely you’ll need to type it on a command, or by mistake; kk, on the other hand, could easily break. Why would you like a different keyword?

To change it, open the workflow’s directory, open the runcommand file in a text editor, and change the 25th line accordingly. Where it says xargs -0 -J {}, change it to xargs -0 -J kk.

Share this post


Link to post

I don't understand the complexity of RunCommand

 

Why can't you just have a /bin/bash script block whose first line contains {query}

 

Then if you have a keyword 't' you can just type things like

 

t date

 

or

 

 

t ls /Applications | grep -i photo

 

 

Seems to work fine ---

Share this post


Link to post

Those specific commands work fine because they’re extremely limited, can’t act on selected items, rely only on system commands, and don’t take into account your own choices/customisations. This workflow was made exactly to bypass those limitations.

If you have selected files/directories (or just a Finder window open in front) and want to act directly on them without having to write their path on Alfred, you need some of this extra complexity. The workflow also takes into account if you use zsh instead of bash, which matters if you’re used to the small differences between them. It also matters to read your shell’s startup files (which Alfred does not do on its own), which will load your specific configurations, like aliases and your PATH, which in turn allows you to run tools you have installed yourself that might reside in specific locations (like the ones installed via homebrew).

Your example works only on extremely basic examples that have, for the most part, no practical use. Most of the time you need to do something custom, something that works like your shell, the way you have it configured, does. A simple /bin/bash Run Script block gives you nothing more than what the terminal does as soon as you install the system; RunCommand gives you your environment, the way you’ve set it up, and the possibility to act directly on selected items.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...