Applescript (osascript): opening split panels in iTerm 2 and executing commands

The following works to open two tabs in iTerm 2 .

I cannot figure out how to do this using split panels.

I tried to apply what I see in several forums, but it never works. Can someone point me in the right direction?

osascript <<-eof tell application "iterm" set myterm to (make new terminal) tell myterm launch session "Default session" tell the last session set name to "Server" write text "cd $projectsFolder" end tell launch session "Default session" tell the last session set name to "Console" write text "cd $projectsFolder" end tell end tell end tell eof 
+6
source share
6 answers

Good, so I finally figured it out.

By sending keystrokes to the application, you can open and navigate through the split panel.

 tell i term application "System Events" to keystroke "D" using command down tell i term application "System Events" to keystroke "]" using command down 

An example of sending commands to separate a panel and assign each panel. I use this to run the node application.

 write text "cd $projectsFolder/$2.m" write text "/usr/local/bin/frontend.sh $1 $2" tell i term application "System Events" to keystroke "D" using command down tell i term application "System Events" to keystroke "]" using command down set name to "$2.api" write text "cd $projectsFolder/$2.api" write text "/usr/local/bin/backend.sh $1 $2" 
0
source

With the new nightly build it's pretty nice. It seems to be missing from the public version, although it was implemented about a year ago: Source - AppleScriptTest.m

 tell application "iTerm" activate select first terminal window # Create new tab tell current window create tab with default profile end tell # Split pane tell current session of current window split vertically with default profile split vertically with default profile end tell # Exec commands tell first session of current tab of current window write text "cd ~/Developer/master-node" write text "coffee" end tell tell second session of current tab of current window write text "gulp w" end tell tell third session of current tab of current window end tell end tell 

I had to look for this for too long, so maybe I can help someone with this (maybe in a couple of weeks myself), because this was one of the first things I found. This solution even works with the activated focus-follow-mouse button, which is missing in other answers.

+14
source

As House noted, in the new beta versions of 2.9, the other answers will no longer work. I was upset that I couldn’t automate this, so I wrote a command line batch file compatible with teamocil that does just that:

https://github.com/TomAnthony/itermocil

It allows you to write YAML files for pre-configured sets of windows and panels that can run predefined command commands for a given project.

+6
source

Update : iTerm2 v3 has significantly improved, but incompatible support for AppleScript - see https://www.iterm2.com/documentation-scripting.html

To provide some background @Joel's own answer:

iTerm 2 AppleScript Support (with iTerm 2 v1.0.0.201306220):

  • incomplete : script support for split panels is missing, so the OP resorts to a suboptimal method for sending keystrokes.

  • demonstrates some strange behavior : when compiling (in the AppleScript editor) the tell "System Events" ... instruction tell "System Events" ... inside the tell application "iTerm" prefix i term application inexplicably inserted before the "System Events" - since the code below is not precompiled, this prefix is ​​not included to avoid future problems.

  • it has bugs / are incompatible with its dictionary : what the dictionary describes as an exec command, which actually does not work, is actually executed by the write text command: it executes the passed argument or - if the argument has a finite space - it simply β€œtypes” the argument, does not sending it.

Here's a solution with broken panels based on a workaround (sending keystrokes) - a bash script calls AppleScript code through osascript :

Limitations due to the fact that panels are not part of the dictionary:

  • another open panel cannot be named
  • no command can be sent to another panel
 #!/usr/bin/env bash projectFolder="$HOME/Desktop" # example osascript <<-EOF tell application "iTerm" tell (make new terminal) # Create a new pseudo terminal... tell (launch session "Default session") # ... and open a session (window) # Name the new window (its original pane). set name to "Server" # Execute the 'cd' command in the original pane. write text "cd '$projectFolder'" # Create a new split pane, horizontally, by sending βŒ˜β‡§-D tell application "System Events" to keystroke "d" using {shift down, command down} # !! Note: We canNOT: # - name this pane separately # - execute a command in it. # Return to the original pane, by sending ⌘-[ tell application "System Events" to keystroke "[" using {command down} end tell end tell end tell EOF 
+5
source

In case this is useful: I have a similar problem related to the fact that iTerm requires a command combination of shortcuts to separate the panels and so that the new panel inherits the name of the original session. I came up with the following that solves this problem and relies less on sending keystrokes (although I would like to completely eliminate them).

 tell application "iTerm" tell the current terminal tell the current session set the_name to get name tell i term application "System Events" to keystroke "d" using {command down, shift down} end tell tell the current session set name to the_name end tell end tell end tell 

I use BetterTouchTool to bind a key combo - namely cmd+' - to the execution of this AppleScript. (I find that for some key combos it gets crazy, I would naively guess, because you effectively hold this key combination on top of all the keystrokes of the script. I did not pursue how to define the key combination in iTerm's preferences. I suspect that this may alleviate the problem.)

+1
source

starting with @ mklement0, this is my script that opens a new tab, splits into 4 panels and runs the commands:

 #!/usr/bin/env bash osascript <<-EOF set cmds to {"rabbitmq-server", "mongod", "redis-server", "htop"} tell application "iTerm" activate set myterm to (current terminal) tell myterm launch session "Default Session" # split vertically tell application "System Events" to keystroke "d" using command down delay 1 # previus panel tell application "System Events" to keystroke "[" using command down delay 1 # split horizontally tell application "System Events" to keystroke "d" using {shift down, command down} delay 1 # next panel tell application "System Events" to keystroke "]" using command down delay 1 # split horizontally tell application "System Events" to keystroke "d" using {shift down, command down} set n to count of cmds repeat with i from 1 to n # next panel tell application "System Events" to keystroke "]" using command down delay 1 tell the current session to write text (item i of cmds) end repeat end tell end tell EOF 
+1
source

All Articles