Skip to content

Instantly share code, notes, and snippets.

@mrgnw
Last active May 28, 2021 19:00
Show Gist options
  • Save mrgnw/dfcb8ec1d650f4a60be79718a1dbefbf to your computer and use it in GitHub Desktop.
Save mrgnw/dfcb8ec1d650f4a60be79718a1dbefbf to your computer and use it in GitHub Desktop.
iTerm2 scripts #config #macOS

iTerm2 Scripts

Docs

iTerm loads .scpt AppleScript files from:

~/Library/Application Support/iTerm2/Scripts

Autolaunch

~/Library/Application Support/iTerm2/Scripts/AutoLaunch.scpt

You don't have to restart iTerm to use updated scripts.

Using AppleScript

AppleScript language guide

Whitespace: Use ¬, the magical line continuation symbol. (+L)

Escape characters with a backslash \"

Concatenate strings with &: "a" & " b"

NewWindow.scpt

tell application "iTerm2"
    set newWindow to (create window with default profile)
    tell current session of newWindow
        write text "echo it works!"
    end tell
end tell

CurrentWindow.scpt

tell application "iTerm2"
    tell current session of current window
        write text "echo it works!"
    end tell
end tell

splitSplitSPLIT.scpt

tell application "iTerm2"
    set newWindow to (create window with default profile)
    tell current session of newWindow
        write text "echo it works!"
        repeat 12 times  -- 12 splits = 13 total
	        split horizontally with default profile
        end repeat
    end tell
end tell

CommandsInSeparateTabs.scpt

tell application "iTerm2"
    set newWindow to (create window with default profile)
    tell current session of newWindow
        write text "echo 'All wings report in'"
        set red1 to (split horizontally with default profile)
        tell red1 
            write text "echo 'red1 reporting in'"
        end tell 
        set red2 to (split horizontally with default profile)
        tell red2 
            write text "echo 'red2 reporting in'"
        end tell 
    end tell
end tell

List of commands, 1 tab

set commandList to { "echo '1'", "echo '2'",  "echo '3'" }

tell application "iTerm2"
    set newWindow to (create window with default profile)

    tell current session of newWindow
	    repeat with c in commandList
    		write text c
    	end repeat
    end tell
end tell

List of commands, many tabs

set commands to {"echo '1'", "echo 2","echo '3'"}

tell application "iTerm2"
    create window with profile "Default"
    set original_window to current window
    
    -- create tabs
    repeat with i from 1 to count of commands
        tell current session of current window
            split horizontally with profile "Default"
        end tell
    end repeat        

    -- run commands in tabs
    repeat with i from 1 to count of commands
        tell session i of current tab of current window
            write text (item i of commands)
        end tell
    end repeat

    -- get last tab
	tell current tab of current window
        set _new_session to last item of sessions
    end tell

    -- select last tab
    tell _new_session 
    	select 
	end tell
    
    -- or select all tabs (cmd+shift+i)
    tell application "System Events" to keystroke "I" using {shift down, command down}
            
end tell

Send commands to other apps.scpt

tell application "Safari"
	make new document with properties {URL:"http://stackoverflow.com/admin.PHP"}
end tell
say "WeeeeeeEeeeee"

Todo: subroutine for running multiple commands

on your_function(a_parameter_of_some_kind)
-- ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment