Scripts for iTerm 2

This script relies on having the shell integration scripts installed. At least I think that’s what’s giving me the current path in my tab title. Anyway, your tab titles have to be in the format SHELL PATH (PROCESS), e.g. “fish /Users/ttscoff/Dropbox/Work/LEX (fish)”. Once that’s set up, you can use this for both name and address scripts:

tell application id "com.googlecode.iterm2"
	tell the current window
		tell the current session
			return item 2 of my split(get name, " ")
		end tell
	end tell
end tell

on split(theString, theDelimiter)
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theDelimiter}
	set _array to every text item of theString
	set AppleScript's text item delimiters to atid
	return _array
end split
1 Like

I’m not an iTerm user myself, but @grahamb here wrote internally:

Hook works with iTerm2, unless user checks “Preferences>Appearance>General>Exclude from Dock and ⌘-Tab Application Switcher”

From my testing, if that box is checked Hook can’t detect iTerm2, neither with [NSWorkspace menuBarOwningApplication] nor [NSWorkspace frontmostApplication] then not

I’ve been supposed to add it to the Linkable Mac Apps page. I’ve now added an entry about iTerm 2 and linked it to this forum topic ( √ 11:36 PM PT).

@grahamb, does Brett’s script work around the issue you identified?

I didn’t want to change my terminal title because I’m already happy with how I have it configured, so I tried to find an alternative way to make iTerm linkable - and because iTerm has excellent support for AppleScript, I found one. You can pull the current working directory from iTerm like this:

tell application id "com.googlecode.iterm2" to tell the current window to tell the current session
	return "file://" & (variable named "path")
end tell

I haven’t enabled iTerm’s Shell Integration features either, so this works out of the box with no special effort on the user’s part. I believe setting up Shell Integration is needed if you want to keep track of the path over SSH, but for local files it just works.

Maybe this script should be added to Hook’s default set, since it works without depending on the user having a particular shell configuration and is language-agnostic?

3 Likes

I agree that this should be the default script for iTerm. Works perfectly without any setup.

1 Like