iTerm Here

iTerm Icon

This is, for lack of a better description, my version of the Open a Command Window Here Windows XP PowerToy. I’ve played around with doing this from the context menu, but I eventually decided I’m not fond of that approach. Since I have the toolbar visible in Finder, however, it’s pretty easy to put a script up there and pretend it’s a real toolbar button.

To use it, save it as an application somewhere you can find it, and call it “Term Here”. Find it with the Finder, copy and paste iTerm’s icon onto it, and drag it up into your Finder toolbar (like how you would drag it into the dock).

My version of this will use the path of the selected item in the front finder window if it is a folder (and is the only thing selected). Otherwise, it will use the path of the Finder window itself. If you want to skip the selection business altogether, just replace the call to selectedFolder() before the last (iTerm) stanza to currentFolder(). It then launches iTerm if needed, create’s a new (empty) window if needed, opens a new tab in that window pushd‘s to the directory, and lists its contents for you.

Here’s the script:

on currentFolder()
tell application "Finder"
    try
        return POSIX path of (front window's folder as text)
    on error
        return null
    end try
end tell
end currentFolder

on selectedFolder()
try
    tell application "Finder"
        get the selection
        set theItem to result's first item as alias
    end tell
    set theInfo to info for theItem
    if theInfo's folder then
        return POSIX path of theItem
    end if
end try
return currentFolder()
end selectedFolder

set targetPath to selectedFolder()
tell application "iTerm"
activate
try
    set term to the front terminal
on error
    set term to (make new terminal)
end try
tell term
    launch session "default session"
    tell current session
        write text "pushd '" & targetPath & "'"
        write text "ls"
    end tell
end tell
end tell

{: lang=applescript }

Feedback, improvements, etc. welcome. Unless you think it should automatically detect whether iTerm or Terminal is your default terminal program, in which case your feedback is only welcome if you can explain a way to detect this from AppleScript that doesn’t double the length of the script. :) I just don’t care enough about this issue to figure out how to do it.

This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Comments are closed.