Fun with the OS X Finder and AppleScript

[image]

So a few months ago I finally decided to bite the bullet and switch to a Mac at home after about five years or so of using Ubuntu. There's lots of random little reasons, but the general idea is that Linux on the desktop is chaos right now and I got sick of fighting with Ubuntu every time there was an update. This, combined with the fact that my son is now old enough to start using a computer regularly and I picked him an old Mac, pushed me to moving over myself. It's just easier to manage, teach, etc. if we're all on the same system.

Though some things are definitely more stable or clean, there are lots of little things I miss from using Gnome on Linux. One of them specifically is Nautilus, and the amazing functionality it has like tabs, efficient use of space, intelligent sorting options, virtual file systems for mounting drives over SSH or SFTP, and a bunch of little plugins. Anyone who loves the Finder hasn't really used any other file manager, I don't think. (And anyone who loves Apple's dictatorial control over the OS - defaulting all the save dialogs in standard apps to iCloud in Mountain Lion, for example - is a complete jackass.)

After fighting with the Finder again this morning, I decided to take a look at the options for adding in some missing features... Specifically, I wanted to be able to open any file in my text editor without having to go through a bunch of dialogs. I recently swapped to Sublime Text 2 from TextWrangler, which had this great little 'Services' menu item which added an option to open any file using the right-click menu. I also wanted to be able to, in the Finder, create a new text document for development, notes, etc. Finally, I wanted to be able to open a folder in iTerm 2 (which I use instead of Terminal) from the Finder as well. Terminal has this option built in of course, but it doesn't work for iTerm.

I decided to try out AppleScript, as it's more 'integrated' into the OSX system, rather than just launching some batch file. I won't make that mistake again, I promise you. AppleScript was created by pure psychopaths, who obviously had a deep, deep hatred for humanity and a less than solid grasp of logic and organizational skills. Also, just about everyone who's ever used AppleScript and posted their solutions online is a complete moron. Probably myself included, but here goes.

Opening any Finder item in your text editor

Here's the script for the first feature I wanted. It's Applescript, but I used the Automator app to make it, as it allows you to create a 'Services' script with a .workflow extension which sit in ~/Library/Services and appear on that nutty 'what-the-hell-is-this-stuff' tab in the Keyboard Settings. I looked it up, and it's weird because Services are an idea that are held over from the NeXT first created years and years ago. The fact it's still there is almost an anachronism - I'd almost say that it'll probably be gone soon, but the fact that it's been around in OS X for 10 years already makes me wonder if there's something oddly important going on there...

Anyways, here's the script. It's small and straight forward, and could probably even use some error handling logic, but it works well enough as it is that I wasn't overly worried about it.

[image]


on run {input, parameters}

        tell application "Sublime Text 2"
                restore -- needed if the window is minimized
                activate
                open input
        end tell

end run

The key part to notice is the 'Services receives selected' dropdown choice and application next to it. What this does is filter the place where you're going to see this Service show up. By choosing files and folders and the Finder, you're limiting it to the context menu when you right click a folder in the Finder, which is pretty much what we want. After you save the script, again, with the .workflow extension into it's special spot in your (hidden by default) ~/Library/Services folder, a checkbox item appears in the Keyboard Shortcuts/Services list in the Settings. You don't actually have to go there, as it's checked on by default when you save, but this is what it looks like:

[image]

If you do go in there, you might as well check off all the services for apps you don't use just to clean up your menus a bit. Where do the options for all this stuff live? No idea, it's some freaky back corner of OS X that we probably shouldn't be messing with, actually...

Creating "Apps" with the AppleScript editor

So when I started down this path of creating these little scripts (which took me a few hours actually, as using AppleScript is like being tied down in a black leather gimp-suit at a dominatrix convention, and losing the safety word... The syntax is like Erlang and ancient Greek got somehow mixed together with Visual Basic, and was translated back into English by mentally-challenged ESL students... but I digress... ) I thought that I'd be able to create a couple right-click options that would let me create a text file and launch a console right from the Finder. But that isn't really possible, as the Services don't pop up if you right-click in the middle of the Finder window - only if you right click on a file or folder icon itself, which isn't really ideal.

I decided instead to create a couple AppleScript .apps that I could launch from a button in the Finder's toolbar, so it looks like this:

[image]

I didn't realize this at first, but using the AppleScript editor, you can create Mac .app application bundles, which are also editable later. The editor also lets you easily open up the bundle and replace the applet.icns file with whatever you want. I just grabbed the icon files from the iTerm and Clippings apps (the latter because it was more 'square' than the regular document icon, in case you really want to know) which gave the scripts a nicer feel than the default script icon. Then I just drag/dropped them into the toolbar on the finder and 'poof', new features! The logic in the AppleScript basically just grabs the window's active folder (and since you just *clicked* the button in the Finder, it's ready to go), and then passes that to a bunch of other lines that seem to do something intelligent with it, or maybe not. It's hard to tell really with AppleScript whether you're creating a new text file, erasing your hard drive or giving yourself a lobotomy. (That last one is a joke of course, just by using AppleScript, you've already demonstrated you've already had one.)

Here's the scripts and some screen shots of the AppleScript text editor.

New Text Document Here


tell application "Finder"
        try
                set fileName to "untitled"
                set fileExt to ".txt"
                set thisFolder to the target of the front window as alias
                tell application "System Events"
                        set fileList to get the name of every disk item of thisFolder
                end tell
                set newFile to fileName & fileExt
                set x to 1
                repeat
                        if newFile is in fileList then
                                set newFile to fileName & x & fileExt
                                set x to x + 1
                        else
                                exit repeat
                        end if
                end repeat

                make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"}
        on error errMsg
                display dialog (errMsg)
        end try
end tell

[image]

Open iTerm Here


tell application "Finder"

        set thisFolder to the target of the front window as alias

        set folderPath to ((POSIX path of thisFolder) as string)

        tell application "iTerm"
                activate
                tell the first terminal
                        launch session "Default Session"
                        tell the last session
                                write text "cd " & folderPath
                        end tell
                end tell
        end tell

end tell

[image]

And a .zip file for you cowards

So, finally, I zipped up the two .apps you can grab here, just in case you happen to use these exact two apps, want this same functionality, and yet don't feel brave enough to test your sanity playing with the Cthulu-like evil that is Apple's official scripting language. I don't blame you, really. (But hey, you can create the Automator thing by yourself if you want it, since it installs it for you as well...)

Share and enjoy!

-Russ

< Previous         Next >