Jump to content
Sign in to follow this  
untidey

[Solved] Terminal command workflow

Recommended Posts

Hopefully there are some Bash scripting gurus out there who might be able to help. I'm trying to create a "Terminal Command" workflow that does the same as Alfred's built-in terminal shortcut without having to open an actual Terminal window. I want it to have access to my usual interactive, login shell environment. (Startup files: ~/.bash_profile, ~/.bash_aliases) 

 

I've linked a 'run script' action with Bash as the interpreter.

 

I tried a basic script '{query}' which works but has no environment loaded.

I tried: 'bash -l -c "{query}"' which, for reasons I can't fathom, doesn't load my environment in Alfred but does on the command line.

I tried:

 

export BASH_ENV='/Users/Nick/.bash_profile'

bash -l -c "{query}"

 

and seem to have created a self-replicating monster that is screwing up the Terminal and my system. >.<

 

Any advice much appreciated! Thanks.

Share this post


Link to post

This is working intermittently for me:

 

source /Users/danielsh/.bash_profile

result=$(eval '{query}' 2>&1)
echo $result

But only intermittently. Functions that I've defined seem to work, but aliases do not, which seems strange. So for example, my .bash_profile contains this, which works:

# Uninstall an app with CleanMyMac
clean () { open -a "CleanMyMac" /Applications/"${1}".app; }

 

...and this, which returns "command not found":

alias myip="dig +short myip.opendns.com @resolver1.opendns.com"

 

 

But maybe that'll give you something to go on.

Share this post


Link to post

Cool thanks for the input. I found the cause of our issues. Non-interactive shells don't expand aliases by default. I'm now using the following:


shopt -s expand_aliases
source /Users/Nick/.bash_profile
{query} 2>&1 &

I needed to append the ampersand to the final line to get Sublime Text 2 to play nicely when called from the workflow though I don't understand why.

Share this post


Link to post
Cool thanks for the input. I found the cause of our issues. Non-interactive shells don't expand aliases by default. I'm now using the following:

 

shopt -s expand_aliases
source /Users/Nick/.bash_profile
{query} 2>&1 &

I needed to append the ampersand to the final line to get Sublime Text 2 to play nicely when called from the workflow though I don't understand why.

 

That's great, thanks! I've made a slight change that doesn't require the user to manually enter his or her own home path and updated the "Background Command" script I'd placed in AlfPT (which was just `eval "{query}"` before seeing this). Here's the script it's using now:

shopt -s expand_aliases

if [ -f "${HOME}/.bash_profile" ] ; then
	source "${HOME}/.bash_profile"
fi

{query} 2>&1 &

 

This, incidentally, seems to work fine with the `subl` command, though `subl` is known for occasionally not working. Here's a link to my version.

 

Edit: Whoops, that was a foolishness; I'd had `subl` aliased in my profile so that it opened a new window, and that was what solved the problem! You're right about your syntax working better than `eval`, so I changed it again and re-uploaded. It also has the benefit of not spitting out a Notification if the command has no output now. Cool!

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.

Sign in to follow this  
×
×
  • Create New...