« More great news from Acrobat... | Main | R.I.P. John M. Ford »

The beginnings of a "Create PDF from E'rage Message" script

Okay, so when I see where Adobe has yet to make it easy to build PDFs from Entourage messages, (or Mail messages for that matter), it bugs me. Yes, I know, you can't make the happy little buttons in Entourage, and it's not easy to do it in Mail, (if you even can at all), but that's not the same as it being impossible. I mean, come on, this is so not just an Outlook thing.

First, Entourage can show you the source of a message, which means you can parse it for HTML and the like. Hell, E'rage will tell you outright if a message has HTML. It's in the Entourage Mail and News Suite. Whether it does or doesn't, it's child's play to get the source of the currently selected message in Entourage. Literally. Two lines for the tell block, one to get the currently selected message, one to get the source of the currently selected message.

In fact, the biggest stumbling block is (shock), Acrobat, whose AppleScript Dictionary blows, and you should really just use Javascript for Acrobat. Partially because that's just the best language for scripting Acrobat. Well, almost entirely because of that. But luckily, Acrobat's AppleScript dictionary does allow you to execute a Javascript. So you can eventually get there from here. (Does anyone out there with more experience on the VBA side of things know if the VBA implementation in Acrobat for Windows is as poor as the AppleScript implementation is in Acrobat for OS X? Either way, a quick comment with your opinion of the VBA implementation would be appreciated.) Yes, it's also possible I fubared the capture web page statement. That would then be an ID10T error on my part, however, the Acrobat dictionary still blows, and you should use Javascript, because in Acrobat, Javascript does not blow.

Anyway, I was playing with this a bit, and here's the source. The reason I was playing with this was because I was earlier playing with generating PDFs from Word files converted to HTML. Yes, Word's HTML sucks, but that's not the point. The point behind that was to see if I could generate, from Word 2004 on Mac OS X, a better PDF than the Adobe Macros can. You know, the ones that haven't improved in a dog's age, because Adobe says it can't happen until Office gives them more access? Yeah, guess what. Just by saving as HTML, then using Acrobat's ability to create a PDF, I was able to create a PDF from Word in Acrobat with live Internet URLs. Wow. Looks like some improvement is possible. Because it's not like Adobe wouldn't even try to improve them even slightly if it were possible? Say with a button that exported it out to HTML in a temp file. That button would then kick off an AppleScript which ran an Acrobat Javascript which then read that HTML, (as crappy as Word's HTML is, it's consistently crappy, which means you can work around it), and created a PDF with more structured features than you currently get out of the "LOOK! I MADE A PDF, YAAAAY!" that are the Adobe macros.

There's just no way that would work at all as a way of saying, "Look, it's not as good as what you get on the Windows side, but it's something of an improvement." Or maybe looking at what you can pull out of a Word file via AppleScript and creating a javascript temp file from it which Acrobat could then run and build a PDF. No, that must be impossible too. There's no way to get all the hyperlink objects of a document. Or get the TOC entries and the pages they link to from the tables of contents of a document so that you could then make those links live in the PDF. Or figure tables. I mean, again, I'm not expecting 100% compatibility. But, some improvement in the last six years or so would be nice.

'Cause there's just no way that the Acrobat team would refuse to do something unless they could do it the exact same way on both platforms, right? That'd be really silly and something you expect from an inexperienced intern, not a multinational made up of mature grownups focused on delivering the best possible solutions for Acrobat customers using Office 2004.

So anyway, about that E'rage script. Now, I did this based on one message, a Classmates.com message. So there's a lot of stuff that would be inappropriate in a more general script. But, you can indeed dump that source to a file that Acrobat can build a PDF of. It's not perfect, but it's not impossible either. Stick it in the E'rage script menu, and you'd have a spiffy damned solution. I'm a tad bit too busy to finish it, but please, take it and have fun with it. Make it rock, then, if you could, give it away. Even my good friends at Adobe, please, feel free to use this. I know you guys meant to do this, but have just been too busy to get it out there.

tell application "Microsoft Entourage"
     set theMessageList to the selection
     repeat with x in theMessageList
          set theMessageSource to source of x
          set theHTMLStart to offset of "<html>" in theMessageSource
          set theHTMLStop to offset of "</html>" in theMessageSource
          if theHTMLStop = 0 and theHTMLStart0 then
               set theBadHTMLTagFlag to true
          else
               set theBadHTMLTagFlag to false
          end if
     end repeat
end tell

set theSystemInfo to system info
set theHomeDir to home directory of theSystemInfo as text
set theDocumentsFolder to theHomeDir & "Documents:"

try
     
set theTempFolder to (theDocumentsFolder & "erage2pdf:" as alias)
     on error theErrorMessage number theErrorNumber --test the error
     if theErrorNumber = -43 then ---43 is the error number for folder doesn't exist, so that's the only time we want to create it
          --
you can add code for other errors if you like
          tell application "Finder"
          make new folder at (theDocumentsFolder as alias) with properties {name:"erage2pdf"} --make the folder
          end tell
     end if
end try

set theTempFolder to (theDocumentsFolder & "erage2pdf:" as text)
set theTempFilePath to (theTempFolder & "messageTempFile" & (time of (current date) as text) & ".html")
set theTempMessageFile to open for access theTempFilePath with write permission
write theMessageSource to theTempMessageFile
set theTempContents to read theTempMessageFile from theHTMLStart
set eof theTempMessageFile to 0
write theTempContents to theTempMessageFile

if theBadHTMLTagFlag then
     set theTempMessageFileEOF to get eof theTempMessageFile
     write "</html>" to theTempMessageFile
end if

close access theTempMessageFile

tell application "Finder"
set theTempFileURL to URL of (theTempFilePath as alias)
end tell

set theTempFileURL to theTempFileURL

tell application "Adobe Acrobat 7.0 Professional"
     set thePDFConvert to capture web page theTempFileURL into New Document
end tell

Technorati Tags: , , , , , , ,

Categories:     Applescript, Entourage X Scripts, Mac OS X Scripts
Posted by John C. Welch at 22:31 | Permalink



Comments

Warning for Notes users: The commenting system uses HTML.
I know this will be scary for some of you, especially Notes fans. However, open standards, rah-rah.
If you want to use less-than or greater-than signs, or other similar characters that HTML reserves,
you'll simply have to learn to do it the HTML way. Luckily, HTML is kind of popular, no matter what
your re-educators have told you, and you can easily find help on the intertubes.
digital.forest Where Internet solutions grow

There, a PayPal Button.

Bing
About the Author
How I do stuff on this site
Family
The Artwork of Melissa Findley
Diane Francis @ the National Post Eric Francis @ the Calgary Sun

BUY MY BOOK! BUY MY BOOK!
Non-DRM eBook PDF:
Get it direct from Peachpit!

Kindle Version:


Dead Tree Version:


Apple Amazon Links
Mac OS X Server 10.6 Snow Leopard

Mac OS X 10.6 Snow Leopard

Mac OS X 10.6 Snow Leopard Family Pack (5-User)

Amazon Book Links
Legacy of Ashes: The History of the CIA

The Donnas: Bitchin'

Wizards at War (The Young Wizards, Book 8)

The Demon's Sermon on the Martial Arts

The Collected Stories of Arthur C. Clarke

JavaScript and Ajax for the Web, Sixth Edition

Awakening Warrior: Revolution in the Ethics of Warfare

FOB Links

Mac Web Writers

Techie Links

Review Victims