---
title: BBEdit "Projects" in Applescript
teaser: BBEdit is very scriptable. Here's an example.
tags: osx
author: Jon Yurek
published_on: 2006-10-31
---

I really, really can't decide between [TextMate] and [BBEdit], the majority of
reasons of which I should leave for another post. But anyway, one of the most
annoying things that I thought was left out of BBEdit was the ability to save
the contents of the Documents Drawer for easy retrieval. I like to keep my work
logically separated by window, and it's a pain to drag all the documents I need
where I want them every time.

[TextMate]: https://www.macromates.com/
[BBEdit]: http://barebones.com/products/bbedit/

Thanks to BBEdit being very Applescript-able, I wrote these scripts. Stick 'em in
your scripts folder and they'll get some use.

Save Project

```applescript
tell application "BBEdit"
    set w to first text window
    set theFiles to the file of every item¬
        of text documents of w
    set f to choose file name with prompt¬
        "Select a project file."
    try
        set fSpec to open for access f¬
            with write permission
    on error what
        display alert¬
            "Could not save project." message what
        close access f
        return
    end try
    set eof of fSpec to 0
    try
        write theFiles to fSpec as list
    on error what
        close access f
        display alert¬
            "Could not write project file." message what
        return
    end try
    close access fSpec
end tell
```

Load Project

```applescript
tell application "BBEdit"
    set f to choose file
    open for access f
    set fs to read f as list
    close access f

    set w to make new text window
    set show documents drawer of w to true
    set doc to ID of first text document of w
    set i to 1
    open fs opening in w
    close document id doc
end tell
```

It's not gorgeous code, but it works. And it's one less thing I have to worry
about in BBEdit. Interestingly enough, I would have written these in Ruby with
[RubyOSA], but the darn thing won't load BBEdit's [sdef] because it [uses non
ASCII characters][non-ascii].

[RubyOSA]: http://rubyosa.rubyforge.org/
[sdef]: https://web.archive.org/web/20070103121140/http://www.satimage.fr/software/en/projects_xcode_sdef.html
[non-ascii]: http://rubyforge.org/tracker/index.php?func=detail&amp;aid=6338&amp;group_id=1845&amp;atid=7180

And for what it's worth, I like to save the documents with a .bbdd extension.
