OSX Terminal close current tab from command line without prompt? - macos

Looking for a way to close the current tab via the command line, I hashed this out, but end up getting a prompt for Do you really want to close which I would like to avoid. Here's my code,
osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "w" using command down'

Why didn't use the:
Terminal.app -> Preferences -> Settings -> Shell
and for the items:
"When the shell exists:" Close if the shell exited cleanly
"Prompt before closing:" Only if there are...." (or Never)

Related

How can I open a new z shell in a tab and run a command?

I'm on macOS which is currently using the zshell.
I would like to run a command in one shell which opens another shell preferably in another tab in the same window and then runs a given command. For example:
> openTab
would open another tab and run a basic command like ls.
Is this possible to do?
It appears that the open command will open new window, but I want it to be opened as a new tab in the current window. See here
Osascript appears a bit messy, is there way to do this natively with zshell?
You can use AppleScript, via osascript to do exactly that:
function runInNewTab() {
osascript >/dev/null <<EOF
tell application "System Events"
tell process "Terminal" to keystroke "t" using command down
end tell
tell application "Terminal" to do script "$*" in front window
EOF
}
Run it like this:
runInNewTab ls -l ~

Add mark to Terminal in background using AppleScript

The macOS Terminal has a nice "mark" feature that allows you to jump between prompts using Cmd+Up/Down. I'm trying to insert my own "marks" from inside of a Python script so that I can jump to specific parts of the output (example).
Thanks to Armin Briegel, I have:
osascript -e 'tell app "System Events" to keystroke "u" using command down'
This works, but has a few problems. It doesn't add a "mark" if the Terminal is not in focus. Also, it triggers the Terminal bell alert if the Terminal is not in focus. Any way to improve this?
Use this to bring Terminal into focus first.
osascript -e 'tell application "System Events" to tell its application process "Terminal" to set frontmost to true'

Trying to automate tabs and shell commands in Terminal?

I have a custom vim setup running inside split (GNU) screen sessions running in several tabs inside Terminal. Naturally I want to automate all that. So Ive Googled a lot and most answers talk about using osascript -e to run a bunch of AppleScript commands. My situation is slightly different: first Im using TotalTerminal, a plugin for Terminal (dont think it matters but mention it just in case) and Im writing a hashbang script and not a bash script, i.e.
#!/usr/bin/osascript
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
tell application "Terminal" to activate
tell application "Terminal" to do script with command "cd ~/Desktop/Projects && screen -d -U -R -A"
which Im running from the command-line. The tab opening works but the script/command runs in a new window instead of inside a newly-created tab.
This is how I might recommend setting things up:
#!/usr/bin/osascript
tell application "System Events"
tell process "Terminal"
set frontmost to true
end tell
end tell
tell application "Terminal"
activate
tell application "System Events" to keystroke "t" using command down
do script "cd ~/Desktop/Projects && screen -d -U -R -A" in window frontmost
do script "clear; echo 'Hello, World!'" in tab 1 of window frontmost
end tell
Note: You also can select the tab you want the next command to go into by using tab x. If you switch back to the first tab you should notice the echo sent to it after creating the new tab.
The example above is a few more lines of code perhaps, although it should get all the processes correctly in order. I think the key ingredient is having Terminal set frontmost to true which gets the current Terminal window to start interacting with the rest of the script.
EDIT: The OP came back and needed to make a few changes and this was the end result:
#!/usr/bin/osascript
tell application "System Events"
tell process "Terminal"
set frontmost to true
end tell
end tell
tell application "Terminal"
activate
do script "mosh user#someserver" in window frontmost
tell application "System Events" to keystroke "t" using command down
do script "cd ~/Desktop/Projects && screen -d -U -R -A" in tab 2 of window frontmost
end tell

Applescript to open Terminal window without sourcing ~/.bash_profile

I am trying to use Platypus to create an app launcher for an interactive command-line program on OSX 10.8. I want to be able to double-click on my application, and have a Terminal window open, running my program. The problem is that my Applescript, (borrowed from Octave, and adapted for Julia) launches a Terminal window and attempts to spit some commands into it, however I have a rather hefty ~/.bash_profile that interferes with this. Is there a way to get my Applescript to open a non-login shell, or not source ~/.bash_profile, etc?
Here's the script that Platypus runs:
# This is the startup procedure written as AppleScript to open a
# Terminal.app (if the Terminal.app is not already running) and start
# the Julia program.
# 20071007 removed: open -a /Applications/Utilities/Terminal.app
osascript 2>&1>/dev/null <<EOF
tell application "System Events" to set ProcessList to get name of every process
tell application "Terminal"
activate
if (ProcessList contains "Terminal") or ((count of every window) is less than 1) then
tell application "System Events" to tell process "Terminal" to keystroke "n" using command down
end if
do script ("exec bash -c \"PATH=${ROOT}/julia/bin:${PATH} OPENBLAS_NUM_THREADS=1 FONTCONFIG_PATH=${ROOT}/julia/etc/fonts GIT_EXEC_PATH=${ROOT}/julia/libexec/git-core GIT_TEMPLATE_DIR=${ROOT}/julia/share/git-core exec '${ROOT}/julia/bin/julia'\"") in front window
end tell
EOF
# Quit the Julia.application immediately after startup (ie. quitting
# it in the taskbar) because once it is started it cannot be restarted
# a second time. If Julia.app stays (eg. because of a crash) opened
# then restarting is not possible.
osascript 2>&1>/dev/null <<EOF
tell application "julia"
quit
end tell
EOF
In general, you don't need a Terminal window to execute command line stuff. You would only use the Terminal if there was information you need to manually type in by hand. So you can probably just run the command using "do shell script" instead of "do script" in a Terminal window. Note that doing it this way won't use your bash profile file. So try this command all by itself in the applescript...
do shell script ("exec bash -c \"PATH=${ROOT}/julia/bin:${PATH} OPENBLAS_NUM_THREADS=1 FONTCONFIG_PATH=${ROOT}/julia/etc/fonts GIT_EXEC_PATH=${ROOT}/julia/libexec/git-core GIT_TEMPLATE_DIR=${ROOT}/julia/share/git-core exec '${ROOT}/julia/bin/julia'\"")
Then you can add your other applescript commands as needed, just don't use the Terminal and thus your bash profile won't be used.

Applescript: Keeping track of multiple terminal windows and writing into each of them

I've got a setup procedure for a project that involves me using multiple terminal windows. The start up procedure is sort of messy and involves me tabbing between the terminal windows (different tools running) and sequentially inputting commands into each terminal.
Applescript is useful for getting the first run of commands out and opening all my terminals with the:
do script "echo blablablabla"
These are decent because they open new terminal windows each time I "do script". This is actually more beneficial to me than tabbing (although, I couldn't quite figure out tabbing between terminal tabs).
However, I'd like to keep track of these windows since I need to go back to specific ones and input more commands. Any ideas?
More specifically:
Is there a way I can add an alias for each window to track it and tab back to it in the Applescript? If so, how do I implement it?
do script has an in specifier:
tell application "Terminal"
set t to do script "echo a"
do script "echo b"
do script "echo c" in t
set index of window 2 to 1
end tell
Or using tabs:
tell application "Terminal"
activate
set t to do script "echo a"
tell application "System Events" to keystroke "t" using command down
do script "echo b" in window 1
set selected tab of window 1 to t
end tell

Resources