Open Quicksilver selection in new iTerm window / tab

I’m not very happy with the iTerm plugin for Quicksilver. It does provide an action for opening Quicksilver’s current selection (i.e. an alias to a directory) in iTerm, but it isn’t very fine tuned, actually.

What it does is just open current selection into a new tab in the first opened iTerm window, which is an unwanted behavior in the 99.999999% of the occasions. The most handy behavior would be that the Quicksilver action opened current selection either in a new window (if there are no iTerm windows in your current Space) or in a new tab in the frontmost iTerm window of the current Space (if there are iTerm windows in your current Space).

Writing the scripts

Given that this has bothered me since the very first time I used this Quicksilver action and that I’m surprisingly keen on hacking a bit, I’ve taken courage and started researching AppleScript in the net. After taking a quick look at some introductory material and taking some inspiration, and making some mistakes, I finally came up to this code with help of this “new tab in iTerm” example:

For opening Quicksilver’s selection (must be a directory, not a file) in a new iTerm window in current Space:

-- File: Open in new iTerm window.scpt

tell application "Quicksilver"
	set folder_path to POSIX path of (selection as text)
end tell

tell application "iTerm"
	activate
	set t to (make new terminal)
	tell t -- this creates a new window, unlike tell current terminal
		launch session "Default Session"
		tell last session
			write text "cd '" & folder_path & "'"
			set name to do shell script "basename '" & folder_path & "'"
		end tell
	end tell
end tell

For opening Quicksilver’s selection (must be a directory, not a file) in a new tab in the last iTerm window in current Space:

-- File: Open in new iTerm tab.scpt

tell application "Quicksilver"
	set folder_path to POSIX path of (selection as text)
end tell

tell application "iTerm"
	activate
	tell the current terminal
		activate current session
		launch session "Default Session"
		tell last session
			write text "cd '" & folder_path & "'"
			set name to do shell script "basename '" & folder_path & "'"
		end tell
	end tell
end tell

Adapting the scripts for making Quicksilver actions

The two previous AppleScript’s work perfectly outside of Quicksilver (e.g. when running them within AppleScript Editor). However, if you place these scripts in the Quikcsilver Actions’ directory and try to run them, they won’t work. To make them work, you must make some slight modifications (take a look at Quicksilver’s AppleScript documentation; my inspiration was the “Paste file path” action).

For opening Quicksilver’s selection (must be a directory, not a file) in a new iTerm window in current Space:

-- File: Open in new iTerm window.scpt

using terms from application "Quicksilver"
	on open folder
		set folder_path to POSIX path of folder

		tell application "iTerm"
			activate
			set t to (make new terminal)
			tell t -- this creates a new window, unlike tell current terminal
				launch session "Default Session"
				tell last session
					write text "cd '" & folder_path & "'"
					set name to do shell script "basename '" & folder_path & "'"
				end tell
			end tell
		end tell
	end open
end using terms from

For opening Quicksilver’s selection (must be a directory, not a file) in a new tab in the last iTerm window in current Space:

-- File: Open in new iTerm tab.scpt

using terms from application "Quicksilver"
	on open folder
		set folder_path to POSIX path of folder

		tell application "iTerm"
			activate
			tell the current terminal
				activate current session
				launch session "Default Session"
				tell last session
					write text "cd '" & folder_path & "'"
					set name to do shell script "basename '" & folder_path & "'"
				end tell
			end tell
		end tell
	end open
end using terms from

For Quicksilver to recognize these actions, you must place both AppleScript files into ~/Library/Application Support/Quicksilver/Actions/ and restart Quicksilver. After doing that, you can just fire up Quicksilver, select an alias to a directory in the first tab, and then select the wanted action in the action tab (second tab).

やった! 🙂

Useful links