BBEdit Finder toolbar button

This should come as no surprise to anyone who’s read my recent posts:

Time to stop pretending I might use Sublime Text. Bought the @bbedit upgrade (version 8 -> 10).
— Dr. Drang (@drdrang) Wed Sep 5 2012 11:14 AM CDT

For a while, I was using both TextMate1 and BBEdit, but today I decided the testing phase is over. I removed TextMate from my Dock and put BBEdit in its place. I’ve changed the default application for files ending in .md, .py, and .txt and will change the default for other plain text extensions as I run into them.2

Lastly, I removed the Finder toolbar button for opening files in TextMate and replaced it with one for BBEdit.

About a year ago, I revamped my Finder toolbar and included an improved “Open in TextMate” button from Henrik Nyh. Henrik’s button, and the associated AppleScript-based application, were the basis for today’s new button.

The button works four ways:

1. With one or more files selected, clicking the button opens those files.
2. With a folder selected, clicking the button opens all the files in that folder.
3. With no files selected, clicking the button opens all the files in the folder associated with the window.
4. Dragging files or folders to the button opens the dragged items.

The value of the button is that it can quickly open files in BBEdit when it isn’t their default application.3 For example, on my computers .html files are set to open in Safari, and I want to keep it that way. But when I need to edit an HTML file, I use the button to open it in BBEdit.

To use the button, download the zipped “Open in BBEdit” application, unzip it, and put the unzipped application in your Applications folder. Then Command-drag it to your Finder toolbar.

Update 9/6/12
If you’re running Mountain Lion (I’m not), you may need to run the app directly from the Finder to deal with the new code-signing/Gatekeeper infrastructure. See Jonathan Lundell’s comment below.

If you just want to use the button, that’s all you need to know. If you’re interested in building a similar app/button of your own, here are some details:

The AppleScript that launches BBEdit is pretty much a straight steal of Henrik’s, with “BBEdit” substituted for “TextMate.”

applescript:
1:  -- Opens the currently selected Finder files, or else the current Finder window, in BBEdit. Also handles dropped files and folders.
2:
3:  -- Original (for TextMate) by Henrik Nyh <http://henrik.nyh.se>
4:  -- Adapted to BBEdit by Dr. Drang <http://leancrew.com>
5:  -- Based loosely on http://snippets.dzone.com/posts/show/1037
6:
7:  -- script was clicked
8:  on run
9:    activate application "System Events"
10:    activate application "Finder"
11:    tell application "Finder"
12:      if selection is {} then
13:        set finderSelection to folder of the front window as string
14:      else
15:        set finderSelection to selection as alias list
16:      end if
17:    end tell
18:
19:    bb(finderSelection)
20:  end run
21:
22:  -- script was drag-and-dropped onto
23:  on open (theList)
24:    bb(theList)
25:  end open
26:
27:  -- open in BBEdit
28:  on bb(listOfAliases)
29:    tell application "BBEdit"
30:      open listOfAliases
31:      activate
32:    end tell
33:  end bb


Update 9/6/12
I added Lines 9 and 10 to work around a bug in Lion and Mountain Lion that’s discussed in the comments.

I saved this as an application with the name “Open in BBEdit.” At this point, it does what it’s supposed to, but its icon is generic and appears in the Dock when the app is launched. We’ll deal with these deficiencies in reverse order.

Right-click on the app and choose Show Package Contents from the menu. Inside the Contents folder is a file called Info.plist. This is just an XML text file. I added the following items inside the main <dict> element:

<key>LSUIElement</key>
<string>1</string>


The LSUIElement key, when set to 1, identifies the app as an “agent,” and agents don’t appear in the Dock.

Making the app’s icon was a bit trickier. The Finder toolbar uses a 32×32 icon, so I only needed to make that one size. It’s built from two parts: a rounded-corner rectangle filled with a gray gradient, and 16×16 BBEdit icon. I got the icon from BBEdit itself. Buried a few levels deep in the BBEdit package is the BBEditApplication.icns file, which includes several icon sizes and designs.

Double-clicking the .icns file opens it in Preview, which allows you to see all the sizes.

Icon 11 is the one used in the Finder’s list view, which seemed like a good choice for the button. I dragged it out from Preview onto my Desktop, where it became a TIFF file. I opened it in Acorn, put it in a layer over the rounded rectangle, and exported the result as a PNG file.

The free version of img2icns allowed me to make a file called droplet.icns from the PNG. I copied this file into the “Open in BBEdit” package to replace the default icon.

With the changes to Info.plist and droplet.icns, the “Open in BBEdit” app was complete.

1. Version 1.5, of course. I found TM 2 weird and foreign.

2. I suppose I could use RCDefaultApp, but I’m not sure it still works.

3. Yes, I could right-click the file and use the Open With… command, but the toolbar button is faster.