iterm2 after upgrade does not open up profiles - macos

I want to be able to type at command prompt
it2 myMachine and have a new session pop up with defaults for that profile in Iterm2.
The following used to work until upgrades, where $1 is the profile name...
osascript <<ENDSCRIPT
on run argv
tell application "iTerm"
activate
tell the first terminal
launch session "$1"
end tell
end tell
end run
ENDSCRIPT
71:79: syntax error: Expected class name but found identifier. (-2741)
I also am looking at straight osascript in bash. But new tab does not get command.
osascript -e "tell application \"Terminal\""
-e "tell application \"System Events\" to keystroke \"t\" using {command down}"
-e "do script \"cd $pwd; clear\" in front window"
-e "end tell"

I found all on iterm2 help pages... tell application "iTerm2" tell current window create tab with default profile end tell end tell There are many standard Applescript functions (e.g., to get the window's size and position) that are not documented here. create tab with default profile create tab with profile "name"

Related

How to open multiple panes on iTerm?

I have created aliases of applescripts to open multiple panes on iTerm. However, ever since the latest update the scripts stopped working. I keep getting this error:
syntax error: Expected end of line but found identifier. (-2741)
Here's the script:
newPaneDown() {
osascript -e "
tell application \"iTerm\"
make new terminal
tell the current terminal
activate current session
tell the last session
tell i term application \"System Events\" to key code 2 using {shift down, command down}
end tell
end tell
end tell"
}
newPaneLeft() {
osascript -e "
tell application \"iTerm\"
make new terminal
tell the current terminal
activate current session
tell the last session
tell i term application \"System Events\" to key code 2 using command down
end tell
end tell
end tell"
}
newPanes4x4() {
/usr/bin/env osascript <<-EOF
tell application "iTerm"
activate
launch session "Panes"
tell i term application "System Events" to keystroke "d" using command down
tell i term application "System Events" to keystroke "D" using command down
tell i term application "System Events" to keystroke "[" using command down
tell i term application "System Events" to keystroke "[" using command down
tell i term application "System Events" to keystroke "D" using command down
end tell
EOF
}
alias p2='newPaneLeft'
alias p3='newPaneDown && newPaneLeft'
alias p4='newPanes4x4'
Applescript is not backwards compatible since iTerm2 Version 3.
The new Applescript syntax is described here.
You should replace:
make new terminal
with
create window with default profile

Applescript–execute multi line code

I have some apple script code:
tell application "System Events"
key code 97
end tell
How do i write the code as a osascript -e command in Terminal?
Everytime I try using \n or the such, I get errors. Sorry if I'm not being specific enough.
You have a couple of options:
Pass each line of the AppleScript code as a separate -e option:
osascript -e 'tell application "System Events"' -e 'key code 97' -e 'end tell'
Pipe the AppleScript code to osascript's STDIN:
osascript <<END
tell application "System Events"
key code 97
end tell
END
Oh, and you can also save AppleScript code as an executable shell script. Just add #!/usr/bin/osascript at the top of the code and save it as a plain text file:
#!/usr/bin/osascript
tell application "System Events"
key code 97
end tell
Other example:
open -a Terminal && \
sleep 2 && \
osascript -e 'activate application "Terminal"' -e 'tell application "System Events" to keystroke "q" using command down'
the first two lines are just to show the final goal, which is focus the Terminal window and quit it, sending Command+q
Actually -e option accepts new lines:
osascript -e '
tell application "System Events"
key code 97
end tell'

How to close a Terminal tab using AppleScript?

I'm using AppleScript to open PostgreSQL in a Terminal tab like this:
#!/bin/bash
function new_tab() {
TAB_NAME=$1
COMMAND=$2
osascript \
-e "tell application \"Terminal\"" \
-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"printf '\\\e]1;$TAB_NAME\\\a'; $COMMAND\" in front window" \
-e "end tell" > /dev/null
}
new_tab "PostgreSQL" "postgres -D /usr/local/var/postgres"
Running this script from the Terminal will open a new tab with PostgreSQL server inside. So at the end of the execution I'll have 2 tabs: the first one which was used to run the script, and the second one containing the server.
How can I close the first one?
This is my try:
osascript -e "tell application \"Terminal\" to close tab 1 of window 1"
But I get this error message:
execution error: Terminal got an error: tab 1 of window 1 doesn’t
understand the “close” message. (-1708)
You can try something like this:
tell application "Terminal"
activate
tell window 1
set selected tab to tab 1
my closeTabOne()
end tell
end tell
on closeTabOne()
activate application "Terminal"
delay 0.5
tell application "System Events"
tell process "Terminal"
keystroke "w" using {command down}
end tell
end tell
end closeTabOne
One way to do it is like this:
osascript \
-e "tell application \"Terminal\"" \
-e "do script \"exit\" in tab 1 of front window" \
-e "end tell" > /dev/null
But Terminal must be configured to close the window when the shell exits.
Anyone has a solution which does not need to do this?
This will close the active tab only:
tell application "Terminal" to close (get window 1)
To determine the tab’s window, you can parse the error message you get when trying to access the still non-existing window property of the tab. The error message usually contains the window's id with which you can reference the window.
As your question is 5 years old, I’ll finish my answer with example code that can be run in the Script Editor instead of a bash script which makes it hard to read.
tell application "Terminal"
-- Perform command
set theTab to do script "echo 'Hello World'"
try
-- Try to get the tab's window (this should fail)
set theWindow to window of theTab
on error eMsg number eNum
if eNum = -1728 then
(*
The error message should look like this:
Terminal got an error: Can’t get window of tab 1 of window id 6270.
*)
-- Specify the expected text that comes before the window id
set windowIdPrefix to "window id "
-- Find the position of the window id
set windowIdPosition to (offset of windowIdPrefix in eMsg) + (length of windowIdPrefix)
-- Get the window id (omitting the period and everything else after)
set windowId to (first word of (text windowIdPosition thru -1 of eMsg)) as integer
-- Store the window object in a variable
set theWindow to window id windowId
else
-- Some other error occurred; raise it
error eMsg number eNum
end if
end try
close theWindow
end tell
I think the key is to get the id of front window.
tell application "Terminal"
do script "pwd"
set myID to id of front window
close window id myID
end tell

Use Applescript to Enter command into Terminal but do not Execute

Using Applescript, is it possible to open a new Terminal and enter the command into the terminal but do not run it?
tell application "Terminal"
do script "echo Hello"
end tell
This code will type the line echo Hello into the Terminal and run it. Can we avoid the execution?
Good case for System Events app and emulating keystrokes:
tell application "Terminal" to activate -- only needed if Terminal may not be running
tell application "System Events"
tell application process "Terminal"
set frontmost to true
keystroke "echo Hello"
end tell
end tell

applescript to osascript conversion?

I am using max msp to run shell commands, I have been prototyping code in applescript and need them to run in osascript for example -
tell application "Google Chrome" to close tab 1 of window 1
converts to
osascript -e 'tell application \"Google Chrome\" to close tab 1 of window 1'
I have converted around 10 commands, but am stuck on the very last one which is
tell application "Google Chrome" to activate
tell application "System Events"
tell process "Google Chrome"
do shell script "/usr/local/bin/cliclick/ c:360,550"
end tell
end tell
which I think goes to
osascript -e 'tell application \"Google Chrome\" to activate' -e 'tell application \"System Events\" to tell process \"Google Chrome\" to do shell script \"/usr/local/bin/cliclick c:360, 550\"'
cliclick lets you use the mouse through shell. http://www.bluem.net/en/mac/cliclick/. The c is the command identifier for clicking, so at x360 y550
is my syntax correct? it works when I dont include the c identifier.
Thanks
I tried this and it gave me this error message:
99:151: execution error: System Events got an error: Invalid argument “360,” to command “c”: Expected two coordinates, separated by a comma. Example: “c:123,456” (1)
Solution: you have an extra space before the number 550 (c:360, 550) and the second value gets lost. Remove the space character and it should work (c:360,550)…
Here is a version where I also changed the quoting:
osascript -e "tell application \"Safari\" to activate" -e "tell application \"System Events\" to tell process \"Safari\" to do shell script \"/usr/local/bin/cliclick c:360,550\""

Resources