Html-ize scripts

It could be a feature request… :slight_smile:

What is the best way to html-ize a script. I mean with the nice syntax highlighting etc.

Getting the rtf “script” of an .app and process that with textutil gives a nice result but that’s a lot of fiddling with files. But that won’t work with .scpt files (no easy access to the rtf representation).

This should get you close:

use AppleScript version "2.5"
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theFile to (choose file with prompt "Choose a script")
set {theScript, theError} to current application's NSAppleScript's alloc()'s initWithContentsOfURL:theFile |error|:(reference)
if theScript is missing value then error theError's localizedDescription() as text
set theAttString to theScript's richTextSource()
set elementsToSkip to {"doctype", "html", "body", "xml", "style", "p", "font", "head", "span"} -- ammend to taste
set theDict to current application's NSDictionary's dictionaryWithObjects:{current application's NSHTMLTextDocumentType, elementsToSkip} forKeys:{current application's NSDocumentTypeDocumentAttribute, current application's NSExcludedElementsDocumentAttribute}
set {htmlData, theError} to theAttString's dataFromRange:{0, theAttString's |length|()} documentAttributes:theDict |error|:(reference)
if htmlData = missing value then error theError's localizedDescription() as text
set theString to current application's NSString's alloc()'s initWithData:htmlData encoding:(current application's NSUTF8StringEncoding)
1 Like

:slight_smile: Let me try that and I’ll get back to you :slight_smile:

Too bad. For a normal script, I do get html code but not the color styles:

<b>tell</b> <i>application</i> “Mail”<br>
<b>set</b> _sel <b>to</b> <b>get</b> <u>selection</u><br>
<b>set</b> _links <b>to</b> {}<br>
<br>
<b>repeat</b> <b>with</b> _msg <b>in</b> _sel<br>
<b>set</b> _messageURL <b>to</b> “message://%3c” &amp; _msg’s <u>message id</u> &amp; “%3e”<br>
<b>set</b> <b>end</b> <b>of</b> _links <b>to</b> _messageURL<br>
<b>end</b> <b>repeat</b><br>
<br>
<b>set</b> <u>AppleScript’s</u> <u>text item delimiters</u> <b>to</b> <u>return</u><br>
<br>
<u><b>set the clipboard to</b></u> (_links <b>as</b> <i>string</i>)<br>
<br>
<b>end</b> <b>tell</b><br>

And since there are no CSS classes associated to the tags, it is not possible to associate an external CSS sheet to the result and get the syntax coloring :frowning:

Ooops, I had not noticed the “elementsToSkip” part when I first run it. Ok, that’s perfect :slight_smile:

Do you know why the html is very different from textutil’s output ?

No. I’d assume with no elements skipped it would be the same, but there might be some difference depending on what you are feeding it.

But it’s not that different. There are more tags with your method than with the other, but since it’s not possible to access an rtf file for scripts that’s the only solution ! Thank you again :slight_smile:

What’s interesting is that both solutions output the same meta tags:

<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1561.6">

And even if the stylesheets and the HTML are slightly different, the pages are rendered exactly the same way in Safari. Pretty cool :slight_smile:

Here is an exemple of htmlization.

https://brandelune.github.io/1112/new%20<textfile.html