Jump to content

Bash Commands w/o Terminal Window


Recommended Posts

Hey All,

 

Looking for a solution on a very minor annoyance, I usually run a lot of shell commands from Alfred using the > prefix, which is great! Love it! However, when it executes the command it pops open terminal and runs the command and leaves another lingering window around which I find annoying.

 

Does anyone know of a way to force Alfred to close the window it opens after the command is executed?

Very rarely do I need the returned command's output. It's usually for firing off quick things like docker pulls, file moves, git commits and such.

 

If I need to do this through an AppleScript I'm open to that as well, just wanted to find some way around my terminal windows popping up everywhere like mushrooms.

 

Thanks!

Link to comment
1 hour ago, vitor said:

Hello and welcome to the forums, @clevermistakes.

 

RunCommand does what you’re after (disclaimer: I wrote it).


It reads your shell’s configuration (so it gets your aliases, environment, PATH, and the like) and runs the command you passed through Alfred. It does so without needing to open a Terminal.

 

This is perfect! Thanks a ton for writing this! I knew someone had to have the same complaint as me somewhere. I'm glad to know there's plenty of us risky bash users who don't care about what the shell says back to us! haha

Link to comment
4 minutes ago, vitor said:

 

If you ever run a command where you do care about the output, action it with ⌘↩ and the result will be shown in large type on the screen.

 

Have you ever encountered issues where some of us lazy users are creating symlinks in /usr/local/bin rather than a home folder? Tried running an alias I use and got an error. Should I just create a quick profile for it to use and path the location?

 

Link to comment
1 hour ago, clevermistakes said:

Have you ever encountered issues where some of us lazy users are creating symlinks in /usr/local/bin rather than a home folder?

 

That’s what Homebrew does, so it’s pretty common.

 

1 hour ago, clevermistakes said:

Tried running an alias I use and got an error.

 

I’ll need you to be more specific. Will need the exact alias, shell, the command you ran, and output of the debugger. A video screencast would also help.

Link to comment
1 hour ago, vitor said:

 

That’s what Homebrew does, so it’s pretty common.

 

 

I’ll need you to be more specific. Will need the exact alias, shell, the command you ran, and output of the debugger. A video screencast would also help.

 

Sure! Can do. 

 

Alias: subl for this symlink: ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl

 

Command: Opens sublime text or a specific file in sublime, for example: subl /etc/hosts

 

Shell Version: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)

 

Alfred input before RunCommand: >subl (works)

RunCommand input: .subl (fails, error 127 exit status)

Debugger output:

Starting debug for 'RunCommand'

[ERROR: action.script] /tmp/runcommand: line 4: subl: command not found

Link to comment

Why do you have an alias and a symlink? Wouldn’t it suffice to have just the symlink and set your PATH to include /usr/local/bin?


Would you be comfortable sharing your .bash_profile so I can test with it (I’m a zsh user)? If not that’s OK, I can think of something else.


What about other commands? Is subl the only offending or do others fail as well? And if other fail, are they only aliases, or do other things fail as well (like executables in your PATH)?

Link to comment
1 hour ago, vitor said:

Why do you have an alias and a symlink? Wouldn’t it suffice to have just the symlink and set your PATH to include /usr/local/bin?


Would you be comfortable sharing your .bash_profile so I can test with it (I’m a zsh user)? If not that’s OK, I can think of something else.


What about other commands? Is subl the only offending or do others fail as well? And if other fail, are they only aliases, or do other things fail as well (like executables in your PATH)?

 

I actually only have a symlink for sublime text, sorry for the confusion.

 

For this issue, on my work machine, my bash profile is actually blank. I simply use the symlinks for it. Most of my aliases are done on remote machines so those machines have the full alias lists, I don't use many on my local machine, probably terribly inefficient of me.

 

Sublime is the only one I've tried so far and use often on my machine for editing deployment configs.

Link to comment
45 minutes ago, clevermistakes said:

For this issue, on my work machine, my bash profile is actually blank.

 

So to identify the executables in /usr/local/bin, do you have that added to your PATH? Where (which file) and how (the command) are you setting it?


If you run echo $SHELL && echo $PATH both in your terminal and with RunCommand (action it with ⌘↩, so you see the result), what are the results? Are they the same?


All this is important because this Workflow emulates your shell config and then runs the command you give it. Assuming the code in the Workflow is correct (I’m pretty confident it is, since it has gone through multiple iterations and has been working reliably for me for over four years) then you might have a non-standard setup. Either way, I need to figure out what is special about your system so I can fix it, and these questions will help me diagnose it.

Link to comment
2 hours ago, vitor said:

 

So to identify the executables in /usr/local/bin, do you have that added to your PATH? Where (which file) and how (the command) are you setting it?


If you run echo $SHELL && echo $PATH both in your terminal and with RunCommand (action it with ⌘↩, so you see the result), what are the results? Are they the same?


All this is important because this Workflow emulates your shell config and then runs the command you give it. Assuming the code in the Workflow is correct (I’m pretty confident it is, since it has gone through multiple iterations and has been working reliably for me for over four years) then you might have a non-standard setup. Either way, I need to figure out what is special about your system so I can fix it, and these questions will help me diagnose it.

 

Sure no problem, results in /usr/local/bin : subl -> /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl

 

The shell and path should be macOS Sierra's default, don't think our work image changes anything:

/bin/bash

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

 

I'm sure your code is great, it's clearly time tested and approved, I'm as curious about the issue as you are! Other than subl, everything else is working great! So far I love it!

 

I don't think there's a non-standard setup, since we regularly wipe our machines when we travel internationally. So by default macOS Sierra doesn't create a bash profile so there shouldn't be one, I just restored this machine like...a week or two ago? I haven't made any modifications to bash other than the symlink for sublime since I use it all the time.

 

Unfortunately my work machine is kind of boring haha, it's pretty barren short of GitHub and Docker since everything I do for work is otherwise on remote machines.

Edited by clevermistakes
Clarify
Link to comment
12 hours ago, clevermistakes said:

The shell and path should be macOS Sierra's default, don't think our work image changes anything:

/bin/bash

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

 

I see usr/local/bin is included by default in the $PATH. I think it didn’t use to be, and Alfred certainly does not take it into account. I think I know where the problem lies.


This version has the fix. Please try it and tell me if it solves it.

Link to comment
38 minutes ago, vitor said:

 

I see usr/local/bin is included by default in the $PATH. I think it didn’t use to be, and Alfred certainly does not take it into account. I think I know where the problem lies.


This version has the fix. Please try it and tell me if it solves it.

 

Hi Vitor,

 

 It's a different response, that's good news. No error this time, but nothing happens, all I get in the debugger is: [input.keyword] Processing output of 'action.script' with arg 'subl'

Link to comment

If I do it with Atom’s CLI script, it works and I get:

[input.keyword] Processing output of 'action.script' with arg 'atom'
[action.script] Processing output of 'action.script' with arg ''


So I installed Sublime Text and tried it. I also don’t get the second line, which suggests it’s getting stuck. And it is. If you check your running processes you’ll see it is started but doesn’t show itself. So I wonder it maybe subl doesn’t like to be called from inside a script. I try it, and it works. So maybe it doesn’t like being called from the script this workflow creates on the fly? I try it manually, and it works.


At this point, I do the most basic thing I can. I set up a simple Keyword in Alfred connected to a single Run Script that consists of nothing but /usr/local/bin/subl. I try it, and it hangs! So at this point I’m pretty sure there’s something about the Alfred+subl mix that is not working and feel confident in saying it’s not failing due to the Workflow. I can open a bug report but everything else is now out of my control.


But now I also ask, why are you running subl from Alfred? Why not just call Sublime Text itself?

Link to comment
18 hours ago, vitor said:

If I do it with Atom’s CLI script, it works and I get:


[input.keyword] Processing output of 'action.script' with arg 'atom'
[action.script] Processing output of 'action.script' with arg ''

 


So I installed Sublime Text and tried it. I also don’t get the second line, which suggests it’s getting stuck. And it is. If you check your running processes you’ll see it is started but doesn’t show itself. So I wonder it maybe subl doesn’t like to be called from inside a script. I try it, and it works. So maybe it doesn’t like being called from the script this workflow creates on the fly? I try it manually, and it works.


At this point, I do the most basic thing I can. I set up a simple Keyword in Alfred connected to a single Run Script that consists of nothing but /usr/local/bin/subl. I try it, and it hangs! So at this point I’m pretty sure there’s something about the Alfred+subl mix that is not working and feel confident in saying it’s not failing due to the Workflow. I can open a bug report but everything else is now out of my control.


But now I also ask, why are you running subl from Alfred? Why not just call Sublime Text itself?

 

No problem if it's a strange one off, I usually use it to open up specific or hidden files quickly for editing, things like /.ssh/config, key files, scripts or containers I'm working on, etc. Not a big deal to do it from Sublime, just became a habit with the ">" command from Alfred...I can continue to use >subl <whateverfile> and use . for everything else!

 

Thanks for all your help Vitor, and the workflow itself! It's been great so far! 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...