AppleScript syntax highlighting

Skip to setup instructions

I've been using Alex Gorbatchev's SyntaxHighlighter to syntactically display code of various languages for several months now. When I decided to post an AppleScript snippet, however, I realised that I was out of luck. SyntaxHighlighter does not include an AppleScript "brush", and a quick flick through the SyntaxHighlighter forums did not bring me any joy.

How hard could it be to write a brush for AppleScript?, I wondered. The handy guide to developing a custom brush got me started, and I was soon busy trying to encapsulate AppleScript's syntax — along with its keywords and countless words and phrases with special meanings — into a handful of regular expressions.

Having created the brush (that's SyntaxHighlighter lingo for the JavaScript file containing the language-specific regular expressions), I proceeded to create a style sheet that would display AppleScript snippets on the Web as they appear on OS X. You can judge my success by comparing the three snippets below: the first is a screenshot of Script Editor's rendering; the second is a screenshot of the same snippet as it appears in my browser; and the final snippet is text rendered by your browser.

Screenshot of Script Editor's rendering of an AppleScript snippet
Screenshot of snippet styled by AppleScript theme for SyntaxHighlighter
(*
    This handler determines whether someone is happy.
    It's actually just a good way to showcase AppleScript syntax highlighting using Alex Gorbatchev's SyntaxHighlighter.
    (* Hey, nested comments. Woot! *)
*)

on user_is_happy()
    -- determine which application is currently frontmost
    tell application "System Events" to set app_list to the name of application processes whose frontmost is true
    set front_app to the first item of app_list
    tell application "Finder"
        activate
        display dialog ¬
            "Enter your name" default answer "" buttons {"Cancel", "OK"} ¬
            default button 2 with icon note
        set user_name to the text returned of the result
        (* The word "return" in the line below should not be in bold. The regex does its best to determine whether the word is being used in a return statement or as a line return. *)
        display dialog "Hello, " & user_name & "!" & return & return & ¬
            "Are you happy?" buttons {"Yes", "No"} with icon note
        set is_happy to the button returned of the result
    end tell
    -- activate the application that was frontmost initially
    tell application front_app to activate
    return is_happy = "Yes" -- this time the word "return" should be in bold
end user_is_happy

if user_is_happy() then
    say "Woohoo!" using "Alex"
else
    repeat 3 times
        beep
        delay 0.5
    end repeat
end if

Live rendering of AppleScript snippet

Setup

To add AppleScript syntax highlighting to your own site or blog, do the following:

  1. Download SyntaxHighlighter, and follow the setup instructions.

  2. Download AppleScript brush (≈7.8 kB), and upload it to your SyntaxHighlighter scripts directory.

  3. Download AppleScript theme (≈4.4 kB), and upload it to your SyntaxHighlighter styles directory.

  4. Download background image (154 bytes), and upload it to your SyntaxHighlighter styles directory.

  5. Include the brush like so:

    <script type="text/javascript" src="/path/to/scripts/shCore.js"></script>
    <script type="text/javascript" src="/path/to/scripts/shBrushAppleScript.js"></script>
    <script type="text/javascript">
        SyntaxHighlighter.all();
    </script>
    

Usage

To have SyntaxHighlighter parse a block of AppleScript, wrap the code in pre tags like so:

<pre class="brush: applescript; class-name: applescript;"></pre>

brush: applescript; tells SyntaxHighlighter to use the AppleScript brush for the text within the pre tag. class-name: applescript; tells SyntaxHighlighter to add the class name "applescript" to the container div that is inserted into the page. (Hopefully I'm able to convince Alex that the brush name should be added as a class name automatically, which would remove the need to include class-name: applescript; each time.)

Note that including ruler: true; will have no effect. Since AppleScript is displayed in a variable-width font, the ruler serves no purpose. The ruler is still inserted into the page (unless ruler: false; is included), but is hidden by the style sheet.

Comments

Do you use a wordpress plugin for syntax highlighting or do you add the classes/JS calls manually? I'm currently experimenting with various wordpress plugins, some of which use Alex Gorbatchev's SyntaxHighlighter but none seems to get de job done right. Some loose indentation, some generate double escaped HTML entities, some forget to place a tag...

What's your setup, and does it get the job done?

Ronald

@Ronald I use the standard version of SyntaxHighlighter, not the WordPress plugin. I am quite happy with SyntaxHighlighter overall, but have modified a number of the brushes to provide additional hooks for styling. At the moment I'm using Prototype to load brushes dynamically. If this is of interest to you, keep an eye out for an improved version of loader.js which I'll hopefully make available within the next few days.

@Ronald: the WordPress plugin that does the things right has recently had a full rewrite. Most other WordPress plugins are forks of this one with added (or removed) features. The name is SyntaxHighlighlighter Evolved by Viper007Bond and it supports out-of-the-box dynamic loading of the necessary brushes (I believe it does so statically). It supports using [name][/name] syntax where "name" is the language alias and automatically inserts the html markup. It preserves whitespace correctly now (was my own admission to the plugin).

@David: I'll add the AppleScript to my overview of available custom brushes. Thanks for notifying me, I had overlooked this one (if you, or anyone else, knows of others that are not included, it'd be great to hear of it!).

-- Abel --

Hi David,

I've followed your instructions as well as the instructions on the SyntaxHighlighter website but I can't get your script working (JavaScript works fine). Are you able to help? I don't know if I can subscribe to updates on this post so maybe you could fire me a quick email? :)

Thanks! Chris

Chris

Respond