Trying to automate tabs and shell commands in Terminal? - macos

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

Related

How to execute a command in full screen mac terminal using Apple Script?

I am trying to automate this using apple script. Let me list down the steps below;
Open a new Mac Terminal window
Make it full-screen
echo "hello world" or sh /some/script/file.sh
I am trying the script below;
tell application "Terminal"
do script " "
activate
end tell
tell application "System Events"
keystroke "k" using {command down}
keystroke "f" using {command down, control down}
end tell
Basically, this script will open a new terminal window and make it full-screen. I am stuck at step 3, which is simply echoing some message, or executing a shell script.
Could somebody please explain how to achieve this?
Thank you.

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'

OSX: How can I programatically tell if I'm running in Terminal or iTerm?

I have a command line application running on OSX that would like to create several tabs in the current window using AppleScript.
How can I tell if my program is running in Terminal, iTerm, or another terminal program?
The $TERM_PROGRAM env var is set to iTerm.app or Apple_Terminal so you can determine which AppleScript cmds to run if you pass it as an arg to osascript (assuming your shelling to osascript)
Example usage:
osascript ThisScriptName.scpt $TERM_PROGRAM
Example script:
--osascript ThisScriptName.scpt $TERM_PROGRAM
on run {TermType}
if (TermType = "iTerm.app") then
-- iTerm.app :-P
tell application "iTerm"
tell current window
tell current session
set newSession to (split horizontally with default profile)
end tell
end tell
end tell
else if (TermType = "Apple_Terminal") then
-- Terminal.app
tell application "Terminal"
do script "open 'https://iterm2.com/downloads.html'" in window 1
end tell
else
-- Unknown terminal application
return "Really? Not using iTerm.app or Terminal.app"
end if
end run
osascript -e 'tell application "Finder" to get the name of every process whose visible is true'
This will a list of running applications, assuming your only running one of terminal, iTerm, ..., this will work
Do what you want with the list
Hope this helps

OSX Terminal close current tab from command line without prompt?

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)

Resources