Convert AppleScript files to HTML files

I wrote a small script here, which can sometimes be useful… It exports AppleScript type file, chosen by the user, as HTML file on the desktop:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set ramDiskName to "RAM Disk"

-- choose applescript type file
set applescriptPath to POSIX path of (choose file of type {"osas", "applescript", "scptd", "app"})

-- convert applescript file's Posix path to NSURL 
set |applescript| to current application's NSURL's fileURLWithPath:applescriptPath

-- whole HFS path of html file, its basename, and its quoted Posix path
set htmlBaseName to ((|applescript|'s URLByDeletingPathExtension())'s lastPathComponent()) as text
set htmlHFSpath to ((path to desktop folder) as text) & htmlBaseName & ".html"
set htmlPosixPath to quoted form of (POSIX path of htmlHFSpath)

-- create RAM disk to make read/write operations faster
set dCapacity to 512 * 2 * 1000 --512MB
try
	do shell script "diskutil erasevolume HFS+ '" & ¬
		ramDiskName & "' `hdiutil attach -nomount ram://" & (dCapacity as string) & "`"
end try

-- write applescript rich text source to temporary RTF file
set {theScript, theError} to (current application's NSAppleScript's alloc())'s initWithContentsOfURL:|applescript| |error|:(reference)
set theSource to theScript's richTextSource()
set theLength to theSource's |length|()
set theRange to current application's NSRange's NSMakeRange(0, theLength)
set rtfData to theSource's RTFFromRange:theRange documentAttributes:(missing value)
set tempFilePath to "/Volumes/" & ramDiskName & "/Temporary.rtf"
(rtfData's writeToFile:tempFilePath atomically:no)

-- convert RTF to HTML and save at desktop
set tempFilePath to quoted form of tempFilePath
do shell script "/usr/bin/textutil -convert html -output " & htmlPosixPath & space & tempFilePath

-- eject RAM disk
tell application "Finder" to eject ramDiskName

You can avoid those intermediate files like this:

use framework "Foundation"
use framework "AppKit"
use scripting additions

-- choose applescript type file
set applescriptPath to POSIX path of (choose file of type {"osas", "applescript", "scptd", "app"})

-- convert applescript file's Posix path to NSURL 
set |applescript| to current application's NSURL's fileURLWithPath:applescriptPath

set htmlBaseName to (|applescript|'s URLByDeletingPathExtension())'s lastPathComponent()
set htmlHFSpath to ((path to desktop folder) as text) & (htmlBaseName as text) & ".html"
set theDest to current application's NSURL's fileURLWithPath:(POSIX path of htmlHFSpath)
set {theScript, theError} to current application's NSAppleScript's alloc()'s initWithContentsOfURL:|applescript| |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"} -- use this to simplify HTML, instead of next line
set elementsToSkip to {}
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
htmlData's writeToURL:theDest atomically:true

Thanks, Shane, for the best script.

I myself stupidly translated the Objective-C code I found on the net into a hybrid AsObjC. This is one of those times when I don’t wonder if someone else’s code is good enough to just translate it to AppleScript.

I will replace my stupid code with yours, in my scripts collection. :slight_smile:

Just great. When I conceived my script, I had no idea that it could turn from a highly specialized solution into an all-encompassing monster (with your help).

Your script, Fredrik71, lists in the comment the valid document types to export rich text to. This gives me a better idea of what is possible. So thank you too.

Affected lines:

Hi Shane,

This isn’t normally an issue for me but with this particular script, I get two errors when I attempt to run it. The two offending lines (9,13) each generate the following:

Error -1708, nsurl doesn’t understand the “fileURLWithPath_” message.

When compiled, each ‘NSURL’ in these two lines is transformed into ‘nsurl’, so the above lines become this:

[format]set |applescript| to current application’s nsurl’s fileURLWithPath:applescriptPath

set theDest to current application’s nsurl’s fileURLWithPath:(POSIX path of htmlHFSpath)[/format]

Although it makes these changes, the script compiles without error. The uncompiled text retains the original capitalization. However, if I add pipes and edit the case to |NSURL| then the script runs.

This occurs with both Script Editor and Script Debugger (7.0.13). It also occurs when I do a straight copy-paste of the script (using firefox).

Any suggestions or settings that might pre-empt this? I’m running Sierra if that makes a difference. Thanks.

It does, sort of. The term nsurl is defined in the Satimage.osax, and you can’t have two different capitalizations. The solution is to use pipes to keep the capitalization: |NSURL|, as you found.

I used to do it as a matter of course in all scripts, but I do it less these days because third-party scripting additions haven’t been supported for a while.

Thanks. That makes sense. When I suggested that it hadn’t previously been an issue, I imagine it was because any such scripts were likely one of those for which you’d included the pipes already.

Thanks again.