Jump to content

Option to automatically close terminal after running command from Alfred


Recommended Posts

You can do it by changing Application to Custom in Alfred Preferences → Features → Terminal and setting the code to something like

on alfred_script(q)
  tell application "Terminal"
    activate
    do script q
    quit
  end tell
end alfred_script

That’s a start but isn’t it, as that will try to close it too soon.

 

For running one-offs, I recommend RunCommand instead (disclaimer: I’m the author). It will run the commands without opening a Terminal.

Edited by vitor
Link to post
Share on other sites
14 hours ago, deanishe said:

It might be better to automatically append ; exit to the command if you just want to close the tab/window instead of quitting Terminal.app.

 

We should also be able to tell AppleScript to close the tab (I think Terminal’s AppleScript Dictionary supports that). exit requires changing the terminal profile configuration, since by default it doesn’t close the tab on Apple’s Terminal app.

Link to post
Share on other sites
24 minutes ago, vitor said:

exit requires changing the terminal profile configuration, since by default it doesn’t close the tab on Apple’s Terminal app.

 

Oh yeah. I forgot about that particular stupidity of Terminal.app (I use iTerm). I'm not sure how simple it is to figure out when the command has finished running via AppleScript, though.

Edited by deanishe
Link to post
Share on other sites
11 minutes ago, deanishe said:

I'm not sure how simple it is to figure out when the command has finished running via AppleScript, though.

 

Me neither. Maybe we could combine both solutions and append ; osascript -e 'tell application "Terminal" to quit' to the command.

Link to post
Share on other sites

Mmm. I think it's a bad idea to quit the application. There might be other stuff running in other tabs/windows.

 

Is configuring Terminal.app to close the tab when the last program exits not the correct thing to do in any case? It's default behaviour is so annoying and weird.

Link to post
Share on other sites
34 minutes ago, deanishe said:

I think it's a bad idea to quit the application. There might be other stuff running in other tabs/windows.

 

Agreed. I thought @Hattrick wanted the app closed, but I stand corrected; the request was to close the window.

 

38 minutes ago, deanishe said:

Is configuring Terminal.app to close the tab when the last program exits not the correct thing to do in any case? It's default behaviour is so annoying and weird.

 

I’d say so. I was also never able to understand why they chose for it to behave like that.

Link to post
Share on other sites
  • 4 months later...
  • 5 months later...
2 hours ago, Ilia said:

Besides, would that be possible not to raise Terminal window at all and run terminal command in the background?

 

Probably, but that would be mostly pointless: if you don't need to interact with the command or see its output, why run it in a terminal? Use a Run Script instead.

Link to post
Share on other sites
15 hours ago, deanishe said:

why run it in a terminal? Use a Run Script instead.

 

Well, my script produces no result if run using Run Script instead.

 

Could that be because the commands on the script are run as:

 

 

/usr/bin/nohup /usr/local/bin/sshfs ... 2>/dev/null &

 

 

Edited by Ilia
Link to post
Share on other sites
17 hours ago, Ilia said:

Could that be because the commands on the script are run as

 

It's more likely that whatever it is you're doing requires something from your shell environment.

 

Why don't you try running the command normally, instead of discarding its output, so you can see what's going on?

Link to post
Share on other sites
34 minutes ago, deanishe said:

Why don't you try running the command normally, instead of discarding its output, so you can see what's going on?

 

It seems that you are right. When running in terminal, .bashrc is run, which sets PATH with GNU utils, rather than BSD, but in Run Script mode, it's not set and my script is using default BSD sed which fails. I know I can run  .bashrc from inside the script to address this issue, however it would be preferable to workaround this issue somehow from inside Alfred? Is this possible? 

Link to post
Share on other sites
23 minutes ago, Ilia said:

Would that be possible to add a tick to Run Scripts mode, to include .bashrc


You’d need a checkbox for every startup file of every shell, and there are a bunch of those.
 

1 hour ago, Ilia said:

I know I can run  .bashrc from inside the script to address this issue, however it would be preferable to workaround this issue somehow from inside Alfred? Is this possible?


Source the .bashrc inside the Run Script, then.

Link to post
Share on other sites
  • 3 months later...

Hi, I can't get the terminal tabs to close, neither Terminal.app nor iTerm2. I have set the Shell in both to close after finishing. I have also tried to add the  ; exit  command to my script, but the tabs still keep open after finishing. Any other tips? I'm on Big Sur. 

 

I do want to see the terminal while running (transcoding video), so Run Script isn't an option for me. I just want it to close after completion. Thanks!

Link to post
Share on other sites

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