automatically convert Pages to PDF

this is probably my most-used Hazel rule, and what convinced me to buy a license. it watches for any newly modified Pages documents and immediately creates a PDF copy using AppleScript. some important things to know about how this operates:
1. uses AppleScript and will open documents that are not already open. most of the time the rule applies to a document you have open and have just saved, so this isn't a big problem.
2. overwrites previous PDF copy in place without asking. if you want to keep a history of the PDF that you can roll back, i suggest running this within a folder that syncs to Dropbox.
3. apparently does not handle bitmap images well. this is a limitation in Pages for which i haven't found a workaround. due to a stupid bug, using a virtual printer instead of Pages' AppleScript conversion tool just doesn't work.
4. saves the PDF in the same folder with the same name, different extension. you can do renaming or re-filing by adding further actions.
that said, here's the rule…
…and script.
when you first implement this rule, if you don't want Pages to go nuts and open every document in the folder you apply this to and start spewing out a ton of PDFs, make sure that all of your Pages documents in that folder have an up-to-date Date Matched parameter. you can do that by running a dummy rule that does Ignore on all Pages Publications, and then turning the dummy rule off.
1. uses AppleScript and will open documents that are not already open. most of the time the rule applies to a document you have open and have just saved, so this isn't a big problem.
2. overwrites previous PDF copy in place without asking. if you want to keep a history of the PDF that you can roll back, i suggest running this within a folder that syncs to Dropbox.
3. apparently does not handle bitmap images well. this is a limitation in Pages for which i haven't found a workaround. due to a stupid bug, using a virtual printer instead of Pages' AppleScript conversion tool just doesn't work.
4. saves the PDF in the same folder with the same name, different extension. you can do renaming or re-filing by adding further actions.
that said, here's the rule…
- Code: Select all
if all of the following conditions are met
Kind is Pages Publication
Date Last Modified is after Date Last Matched
do the following
Run AppleScript [see below]
Send Growl notification with pattern [file] PDF'd
…and script.
- Code: Select all
tell application "Pages"
open theFile
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .pdf extension.
set docName to first text item of docName & ".pdf"
set AppleScript's text item delimiters to prevTIDs
-- Get folder that dropped file exists in.
tell application "Finder"
set sourceFolder to (container of theFile) as Unicode text
end tell -- Finder
-- Save file to folder that dropped file exists in.
set docPathAndName to sourceFolder & docName
save front document as "SLDocumentTypePDF" in docPathAndName
end tell
when you first implement this rule, if you don't want Pages to go nuts and open every document in the folder you apply this to and start spewing out a ton of PDFs, make sure that all of your Pages documents in that folder have an up-to-date Date Matched parameter. you can do that by running a dummy rule that does Ignore on all Pages Publications, and then turning the dummy rule off.