automatically convert Pages to PDF

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

automatically convert Pages to PDF Sun May 06, 2012 8:55 pm • by ecormany
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…
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.
ecormany
 
Posts: 28
Joined: Wed Apr 25, 2012 6:15 pm

Re: automatically convert Pages to PDF Tue Dec 10, 2013 7:05 pm • by ecormany
this script broke when i updated to iWork '13. i finally banged my head against a wall long enough to make the minor changes so it works once again!

Code: Select all
tell application "Pages"
   open theFile
   set theFront to front document
   set docName to name of theFront
   -- 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 alias)) as Unicode text
   end tell -- Finder
   -- Save file to folder that dropped file exists in.
   set docPathAndName to sourceFolder & docName
   export theFront to file docPathAndName as PDF
end tell
ecormany
 
Posts: 28
Joined: Wed Apr 25, 2012 6:15 pm

Re: automatically convert Pages to PDF Sun May 25, 2014 1:54 pm • by briandiehl
This is a F A N T A S T I C rule. Way to be for dreaming this masterpiece up! I am having one major issue with it though - when I run it, it outputs PDFs with single capital letters as their titles.

Also, I am curious if there is a way to cause this to delineate between pages '09 and pages '13 documents? Pages '13 has a tendency for messing with my old Page Layout files.
briandiehl
 
Posts: 1
Joined: Sun May 25, 2014 10:13 am

Re: automatically convert Pages to PDF Tue Dec 23, 2014 12:03 am • by fiamh
Does this still work? My documents get converted to [First letter of the doc].pdf (e.g., 'H.pdf'). Seeing ` set docName to first text item of docName & ".pdf"` I would guess that setting the delimiter to `.pages` no longer works?

Best, Oliver
fiamh
 
Posts: 1
Joined: Tue Dec 23, 2014 12:00 am

Re: automatically convert Pages to PDF Sat Feb 07, 2015 6:36 pm • by bernstein
Hi there,

thanks you for this workflow.

I made a little change in the applescript to get it working with Pages 5.5.2

Code: Select all
tell application "Pages"
   open theFile
   set theFront to front document
   set docName to name of theFront
   -- 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 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 alias)) as Unicode text
   end tell -- Finder
   -- Save file to folder that dropped file exists in.
   set docPathAndName to sourceFolder & docName
   export theFront to file docPathAndName as PDF
end tell


Cya
bernstein
 
Posts: 1
Joined: Sat Feb 07, 2015 6:31 pm

Re: automatically convert Pages to PDF Sun Feb 14, 2016 1:05 pm • by james442
Does this still work? My documents get converted to [First letter of the doc].pdf (e.g., 'H.pdf'). Seeing ` set docName to first text item of docName & ".pdf"` I would guess that setting the delimiter to `.pages` no longer works?


Thanks for sharing this topic.
james442
 
Posts: 1
Joined: Sat Feb 13, 2016 1:49 pm

Re: automatically convert Pages to PDF Tue Mar 01, 2016 5:44 am • by dnouls
Is Pages still supporting AppleScript automation ? Whatever I tried it does not respond at all.
Are there alternatives out there to automate conversion from Pages to PDF and Word ?

2016-03-01 10:35:20.580 hazelworker[16439] Processing folder Facturen Uit (forced)
2016-03-01 10:35:22.724 hazelworker[16439] 2016002 Onkosten.pages: Rule Convert Pages to PDF matched.
2016-03-01 10:37:22.797 hazelworker[16439] [Error] AppleScript failed: Error executing AppleScript on file /Users/home/Documents/Firma/2016/Facturen Uit/2016002 Onkosten.pages.
2016-03-01 10:37:22.798 hazelworker[16439] OSAScript error: {
NSLocalizedDescription = "Pages got an error: AppleEvent timed out.";
NSLocalizedFailureReason = "AppleEvent timed out.";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,424424 \"Pages\"]>";
OSAScriptErrorAppNameKey = Pages;
OSAScriptErrorBriefMessageKey = "AppleEvent timed out.";
OSAScriptErrorMessageKey = "Pages got an error: AppleEvent timed out.";
OSAScriptErrorNumberKey = "-1712";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
2016-03-01 10:37:22.801 hazelworker[16439] Received abort event.

The script is not even trying to start Pages. And even if I try to start it manually before running the script it still fails with the same result.

My god these AppleScript things are bloody difficult to create. So little good documentation, such a bloated syntax.
dnouls
 
Posts: 1
Joined: Tue Mar 01, 2016 5:39 am

Re: automatically convert Pages to PDF Tue Mar 01, 2016 12:50 pm • by Mr_Noodle
I think Apple cut out a lot of AppleScript support at some point back when they were crippling iWork to make it more in line with their iOS equivalents. I was under the impression most of that was restored in more recent versions. You may want to double-check the version you are running.
Mr_Noodle
Site Admin
 
Posts: 8219
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: automatically convert Pages to PDF Wed May 03, 2017 1:26 am • by jayhalleaux
Code: Select all
set _document to theFile

tell application "Finder"
   set _directory to get container of file _document
   
   set _documentName to name of _document
   if _documentName ends with ".pages" then ¬
      set _documentName to text 1 thru -7 of _documentName
   
   set _PDFName to _documentName & ".pdf"
   set _location to (_directory as string) & _PDFName
end tell

tell application "Pages"
   activate
   open _document
   
   with timeout of 1200 seconds
      export front document to file _location as PDF
   end timeout
   
   close front document
end tell


this worked for me
jayhalleaux
 
Posts: 6
Joined: Sat Apr 29, 2017 2:41 am

Re: automatically convert Pages to PDF Wed Jul 19, 2017 2:15 pm • by beaucrosetto
I an new to all of this and AppleScript - how do I use an AppleScript to create a rule in hazel? Can someone link me to a tutorial
beaucrosetto
 
Posts: 1
Joined: Wed Jul 19, 2017 2:13 pm

Re: automatically convert Pages to PDF Fri Aug 04, 2017 1:00 pm • by BrianGa
beaucrosetto wrote:I an new to all of this and AppleScript - how do I use an AppleScript to create a rule in hazel? Can someone link me to a tutorial


Good question Beau, any tips for a newbie or links to some good tuts?
BrianGa
 
Posts: 1
Joined: Sat Jul 29, 2017 7:06 am


Return to Tips & Tricks - DO NOT POST QUESTIONS