Jump to content
Hattrick

Option to automatically close terminal after running command from Alfred

Recommended Posts

Most of the time I use the `> code` keyword, I'm using it to run a one-shot terminal command that doesn't need the terminal window open afterwards. It's a little annoying to have to close the window manually each time.

Share this post


Link to post
Share on other sites

  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.

Share this post


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.

Share this post


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

Share this post


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.

Share this post


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.

Share this post


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.

Share this post


Link to post
Share on other sites

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