The question came up elsewhere, of how to make PDFs of from OpenOffice docs. This is a quick-and-dirty way to make a PDF of any document TextEdit can open. It has some limitations, but I figure it might be useful for someone anyway.
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
my makePDFFrom:(POSIX path of (choose file with prompt "Choose a file TextEdit can open"))
on makePDFFrom:posixPath
-- make URL and build destination path
set theURL to current application's |NSURL|'s fileURLWithPath:posixPath
set destPath to theURL's |path|()'s stringByDeletingPathExtension()'s stringByAppendingPathExtension:"pdf"
-- get doc's contents as styled text
set {styledText, theError} to current application's NSAttributedString's alloc()'s initWithURL:theURL options:(missing value) documentAttributes:(missing value) |error|:(reference)
if styledText = missing value then error (theError's localizedDescription() as text)
-- set up printing specs
set printInf to current application's NSPrintInfo's sharedPrintInfo()'s |copy|()
printInf's setJobDisposition:(current application's NSPrintSaveJob)
printInf's |dictionary|()'s setObject:destPath forKey:(current application's NSPrintSavePath)
-- get dimensions of print area; you could instead set these
set theSize to printInf's paperSize()
set theLeft to printInf's leftMargin()
set theRight to printInf's rightMargin()
set theTop to printInf's topMargin()
set theBottom to printInf's bottomMargin()
-- make text view and add text
set theView to current application's NSTextView's alloc()'s initWithFrame:{{0, 0}, {(width of theSize) - theLeft - theRight, (height of theSize) - theTop - theBottom}}
theView's textStorage()'s setAttributedString:styledText
-- set up and run print operation without showing dialog
set theOp to current application's NSPrintOperation's printOperationWithView:theView printInfo:printInf
theOp's setShowsPrintPanel:false
theOp's setShowsProgressPanel:false
theOp's runOperation()
end makePDFFrom: