TextMate Here

TextMate Icon

That was actually the first post in a series (of two). This one opens a new TextMate window on the selected item (if there’s a single item selected), or the current folder otherwise. This makes the script a lot shorter (that and there being a simple command-line TextMate launcher that I can use).

The instructions are the same as last time; save it as an application somewhere you can find it, and call it “TextMate Here”. Find it with the Finder, copy and paste TextMate’s icon onto it, and drag it up into your Finder toolbar.

If you have TextMate installed someplace strange (or have an old enough one that it doesn’t have the mate command in it) it will break; if this happens, you get to keep both pieces.

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 selectedItem()
    tell application "Finder"
        try
            get the selection
            return POSIX path of (the result's first item as text)
        end try
    end tell
    return currentFolder()
end selectedItem
 
do shell script "/Applications/TextMate.app/Contents/Resources/mate '" & selectedItem() & "'"

Feedback, improvements, etc. still welcome.

AppleScript | Permalink | No Comments

iTerm Here

iTerm Logo

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

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.

AppleScript | Permalink | No Comments

Updates

Okay, I rebuilt the site again. Since I didn’t just copy the theme over, there will likely be some formatting quirks. On the plus side, since Wordpress is one-click installed now, hopefully we can stop counting the lag between Erwin updating his blog and me updating mine in months.

Site Info | Permalink | No Comments