Sunday, June 13, 2021

#1 2021-05-16 04:33:34 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1878

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:

Applescript:


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

Last edited by KniazidisR (2021-05-16 05:32:07 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB


Filed under: applescript, HTML

Offline

 

#2 2021-05-16 06:15:56 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6651

Re: Convert AppleScript files to HTML files

You can avoid those intermediate files like this:

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2021-05-16 07:02:50 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1878

Re: Convert AppleScript files to HTML files

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. smile

Last edited by KniazidisR (2021-05-16 07:17:44 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#4 2021-05-16 11:17:11 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 798

Re: Convert AppleScript files to HTML files

Here is one that use @Shanes code to output webarchive instead. smile

Applescript:

use framework "Foundation"
use scripting additions

set thePath to POSIX path of (choose file of type {"osas", "applescript", "scptd", "app"})
set theURL to current application's |NSURL|'s fileURLWithPath:thePath
set theBasename to (theURL's URLByDeletingPathExtension())'s lastPathComponent()

set htmlSavePath to POSIX path of (path to desktop) & (theBasename as text) & ".webarchive"
set htmlSaveURL to current application's |NSURL|'s fileURLWithPath:htmlSavePath

set {theScript, theError} to current application's NSAppleScript's alloc()'s initWithContentsOfURL:theURL |error|:(reference)
if theScript is missing value then error theError's localizedDescription() as text

set attributeString to theScript's richTextSource()
set elementsToSkip to {}
(**
* NSAttributedStringDocumentType:
*    NSDocFormatTextDocumentType, NSHTMLTextDocumentType, NSMacSimpleTextDocumentType
*    NSMacSimpleTextDocumentType, NSOpenDocumentTextDocumentType, NSPlainTextDocumentType
*    NSRTFTextDocumentType, NSRTFDTextDocumentType, NSWebArchiveTextDocumentType
*    NSWordMLTextDocumentType
*)

set theDict to current application's NSDictionary's dictionaryWithObjects:{current application's NSWebArchiveTextDocumentType, elementsToSkip} forKeys:{current application's NSDocumentTypeDocumentAttribute, current application's NSExcludedElementsDocumentAttribute}

set {htmlData, theError} to attributeString's dataFromRange:{0, attributeString's |length|()} documentAttributes:theDict |error|:(reference)
if htmlData is missing value then error theError's locallizedDescription() as text
htmlData's writeToURL:htmlSaveURL atomically:true


if you are the expert, who will you call if its not your imagination.

Online

 

#5 2021-05-16 12:13:10 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1878

Re: Convert AppleScript files to HTML files

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.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)