Textwrangler: Open file from directory of current file

A small thing that’s been bugging me with TextWrangler and BBEdit is that the current directory in the Open File dialog doesn’t follow the directory of the current file.

In other words: First I’ll open and edit a file somewhere in a deep directory hierarchy. This is the main directory that I work in with this project. Now, I need to open a file from my Desktop. No problem, use the shorcut in the side panel of the Open File dialog. Then, I need to open another file from my main directory.

“Son of a — !”

I have no choice but to navigate back from the Desktop to the main directory, clicking on each directory on the way. Infuriating.

I used to use, in my Windows days, an Editor called EditPlus, which had a nice feature where the default directory of the Open File dialog followed the directory of the currently active file. So, from the previous example, if I wanted to open a file in my main project directory, I’d just select (in the editor) a file from that directory and press Cmd-O. Need another file from the Desktop? Select the file I just opened from the Desktop, press Cmd-O.

This Applescript adds the same functionality to TextWrangler. I think it should also work with BBEdit. [EDIT: It does.]

[EDIT: New version uploaded on 2012-10-21, see comments below.]

textwrangler_open_file_in_current_directory.zip

textwrangler_open_file_in_current_directory.tgz

28 comments

  1. Brilliant! I normally use JEdit for working on large projects, but prefer TextWrangler for quick file modifications. With this extra feature I may use TW more frequently! THanks.

  2. Looks like there’s a minor bug in the script; If there are no windows open, there’s no “current document”, and trying to open a document results in a “Can’t get document 1”-error. I’ll fix it when I get the chance. Until then, the workaround is to create a new document with Cmd-N, and then open with Cmd-O.

    Edit: This is fixed in the latest version.

  3. I have downloaded the file but I’m not able to uncompress it. How do I make this script work? Text Wrangler had, until recently, been doing this and I thought it was a default feature. If I had a document in my drawer I could select it and open another file within that directory with ease. Now I have to start over again from my main directory which is annoying when I’m 5+ levels in. I’m glad there’s a solution, just don’t know how to make it work.

  4. That’s odd, I’m also unable to uncompress the tgz from Finder. Works fine from the command line, though. For your convenience, I’ve added the same package as a zip-archive, which uncompresses fine in my Finder.

  5. Thank you – this was my one main annoyance. Took me a few google tries before your post came up. Seems to be working great.

  6. It works fine on Snow Leopard and higher but, however, I tried it on Tiger, and it always generates an error: “TextWrangler got an error: Can’t make into type alias” (where is the filename of the current document, with full path)

    Do you’ve any idea on how to fix it so that it also runs on Tiger?

    Also, I found the file chooser window allows to select a file only… would it be possible to let the user open several files at once?

    Thanks a lot!!

    1. Note: it seems my comment got automatically filtered, and some words were discarded. In particular, the error message is “TextWrangler got an error: Can’t make file into type alias” (where file is the filename of the current document, with full path)

    2. Another note: I’ve zero knowledge of Applescript, but I tried to find what line was causing the error: I did this by forcing errors line by line introducing undefined variables, and with this strategy I found that the line causing the error seems to be this one:

      set new_file to choose file default location (current_path)

    3. Sorry, I have no idea about the Tiger error, and I have to Tiger machine to debug on. Googling the error message does return quite a few hits.

      I’ve uploaded a new version of the script that allows multiple selections, as well as fixes the bug I mentioned in an earlier comment.

  7. It works like a charm, OMG you are going to save me so much time!

    I really miss edit+ anyway, I felt much more productive in writing code. After years, I still don’t have a Mac equivalent so light and powerful.

    1. There are heaps of good editors for OS X. I’m actually pretty old school sticking to TextWrangler. Some editors I’ve heard a lot of good things about are TextMate, Chocolat and Sublime Text. I think it’s just a matter of getting any one of them configured the way you want, and then getting used to it.

  8. Just an additional note: one of the other features I really miss from edit+ is the capability to indent a bunch of code lines just by selecting them and pressing the tab key. If I do that with textwrangler, the text is deleted.
    There is the text->shift right command but it is way less convenient.
    Do you have any solutions for that? Thanks!

    1. I’m not sure if it’s in the default configuration, but I’ve got a keyboard shortcut configured so that Cmd + ] indents and Cmd + [ removes indents. I’ve been quite happy with that. TextWrangler doesn’t allow setting just the tab-key as a keyboard shortcut, but it might be doable with another AppleScript.

  9. You just saved me! This was such an annoyance. Ever since I converted from Windows and was using Textpad, to Mac/TextWrangler, I have missed this simple, intuitive function. Thanks for this fix!!

  10. This works like a champ. Thanks so much, I was just about to give up on TextWrangler over this little thing.

Leave a Reply to Cesar Cancel reply

Your email address will not be published. Required fields are marked *