Example Script

iTerm has sophisticated Applescript support allowing one to write stand-alone scripts to launch the application and open multiple sessions from the addressbook into either new tabs or new windows. You can also set some other parameters for a session such as foreground and background colors, and transparency. Here is a sample script:

-- A sample iTerm Applescript

tell application "iTerm"

	-- close the first session
	terminate the first session of the first terminal

	-- make a new terminal
	set myterm to (make new terminal)

	-- talk to the new terminal
	tell myterm

		-- set size
		set number of columns to 100
		set number of rows to 50
		-- make a new session
		set mysession to (make new session at the end of sessions)

		-- talk to the session
		tell mysession

			-- set some attributes
			set name to "tcsh"
			set foreground color to "red"
			set background color to "blue"
			set transparency to "0.6"

			-- execute a command
			exec command "/bin/tcsh"

		end tell -- we are done talking to the session

		-- we are back to talking to the terminal

		-- launch a default shell in a new tab in the same terminal
		launch session "Default Session"

		-- launch a saved session from the addressbook.
		launch session "Root Shell"
		-- select the previous session
		select mysession
		-- get the tty name of a session
		set myttyname to the tty of the first session
		-- refer to a session by its tty/id
		tell session id myttyname
		    set foreground color to "yellow"
		end tell

	end tell

	-- talk to the first terminal
	tell the first terminal

		-- launch a default shell in a new tab in the same terminal
		launch session "Default Session"

		tell the last session

			-- write some text
			write text "cd Projects/Cocoa/iTerm"
			-- write the contents of a file
			write contents of file "/path/to/file/"

		end tell

	end tell

	-- reposition window and name it
	set the bounds of the first window to {100, 100, 700, 700}
	set the name of the first window to "A Window Title"

end tell

These scripts can then be saved as stand-alone executable applications.

Autolanching Scripts

iTerm also supports autolaunching of an Applescript on startup. On startup, iTerm looks for an Applescript called AutoLaunch.scpt under ~/Library/Application Support/iTerm. If it is found, the AutoLauch.scpt is launched and executed. This feature can be used to launch multiple sessions in defined windows and tabs when iTerm starts up.

User Defined Scripts

iTerm also supports launching of user defined scripts from the "Scripts" menu. The scripts need to be stored under the ~/Library/Application Support/iTerm/Scripts directory. You can create this directory if it does not already exist. iTerm checks this directory on startup.