Applescript (osascript): opening split panes in iTerm 2 and performing commands

Joel picture Joel · Mar 12, 2014 · Viewed 16.1k times · Source

The following works to open two tabs in iTerm 2.

I can't seem to figure out how to get this to using split panes instead.

I've tried applying what I see on several forums, but it never works. Could 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

Answer

Dom picture Dom · Feb 14, 2015

With the new nightly build it is quite nice. It seems to be missing in 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 search way too long for this, so maybe I can help somebody out with this (probably myself in a couple of weeks), because this was one of the first things I found. This solution even works with activated focus-follows-mouse, which is missing in the other answers.