Wordyard

Hand-forged posts since 2002

Scott Rosenberg

  • About
  • Greatest hits

Archives

Wordyard / Software / A geeky problem with Mac scripting

A geeky problem with Mac scripting

November 22, 2009 by Scott Rosenberg 12 Comments

Here’s what turns out to be the most intractable problem I’ve encountered in my move to OSX as my primary work platform:

For years I used a programmers’ text editor tool in Windows called Ultraedit. It worked great and allowed me to record macros. The most indispensible one, which I used constantly, was for automating the creation of HTML links. I would store the link-to URL in a clipboard, select some link text and start the macro. The macro would magically surround the link text with the proper HTML code to link it to the URL in the clipboard.

I achieved this by
(a) copying the link text to a second clipboard;
(b) typing the <a href=”

(c) pasting in the URL from the first clipboard;
(d) closing the tag with “>
(e) pasting the link text from clipboard #2;
(f) ending the link with </a>

It sounds kinda complicated but it worked beautifully, and Ultraedit’s macro recorder simply “got it.” I created the macro years ago, and its keyboard shortcut became hardwired in my memory.

Now I’m using TextWrangler and, alas, AppleScript doesn’t seem to get it at all. The AppleScript recorder seems to grab the actions at too specific a level — i.e., it doesn’t capture “switch to next clipboard” but records the specific clipboard number; it doesn’t capture “current active document” but records the specific document name that I happen to be using while I’m recording the script.

I was gearing myself up to learn enough AppleScript to try to write the script (or edit a recorded script well enough to make it work). Then I discovered that, perhaps thanks to Snow Leopard upgrade, the entire AppleScript recorder in TextWrangler doesn’t seem to work at all. When I record a script and try to save it I get the following error message: (MacOS Error code: -4960). As far as I can tell, I can’t save any scripts at all, making any AppleScript solution to this problem seem hopeless.

I know, I know, if I had learned emacs years ago I wouldn’t have any of these problems. But I didn’t. I welcome any tips/suggestions! Is there a text-editor for Mac that will make my life easier? (I used to use the full version of BBEdit, and, back in those days, it wasn’t any easier to script than Textwrangler.) Is there some obvious solution I’m missing?

Filed Under: Software

Comments

  1. Gabe

    November 22, 2009 at 10:10 am

    I’m sure there are many ways to do this, but I’d use the excellent third-party tool Keyboard Maestro. You can use it to create keyboard macros that work everywhere or within a specific application. It allows multiple clipboards and preset text snippets—everything you’d need for this function.

    Another advantage: once you set up the macro, you can easily set it to work in any text editor.

  2. T.J. Lahey

    November 22, 2009 at 10:15 am

    TextMate (http://macromates.com/) is an excellent text editor. It has an entire mode for dealing with HTML and has lots of handy shortcuts for manipulating text. It also allows you to use basically any UNIX scripting language to extend it. However, for something as simple as what you’re doing, BlogAssist (http://www.dejal.com/) might be what you’re looking for. Both have trial versions so you can see if they fit what you need.

  3. Scott Rosenberg

    November 22, 2009 at 10:27 am

    Thanks for these helpful (and fast!) suggestions. I’ll pursue them and post my experiences…

  4. Jeremy

    November 22, 2009 at 11:02 am

    jEdit lets you record macros, that can be used to make keyboard shortcuts, that allow you create a hyperlink or any other similar statement with the contents of your clipboard.

  5. John Schofield

    November 22, 2009 at 4:01 pm

    I’ve got to second the TextMate suggestion. TextMate is one of the best text editors out there, ever, on any platform. For the problem you describe, it’s like hitting a gnat with a…large and heavy thing.

    But since you write text for a living, you might consider it anyway. If I wasn’t forced to be cross-platform on a regular basis, I’d use TextMate exclusively.

  6. Naum

    November 22, 2009 at 5:44 pm

    Another plug for TextMate.

    Has lots of shortcuts for linking text.

    For example, Cmd+Shift+L will take your highlighted text and make a link out of it, using URL you previously selected (from your browser) session.

    Or pound another key and it will do a Google “I’m Feeling Lucky” search and auto-generate the link tag for the link destination.

  7. Patrick Nielsen Hayden

    November 22, 2009 at 6:15 pm

    TextMate plus the TextExpander preferences pane does pretty much everything I can imagine needing.

    Bit of a learning curve on both, but worth it.

  8. Scott Rosenberg

    November 22, 2009 at 9:55 pm

    Excellent! I’m finding Textmate great as promised, and Keyboard Maestro gets my particular macro up and running as well.

    Problems solved. Let no one sneer at the wisdom of crowds, at least this particular crowd….

  9. Rich Siegel

    November 23, 2009 at 5:45 am

    AppleScript isn’t a macro system; it’s for automation – so while you can record operations and use the results as a basis for writing a new script, you’ll typically find that it’s not suitable for the sort of “macro” recording and playback that you’re thinking of. (Side note: the -4960 error will be fixed in a TextWrangler update, but please note that you can always record scripts using the AppleScript Editor.)

    For your use case, the BBEdit clipping system does precisely what you need. If you make a clipping with text like this:

    <a href=”#selection#” rel=”nofollow”>#clipboard#</a>

    Then copy the link text to the clipboard, select the URL in your text, and invoke the clipping (“Insert Clipping…”).

    Since you already have BBEdit, you have everything you need. :-)

  10. Rich Siegel

    November 23, 2009 at 5:46 am

    Ugh, the blogging system mangled my clipping example. Figures. Write to the BBEdit google group and someone can provide an example there. :-)

  11. Scott Rosenberg

    November 23, 2009 at 6:15 am

    Thanks for the tip, Rich!

    I fixed your clipping example (which survived intact in the database) to be readable for future reference…

  12. Volker Kleinschmidt

    July 24, 2018 at 10:19 am

    Btw, the error -4960 has not been fixed, and TextWrangler 5.5.2 is the latest version of that product, which has been sunset.

    What’s the most irritating about that error is that nobody ever gives any explanation for what it might mean and how one might avoid it. It’s just an “unknown error”. Mysterious and unexplainable. Something went wrong. Super unhelpful!

    I’ve just gotten it while trying to save a totally innocuous 7-line text file I had typed up – and creating the same file (same name, same location, same contents) in TextEdit, then opening/editing/saving it in TextWrangler worked just fine.

Leave a Reply

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