Open Terminal window and pre-populate with command - macos

Is it possible to use the open command to open a Terminal window and pass it an argument to pre-populate the new window with a specific command?

Like this, but using osascript rather than open:
osascript -e 'tell app "Terminal" to do script "ls -l"'
If you want to use a bash variable in the script, do something like this:
BASHVAR="hello"
osascript<<EOF
tell app "Terminal" to do script "echo $BASHVAR"
EOF

Related

How to force close terminal window id using AppleScript?

I am starting a new terminal window from a terminal:
osascript -e 'tell app "Terminal" to do script "myProgram.sh"'
Since the new process is not ending(it is basically a server), I want to be able to stop it at some point using AppleScript.
If I do:
osascript -e 'tell app "Terminal" to close window id {windowId}'
I am getting the alert box "Do you want to terminate running process...?".
Is it possible to force close the window id? If not, how can I confirm this alert box using AppleScript?
You could try this:
do shell script "killall -QUIT Terminal"
I hope this helps you solve your problem 🙂.

How to open and close apps using bash in MacOS

I want to open an app(.app) from a bash shell(.sh using #!/usr/bin/bash) which is located in a folder in /Applications. How do I open it?
If I can open it, can I close it? If yes, how?
Setting your shebang to #!/usr/bin/env bash (which is the preferred way as it's portable), consider the following examples:
Open app:
Utilize open command with the -a option. For instance:
open -a "Safari"
Or, using osascript to execute an AppleScript. For instance:
osascript -e 'tell application "Safari" to activate'
Or a terse equivalent:
osascript -e 'activate app "Safari"'
Close app:
Utilize osascript to execute an AppleScript. For instance:
osascript -e 'tell application "Safari" to quit'
Or a terse equivalent:
osascript -e 'quit app "Safari"'
Note: If bash actually resides in /usr/bin/ on macOS as per your question the above examples will work successfully with the shebang: #!/usr/bin/bash
of course you can do that, please use the terminal command "open".
#!/bin/bash
echo This is a play music script
# play Music from command line in the background
open ~/Downloads/AlanWalker-Faded.m4a &
# wait some seconds
sleep 60
# force quit iTunes
killall iTunes

On osx, how to open new iterm terminal tab from one script then run command in that temrinal window

I am running osx. I have a script which runs the full stack then runs grunt at the end. I want to be able to separate running grunt in a separate terminal tab, i am using iterm.
I have tried something like
open -a Terminal "cd ~/dev/work/poc/user-interface/src/main/webapp; grunt"
any ideas?
This should do it for you (obviously change dir & command):
osascript -e '
tell app "iTerm"
activate
tell the first terminal
launch session "Default Session"
tell the last session
set name to "New Session"
write text "cd /usr/bin; ls"
end tell
end tell
end tell'
It's a slightly modified version of something I wrote to add iTerm support for RStudio.
Like this:
#!/bin/bash
osascript -e '
tell application "Terminal"
do script "date"
activate
end tell'
Replace date with whatever you want it to do.

exit terminal after execution

I have a shell script that runs an applescript. The applescript is executed from a terminal and I want that terminal to close after its done.
In my shell script I execute my applescript with this line. It opens a terminal, runs and then the terminal just sits there even though the applescript is done. I have other terminals open that I use all the time so I don't want to close all of them, just that one.
osascript -e 'Tell application "Terminal" to do script "osascript ~/Scripts/reset_simulator.applescript"'
Thanks
In your shell script, you executing the
osascript -e 'Tell application "Terminal" to do script "osascript ~/Scripts/reset_simulator.applescript"'
what really opens an new Terminal.app window.
Have you some special reason to open an new terminal window? Why just not running the
osascript ~/Scripts/reset_simulator.applescript
from your shell script and in the current window.
But if you want such thing (sounds to me strange) always can use the next:
osascript -e 'Tell application "Terminal" to do script "osascript ~/Scripts/reset_simulator.applescript;exit"'
#note the "exit" here ---------------------------------------------------------------------------------^^^^^
and change the Preferences to "close window" when the shell exits.
Reading your comments above, sounds me than you want run some applescript in the "background".
So you can do the next:
a.) from your shell script
osascript ~/Scripts/reset_simulator.applescript &
2.) if you want still open the another terminal window
osascript -e 'Tell application "Terminal" to do script "osascript ~/Scripts/reset_simulator.applescript&exit"'
#note the & here --------------------------------------------------------------------------------------^
In your Terminal settings, change the setting under "Shell->When the shell exits" to "Close if the shell exited cleanly".

OSX - How to auto Close Terminal window after the "exit" command executed.

When I'm done with Terminal, I want to exit it. Right now, I have three options:
killall Terminal. It will end the process, but rather abruptly. I don't think this is the best idea.
Call exit. I've changed the settings so exit closes Terminal. The app still appears open in the Dock though, and doesn't do what I want it to do.
Right click>Quit. However, this isn't a Terminal command to exit it.
So now, what is the way I should exit and close Terminal? I've heard something about osascript but I'm not too sure. I want to exit and close, so that Terminal is no longer open, both as a window and as a process.
in Terminal.app
Preferences > Profiles > (Select a Profile) > Shell.
on 'When the shell exits' chosen 'Close the window'
How about the good old Command-Q?
Actually, you should set a config on your Terminal, when your Terminal is up press ⌘+, then you will see below screen:
Then press shell tab and you will see below screen:
Now select Close if the shell exited cleanly for When the shell exits.
By the above config each time with exit command the Terminal will close but won't quit.
In the Terminal app, Preference >> Profiles tab.
Select the Shell tab on the right.
You can choose Never Ask before closing to suppress the warning.
You could use AppleScript through the osascript command:
osascript -e 'tell application "Terminal" to quit'
In a terminal window, you can type:
kill -9 $(ps -p $PPID -o ppid=)
This will kill the Terminal application process, which is the parent of the parent of the current process, as seen by the kill command.
To close a Terminal window from within a running script, you need to go up one more level in the process hierarchy like this:
kill -9 $(ps -p $(ps -p $PPID -o ppid=) -o ppid=)
I 've been using ctrl + d. It throws you out into the destination where You've started the sqlite3 command in the first place.
osascript -e "tell application \"System Events\" to keystroke \"w\" using command down"
This simulates a CMD + w keypress.
If you want Terminal to quit completely you can use:
osascript -e "tell application \"System Events\" to keystroke \"q\" using command down"
This doesn't give any errors and makes the Terminal stop cleanly.
You can also use this convoluted command, which does not trigger a warning about terminating its own process:
osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit
This command is quite long, so you could define an alias (such as quit) in your bash profile:
alias quit='osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit'
This would allow you to simply type quit into terminal without having to fiddle with any other settings.
Use the osascript command in your code as icktoofay mentioned: osascript -e 'tell application "Terminal" to quit'
Then, open Terminal preferences, go to Settings > Shell, and set "Prompt before closing:" to "Never." Terminal should now quit completely (not remain open in your dock) and ignore the prompt before quitting. If you have only one Terminal window open and the osascript command is your last line of code, it should wait for whatever command you ran before to finish.
This would not be ideal if you are running scripts in the same window or other windows in the background (for instance, you may run a command in the background and continue using the current window for other commands if the first command is followed by an ampersand); be careful!
If you wrap the osascript code in a shell script file, you can probably call it with whatever pithy file-name you give it---as long as it is in Terminal's search path (run echo $PATH to see where Terminal looks for scripts).
I've been using
quit -n terminal
at the end of my scripts. You have to have the terminal set to never prompt in preferences
So Terminal > Preferences > Settings > Shell
When the shell exits
Close the window
Prompt before closing
Never
Create a script:
cat ~/exit.scpt
like this:
Note: If there is only one window, just quit the application, else simulate command + w to close the tab)
tell application "Terminal"
set WindowNum to get window count
if WindowNum = 1 then
quit
else
tell application "System Events" to keystroke "w" using command down
end if
end tell
Then add a alias in your *shrc
just like vi ~/.bashrc or zshrc (anything else?)
add it:
alias exit="osascript ~/exit.scpt"
And source the ~/.bashrc or reopen your terminal.app
This is what I did for a command I just wrote. I wanted to be able to create a "shortcut" to the Backup directory for Apple Configurator that worked on different computers, but since it's relative to the user's home directory, I needed to make it a .command file. Here are the contents:
#!/usr/bin/env bash
open "${HOME}/Library/Application Support/MobileSync/Backup"
(sleep 0.1 ; osascript -e 'tell application "Terminal" to quit') &
I tried several variations of the answers here. No matter what I try, I can always find a use case where the user is prompted to close Terminal.
Since my script is a simple (drutil -drive 2 tray open -- to open a specific DVD drive), the user does not need to see the Terminal window while the script runs.
My solution was to turn the script into an app, which runs the script without displaying a Terminal window. The added benefit is that any terminal windows that are already open stay open, and if none are open, then Terminal doesn't stay resident after the script ends. It doesn't seem to launch Terminal at all to run the bash script.
I followed these instructions to turn my script into an app: https://superuser.com/a/1354541/162011
If this is a Mac you type 'exit' then press return.

Resources