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.

BBEdit Finder toolbar button

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.”

 1:  -- Opens the currently selected Finder files, or else the current Finder window, in BBEdit. Also handles dropped files and folders.
 3:  -- Original (for TextMate) by Henrik Nyh <>
 4:  -- Adapted to BBEdit by Dr. Drang <>
 5:  -- Based loosely on
 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
19:    bb(finderSelection)
20:  end run
22:  -- script was drag-and-dropped onto
23:  on open (theList)
24:    bb(theList)
25:  end open
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:


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.

BBEdit application icon file

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

Small BBEdit icon in Preview

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.

Open in BBEdit icon file

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. 

16 Responses to “BBEdit Finder toolbar button”

  1. Lauri Ranta says:

    The run handler is affected by rdar://9406282: Finder scripting selection may refer to bogus value not correlated with UI.

    If you open a new Finder window, select some file, and click the tool bar icon, BBEdit opens files selected in the second frontmost window (or nothing). As a workaround, you could focus another application and then Finder again before getting the selection:

    activate application "SystemUIServer"
    activate application "Finder"

    update folder of Finder window 1 or focusing a different window doesn’t seem to work.

    I had a few scripts like this, but I stopped using them because of that bug. I’ve just assigned a shortcut to a script that focuses an open with or open recent menu:

        set l to {"Open Recent", "Open Recent File", "Recent Projects", "Open Recent Set", "Open With"}
        tell application "System Events" to tell (process 1 where it is frontmost)
            tell menu bar item 3 of menu bar 1
                repeat with x in l
                        click menu item x of menu 1
                        exit repeat
                    end try
                end repeat
            end tell
        end tell
    end try

    But there’s another bug where the open with menu doesn’t work with files selected on the desktop if you also have other Finder windows open.

    People wouldn’t be so afraid of the file system if Finder actually worked.

  2. Dr. Drang says:

    I suppose the reason I never noticed this bug, which also affects my old “Open in TextMate” button, is that I always open files by dragging to the button instead of selecting and clicking.

    The easiest workaround, suggested by you and also by Daniel Jalkut in the radar, is to add

    activate application "System Events"
    activate application "Finder"

    just before the tell line in the on run handler. There’s a little flash as the Finder is deactivated and reactivated, but it does fix the problem of opening the wrong file.

    The radar says this is a Lion bug. I haven’t upgraded either of my computers to Mountain Lion—is the bug still there? If the bug isn’t present in ML, I’m inclined to leave the AppleScript as is. Future readers, including me, won’t get any value out of a workaround for a bug in an out-of-date OS.

  3. Jonathan Lundell says:

    I confirm that the bug is present in 10.8.1, and that the good doctor’s suggested fix works fine.

    I had to run the script once from the Finder in order to bypass the ML code-signing check.

    Idle curiosity: why the small button? My only other third-party toolbar button is the same size (height, anyway) as the stock buttons, and make marginally easier click targets.

  4. Dr. Drang says:

    Thanks for telling me about the bug. I’ll add the workaround to both the AppleScript listing and to the zipped app.

    The button is small to match the other custom buttons in that section of the toolbar. That was the standard toolbar button size back in the Snow Leopard days (see the screen shot in my post from early last year, where all the buttons are the same size), and I just haven’t gotten around to making them bigger. I should, though. Is 48×48 the right size now?

    Update: It looks like the only way I can make the buttons “bigger” is to make them more square. I can increase the height this way, but it will decrease the width. The value in using icons that look like buttons was better visual integration with the standard Finder toolbar buttons. Some of that integration will be lost if I make the buttons square. I guess I’ll have to do some experimenting to see what looks best.

  5. Lauri Ranta says:

    If System Events wasn’t running before, activating it would take a bit longer than activating a process like SystemUIServer. It would also use some memory for a few minutes.

    $ time osascript -e 'activate app "System Events"'
    real    0m0.256s
    user    0m0.043s
    sys 0m0.014s
    $ time osascript -e 'activate app "SystemUIServer"'
    real    0m0.060s
    user    0m0.031s
    sys 0m0.012s
    $ time osascript -e 'activate app "System Events"'
    real    0m0.061s
    user    0m0.031s
    sys 0m0.012s
    $ time osascript -e '""'
    real    0m0.054s
    user    0m0.029s
    sys 0m0.011s
  6. Dr. Drang says:

    I’ve tried it both ways, Lauri, and found no perceptible difference to the user. While I appreciate the concern for efficiency and memory use (especially as someone on a RAM-constrained iMac), I’m going to stick with System Events because I think it’s an application more AppleScript users are familiar with.

  7. Jonathan Lundell says:

    40x22, says xScope. The cd-to icon looks to be 22x22, which isn’t so bad either. Plus Retina variants, presumably.

    Also, Apple’s (but not cd-to) have unselected variants, used when the window doesn’t have focus. Maybe not available to dragged-in apps?

  8. Dr. Drang says:

    Apparently, Jonathan, we were typing at the same time. See my update to Comment #4.

  9. Claude says:

    I’ve adapted the script for Sublime Text. I’ve made a icon but I don’t have a blog to publish it, if I can send it to someone it would be nice. The icon looks small too. the script:

    -- Opens the currently selected Finder files, or else the current Finder window, in SublimeText2. Also handles dropped files and folders.
    -- Original (for TextMate) by Henrik Nyh
    -- Adapted to BBEdit by Dr. Drang
    -- Based loosely on
    -- Open with Sublime Text 2:
    -- script was clicked
    on run
        activate application "System Events"
        activate application "Finder"
        tell application "Finder"
            if selection is {} then
                set finderSelection to folder of the front window as string
                set finderSelection to selection as alias list
            end if
        end tell
    end run
    -- script was drag-and-dropped onto
    on open (theList)
    end open
    -- open in SublimeText2
    on SublimeText2(listOfAliases)
        tell application "Sublime Text 2"
            open listOfAliases
        end tell
    end SublimeText2
  10. Bill Shepherd says:

    I had to recreate the whole thing to get around Gatekeeper - it didn’t want to open your app from the Finder toolbar, but it didn’t have any problem with my app (copied wholesale from you).

    I’d like to see a larger toolbar icon, if that’s possible. Looks okay in the Finder list view, but seems oddly tiny sitting there by itself on the toolbar.

  11. Dr. Drang says:

    I reformatted your code; let me if I messed something up in doing so.

    Don’t know what to say about Gatekeeper, since I don’t have any first-hand experience with it. As I said in Comment 4, I’m experimenting with different icons. I’ll post an update if I come up with something I like.

  12. Michael Fessler says:

    One further issue I just found: On Mountain Lion, if you have no files selected, but are in the “All My Files” view in the Finder, then clicking on the toolbar button yields a dialog box with the message “MacOS Error Code: −4960”.

    Loost as if this may be because because the “All My Files” view is not a “real folder,” so attempts to get “Folder of front window” and convert it to a string fail.

    Selecting a file first, or dragging it to the toolbar icon, works fine.

  13. Tom Shannon says:


    You might already be doing this but FWIW, to get around Gatekeeper, I always need to right click on the file and choose “Open”. This generates a warning message but if you clear it, you won’t get it any more (no matter how you open it).

    I’m not sure why this works for right clicking but not double-clicking. I guess Apple figured that if you know that you can right click to open, you probably know what you’re doing. Or at least you gave it some thought before opening it.

  14. Dr. Drang says:

    Good catch, Michael. I can think of two fixes:

    1. Test for the window name in the if selection is {} part of the on run handler and bail out if it’s “All My Files.”
    2. Use a try/except block in the on run handler to cover a multitude of sins.

    I won’t be doing either of these. One of the great advantages of writing code for yourself is that you don’t have to handle situations you’ll never encounter. Unless I start using “All My Files” (unlikely), I have no reason to clutter up my code with a test for it.

    I do, on the other hand, think it’s important to provide everything you need to customize the app to your needs. If you’re an “All My Files” user, by all means add the code to handle the bug Michael pointed out.

  15. Eddy says:

    Because of Gatekeeper in Mountain Lion, I couldn’t get “Open in BBEdit” to ever launch successfully. Right-clicking and choose “Open” would just display an error code dialog.

    Rather than re-creating the app from scratch like Bill Shepherd did, I found it easier to remove the “quarantine” extended attribute in Terminal:

    xattr -d /Applications/Open\ in\

    After doing that, it started working great. As a long-time BBEdit user, this will be very handy. Thanks!

  16. StumblingMages says:

    Nice tip. I cloned it for Sublime Text 2 and found it didn’t work quite the same for Path Finder. Then I found this post for open in TextMate Very similar but now the same button works in both Path Finder and Finder. The additional bonus is I can launch it via Quicksilver to open the selected files in either file browser.