Jump to content
vitor

CommandRun — Run terminal commands without opening one, on the current Finder window

Recommended Posts

You may find this useful. If you set your terminal application to 'Custom' in Alfred's settings, then you can do something like the below. It runs the shell command in the current terminal without starting up a new terminal window. I use iTerm2, if you're using Terminal.app the AppleScript will be slightly different.

 

on alfred_script(q)
	tell application "iTerm"
		activate
		
		try -- We don't need error messages
			set miniaturized of windows to false
		end try
		
		try
			set _session to current session of current terminal
		on error
			set _term to (make new terminal)
			tell _term
				launch session "Default"
				set _session to current session
			end tell
		end try
		
		tell _session
			write text q
		end tell
	end tell
	tell application "System Events"
		set visible of process "iTerm" to true
	end tell
end alfred_script

Share this post


Link to post

You may find this useful. If you set your terminal application to 'Custom' in Alfred's settings, then you can do something like the below. It runs the shell command in the current terminal without starting up a new terminal window. I use iTerm2, if you're using Terminal.app the AppleScript will be slightly different.

 

I’ve seen that code of yours before (in fact, I’d like to be able to do a variation on it), but it still has a fundamental difference — it needs the terminal, which means it’ll still make you wait, if you do not have it open.

Even though I run a lot of commands, I do not like to keep the terminal open at all times. To me, if it’s open it means it’s doing something, which is one of the clues I use while I work. This workflow scratches a specific itch, when I want to run quick commands, but waiting for the terminal to start is annoying.

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 PATHalias, and what else you might have there).

Share this post


Link to post

Update.

 

It now uses the frontmost Finder window as the starting point of the command (if none is open, it defaults to your home directory).

Share this post


Link to post

Update.

 

It now uses a more “natural” directory detection. Commands start in the frontmost Finder window, and if you have none selected or open, they’ll start on the Desktop.

Share this post


Link to post

Great workflow. Would be very useful to me. But unfortunately I can't seem to get this to work on OSX Mavericks. Anything idea?

 

One of the biggest challenges to make this workflow and its “sibling” work is that Alfred does not read your PATH and other things that your terminal does when starting. Different users also have different shells and configurations, which makes it harder to set this up in a way that works for everybody. This last update addresses those issues, in some form.

 

Can you please tell me what is your default shell (bash, zsh, other), where it’s located (are you using /usr/bin/zsh/, or /usr/local/bin/zsh, for example), what command you’re trying to run that fails, and how did you install it (is it a system command, or did you get it from homebrew or some other means)? Basically, the more details, the better.

Share this post


Link to post

 

One of the biggest challenges to make this workflow and its “sibling” work is that Alfred does not read your PATH and other things that your terminal does when starting. Different users also have different shells and configurations, which makes it harder to set this up in a way that works for everybody. This last update addresses those issues, in some form.

 

Can you please tell me what is your default shell (bash, zsh, other), where it’s located (are you using /usr/bin/zsh/, or /usr/local/bin/zsh, for example), what command you’re trying to run that fails, and how did you install it (is it a system command, or did you get it from homebrew or some other means)? Basically, the more details, the better.

I'm running Mavericks, latest DP (legit I'd like to add). 

 

Shell:

/bin/bash (out-of-the-box)

GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
Copyright © 2007 Free Software Foundation, Inc.
 
Finder:
select my homefolder
 
Command (nothing works): 
touch HelloWorld.txt
/usr/bin/touch HelloWorld.txt
 
I do have HomeBrew installed, but I assume that's not relevant for the command above.
 
Your help is greatly appreciated!

Share this post


Link to post

That really is weird, being touch such a basic command. Could you please redownload and install the workflow, just so we’re sure it’s really the latest version? There were some recently that kind of broke some things.

Share this post


Link to post

I've just installed a newly downloaded copy. That didn't help. I'll add debugging to the workflow scripts to hopefully see what happens. 

Share this post


Link to post

Your script creates a command file in the workflow folder. The issue on my machine is that my shell can't execute that file using ./command. I've changed the script so it creates the command file in /tmp. That did the trick. 

Share this post


Link to post

Odd. I have no idea why that happens. Thank you for figuring it out, though, I’ve update the workflow to reflect that change.

Share this post


Link to post

bash was still not sourcing ~/.bash_profile so no alias's. the fix for me  was changing the tmp/commandRun file creation echo to 

echo "shopt -s expand_aliases
source ~/.bash_profile
cd ${dirPath}
{query}" > /tmp/commandrun 
 
 

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, following lordheart’s report. If you use this, please download the other workflow instead, as it does the same (but improved) and more.

Edited by Vítor

Share this post


Link to post

I’ve readded support for tcsh and csh; check RunCommand for those. I’ve removed them at the time for a few reasons, but your comment made me revisit it and make them work again.

Edited by Vítor

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