AppleScript syntax highlighting again
September 9, 2012 at 11:11 AM by Dr. Drang
The AppleScript language rules for the Highlight.js syntax highlighting package have made a big leap forward and have reached a state where people other than Nathan Grigg and I can use them.
As before, virtually all the credit goes to Nathan. He extended and cleaned up my initial ideas on how to handle multi-word commands and keywords—things like display dialog
and choose from list
, which AppleScript is littered with—putting them in a form that both more consistent with the rest of Highlight.js and easier to work with in a CSS file. My only contribution has been a simple definition that tries to distinguish between the use of the word return
as a command and its use as a constant.
Here’s a brief test suite that shows the features (and a couple of bugs):
applescript:
-- multi-word commands
do shell script "date"
display dialog "Does this work?"
-- multi-word constant
set text item delimiters to "|"
-- types and properties
tell application "iTunes" to get current selection
get character 5 from "abcdefghij"
-- multi-word keyword (greater than)
if 5 is greater than 4 then
set thing to 6
end if
-- "return" as a constant
set a to return & "abc" & return & "def" & linefeed & "ghi"
-- "return" as a command
return a
-- "return" as a command and constant
if 4 is less than 5 then
return thing & return & a
end if
-- "return" inside string is properly identified
set c to "abc is not a
return to def"
-- "return" as a constant, misidentified as a command
set b to "abc" & ¬
return & "def"
-- application dictionaries can redefine words
tell application "BBEdit" to set show tab stops to true
The second-to-last example shows how the rule for return
can be fooled to show its use as a constant misidentified as a command.
The last example shows a feature of AppleScript that will play havoc with any syntax highlighter that isn’t itself an AppleScript interpreter: dictionaries can redefine words, even words that are part of the core of AppleScript. In this case, AppleScript’s tab
constant has been subsumed into the show tab stops
property in BBEdit’s dictionary. The highlighter knows tab
but knows nothing of show tab stops
, so the highlighting is wrong. This is the problem Hamish Sanderson (has) was talking about in his comments on my earlier post. He had some suggestions on how to get around this problem, but they’d change my workflow in ways I’m not willing to accept at the moment. I’ll live with the occasional highlighting errors.
If you want to use these definitions with Highlight.js, get a copy of either my fork or Nathan’s from GitHub. Make sure you get the “applescript” branch. In Terminal, cd
to the main directory of the repository and execute
python tools/build.py :common applescript
This will create a “packed” version of Highlight.js in the build
directory called highlight.pack.js
. It’ll include syntax defintions for AppleScript in addition to common languages like Perl, Python, Ruby, JavaScript, C, and so on. A complete list of the languages included will print out as the build proceeds.
Upload the highlight.pack.js
to your web server and add the code to your HTML that will include and call it.
The highlighting styles come through CSS. You can use one of the many styles that come with Highlight.js or use one of them as a template to create your own. To highlight certain AppleScript language features, you’ll have to include styles for the following classes that aren’t in the standard Highlight.js CSS files:
pre .property
for things likecharacter
,item
,day
,month
, andPOSIX path
.pre .type
for things likeapplication
,date
, andalias
.pre .command
for things likeactivate
,say
,display dialog
, anddo shell script
.
With the definitions in a reasonably mature state, I doubt that I’ll be blogging about this anymore. Whatever changes are are made will show up in my GitHub repository or Nathan’s.