use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
-- classes, constants, and enums used
property NSAutoPagination : a reference to 0
property NSClipPagination : a reference to 2
property NSThread : a reference to current application's NSThread
property NSPrintJobSavingURL : a reference to current application's NSPrintJobSavingURL
property NSPrintOperation : a reference to current application's NSPrintOperation
property NSPrintSaveJob : a reference to current application's NSPrintSaveJob
property |NSURL| : a reference to current application's |NSURL|
property NSString : a reference to current application's NSString
property NSTextView : a reference to current application's NSTextView
property NSPrintInfo : a reference to current application's NSPrintInfo
property NSAttributedString : a reference to current application's NSAttributedString
property NSData : a reference to current application's NSData
property NSUUID : a reference to current application's NSUUID
property NSDictionary : a reference to current application's NSDictionary
property theResult : false -- whether it succeeded or not
set thePath to POSIX path of (choose file of type {"public.html"})
set thePath to NSString's stringWithString:thePath
set newPath to thePath's stringByDeletingPathExtension()'s stringByAppendingPathExtension:"pdf"
set styledText to my styledTextFromHTMLFile:thePath
my saveStyledText:styledText asPDFToFile:newPath
on styledTextFromHTMLFile:thePath
set theData to NSData's dataWithContentsOfFile:thePath
set attStr to NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:(missing value)
return attStr
end styledTextFromHTMLFile:
on saveStyledText:styledText asPDFToFile:newPath
-- create print info for saving to file
set destURL to |NSURL|'s fileURLWithPath:newPath
set printInfo to NSPrintInfo's alloc()'s initWithDictionary:(NSDictionary's dictionaryWithObject:destURL forKey:(NSPrintJobSavingURL)) -- sets destination
printInfo's setJobDisposition:NSPrintSaveJob -- save to file job
printInfo's setHorizontalPagination:NSClipPagination
printInfo's setVerticalPagination:NSAutoPagination
-- get page size and margins
set pageSize to printInfo's paperSize()
set theLeft to printInfo's leftMargin()
set theRight to printInfo's rightMargin()
set theTop to printInfo's topMargin()
-- make a very deep text view
set theView to NSTextView's alloc()'s initWithFrame:{{0, 0}, {(pageSize's width) - theLeft - theRight, 3.0E+38}}
theView's setHorizontallyResizable:false
-- put in the text
theView's textStorage()'s setAttributedString:styledText
-- size to fit; must be done on the main thread
if NSThread's isMainThread() then
theView's sizeToFit()
else
theView's performSelectorOnMainThread:"sizeToFit" withObject:(missing value) waitUntilDone:true
end if
-- create print operation and run it
set printOp to NSPrintOperation's printOperationWithView:theView printInfo:printInfo
printOp's setShowsPrintPanel:false
printOp's setShowsProgressPanel:false
if NSThread's isMainThread() then
set my theResult to printOp's runOperation()
else
my performSelectorOnMainThread:"runPrintOperation:" withObject:printOp waitUntilDone:true
end if
end saveStyledText:asPDFToFile:
on runPrintOperation:printOp -- on main thread
set my theResult to printOp's runOperation()
end runPrintOperation: