AppleScript syntax highlighting (finally)

I added syntax highlighting to the source code posted here back in late 2010. After a brief time using Pygments on the server, I switched to Highlight.js. I liked it the best of the JavaScript-based highlighters because it was cleanly written and easy to understand. The only problem with Highlight.js was that it didn’t support AppleScript. Initially, that didn’t bother me because I didn’t do much AppleScripting. That’s changed, and I expect to do more coding in AppleScript as I continue to explore BBEdit.

I was lamenting Highlight.js’s lack of AppleScript support on Twitter this afternoon with Brett Terpstra and Gabe Weatherhead. I had decided it was time to bite the bullet and write the AppleScript definitions myself.

I really need to teach highlight.js how to handle AppleScript.
  — Dr. Drang (@drdrang) Sun Sep 2 2012 5:14 PM CDT

Imagine my delight to find this tweet waiting for my when I got back from supper:

@drdrang @ttscoff @macdrifter I made some not too long ago. I just put them on github. github.com/nathan11g/high…
  — Nathan Grigg (@nathangrigg) Sun Sep 2 2012 6:33 PM CDT

Nathan Grigg is one of those people whose blog doesn’t get nearly the attention it should. As other bloggers rewrite the same rumors about how thin the next iPhone will be, Nathan puts out truly original material that’s clear, concise, and actually helpful. For example, I don’t know if I’d’ve been able to write my recent series of scripts on Markdown reference links in BBEdit without this post of Nathan’s.

Nathan would be, I think, the first to say that his AppleScript definitions for Highlight.js, a branch of his fork from Ivan Sagalaev’s original repository, are just a first step, but that’s the step I was most dreading. AppleScript has a huge set of keywords, and Nathan put in the effort to add them all. He has comments and strings working, too. My fork has a first cut at function definitions.

I’ve updated my styleCode function, first described here, to allow AppleScript code to be entered. Although Highlight.js has a language detection feature that can figure out (reasonably accurately) which syntax definitions to apply, I’ve never used it. I always start the code with a line that declares the language.

A script like this

    applescript:
     1:  tell application "BBEdit"
     2:    set myText to contents of front document
     3:    set myRef to do shell script "~/bin/bbstdin " & quoted form of myText & " | ~/bin/getreflink"
     4:    
     5:    if myRef is not "" then
     6:      if length of selection is 0 then
     7:        -- Add link with empty text and set the cursor between the brackets.
     8:        set curPt to characterOffset of selection
     9:        select insertion point before character curPt of front document
    10:        set selection to "[][" & myRef & "]"
    11:        select insertion point after character curPt of front document
    12:        
    13:      else
    14:        -- Turn selected text into link and put cursor after the reference.
    15:        add prefix and suffix of selection prefix "[" suffix "]" & "[" & myRef & "]"
    16:        select insertion point after last character of selection
    17:      end if
    18:    end if
    19:    
    20:  end tell

will now display like this,

applescript:
 1:  tell application "BBEdit"
 2:    set myText to contents of front document
 3:    set myRef to do shell script "~/bin/bbstdin " & quoted form of myText & " | ~/bin/getreflink"
 4:    
 5:    if myRef is not "" then
 6:      if length of selection is 0 then
 7:        -- Add link with empty text and set the cursor between the brackets.
 8:        set curPt to characterOffset of selection
 9:        select insertion point before character curPt of front document
10:        set selection to "[][" & myRef & "]"
11:        select insertion point after character curPt of front document
12:        
13:      else
14:        -- Turn selected text into link and put cursor after the reference.
15:        add prefix and suffix of selection prefix "[" suffix "]" & "[" & myRef & "]"
16:        select insertion point after last character of selection
17:      end if
18:    end if
19:    
20:  end tell

whereas code without the language line will look like this:

 1:  tell application "BBEdit"
 2:    set myText to contents of front document
 3:    set myRef to do shell script "~/bin/bbstdin " & quoted form of myText & " | ~/bin/getreflink"
 4:    
 5:    if myRef is not "" then
 6:      if length of selection is 0 then
 7:        -- Add link with empty text and set the cursor between the brackets.
 8:        set curPt to characterOffset of selection
 9:        select insertion point before character curPt of front document
10:        set selection to "[][" & myRef & "]"
11:        select insertion point after character curPt of front document
12:        
13:      else
14:        -- Turn selected text into link and put cursor after the reference.
15:        add prefix and suffix of selection prefix "[" suffix "]" & "[" & myRef & "]"
16:        select insertion point after last character of selection
17:      end if
18:    end if
19:    
20:  end tell

I’ve gone back and added the applescript: line to code in recent posts, but I probably won’t bother doing the same to older posts unless I happen to link to them and see that their AppleScript could use a little spiffing up. One of the nice things about dynamic syntax highlighting is that the highlighting will improve as I add new features to the definitions file.