I have a pile of Pages (iWork) documents that I needed to convert to PDF. As near as I can tell, Pages doesn’t support any direct way to invoke its “Export to PDF” feature, and using the Print route to “Save as PDF” has its own well-documented challenges, so I created this script to automate the process using GUI scripting.
The strangest line of the script is “set frontmost to true”, which has a function I cannot describe to you because I don’t know what it is (it was in the AppleScript forum here: http://lists.apple.com/archives/applescript-users/2003/Jun/msg00662.html); the script won’t work reliably without it, though.
Upon execution the script will prompt you for a folder full of Pages documents; it will then, one by one, open each document, and export it as a PDF file to the default location for saving such a thing.
May not be the best way to do this (I welcome suggestions for alternatives!), but it worked for me.
set F to choose folder
tell application "Finder"
set P to (files of entire contents of F)
repeat with I from 1 to number of items in P
set this_item to item I of P as alias
if kind of this_item as string is "Pages Publication" then
tell application "Pages"
open this_item
tell application "System Events"
tell process "Pages"
activate
set frontmost to true
click menu item "Export." of menu 1 of menu bar item "File" of menu bar 1
click button "PDF" of radio group 1 of sheet 1 of window 1
click button "Next..." of sheet 1 of window 1
click button "Export" of sheet 1 of window 1
end tell
end tell
close window 1 without saving
end tell
end if
end repeat
end tell
on open theFiles
tell application "Pages"
repeat with aFile in theFiles
open aFile
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
-- Save file to Desktop.
set docPathAndName to (path to desktop as string) & docName
save front document as ".pdf" in docPathAndName
close front document
end repeat
end tell
end open
A few modifications of the most recent version of the script, required (in my case at least) to make things work. This was tested with Pages '09.
Open Script Editor, copy the script into it, and save as an application. Drag and drop files you want to convert onto the application icon.
Saves converted files to Desktop.
on open theFiles
tell application "Pages"
repeat with aFile in theFiles
open aFile
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
-- Save file to Desktop.
set docPathAndName to (path to desktop as string) & docName
save front document as "SLDocumentTypePDF" in docPathAndName
close front document
end repeat
end tell
end open
Slightly modified version of the script I just posted. This version saves the new file – the PDF – in the same folder as the original document:
on open theFiles
tell application "Pages"
repeat with aFile in theFiles
open aFile
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 aFile) 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
close front document
end repeat
end tell
end open
The problem with all these scripts, except maybe the first one which I haven’t tried, is that pages exports images using the lowest quality. Because I have images in my docs, including a scanned signature, the PDF looks horrible.
If I manually export, the high-quality pdf export is the default works, but I cant see how to automate the export of pdf using pages high quality options.
Installing cups-pdf http://www.codepoetry.net/projects/cups-pdf-for-mosx, creating a virtual printer named “Virtual Printer” and
peplacing the pdf export of the file with this will yield a high-quality pdf document.
set printOpts to {target printer:“Virtual Printer”}
tell application “Pages”
repeat with aFile in theFiles
open aFile
print front document with properties printOpts
close front document saving no
end repeat
end tell
–end open
Sorry for performing a bit of thread necromancy here, but it seemed relevant to this discussion enough to post here.
Any tips on getting this to work with Numbers and Keynote files? I already found out that if you change .pdf out for .doc and swap SLDocumentTypePDF for SLDocumentTypeMSWord it works fine to convert Pages documents to Word documents.
The code listed by ct77 is working for Pages to .doc and .pdf. If I swap Pages for Keynote and change other things as appropriate it gets to the line which starts "save front document" and gives me an error stating it cannot save the document in that format, I've tried a number of options for the string there with no success.
Trying the code to work on Numbers met with even less success than with Keynote, throwing an error message at the line beginning "set docName to" stating that it "couldn't find document 1".
Any help you guys can provide is greatly appreciated.
I have made this for Exporting Numbers '09 documents to Excel format, using GUI and the Export function.
If you want PDF’s instead of Excel, then change the two lines in the clickExcelButton handler, from button 2 to 1.
– click radio button 2 of radio group 1 of sheet 1 of window theWindow
and
– click checkbox 2 of radio group 1 of sheet 1 of window theWindow
This script should work in both Mac OS X 10.5 and Mac OS X 10.6.
tell application "Numbers"
set theWindow to name of document 1
end tell
tell application "System Events"
tell process "Numbers"
activate
set frontmost to true
click menu item 11 of menu 1 of menu bar item 3 of menu bar 1
my clickExcelButton(theWindow)
click button 1 of sheet 1 of window theWindow
click button 1 of sheet 1 of window theWindow
end tell
end tell
on clickExcelButton(theWindow)
tell (system info) to set currentOSVersion to get system version
if currentOSVersion begins with "10.6" then
tell application "System Events"
tell process "Numbers"
click radio button 2 of radio group 1 of sheet 1 of window theWindow
end tell
end tell
else
tell application "System Events"
tell process "Numbers"
click checkbox 2 of radio group 1 of sheet 1 of window theWindow
end tell
end tell
end if
end clickExcelButton
I have also an “extension” of this script, that can close the document, when done.
Just add this to the buttom of the script, it will check the CPU usage of Numbers, and when usage drops below theLimit it can do something, in this close the document without saving.
set theLimit to 10
repeat
tell application "System Events"
set PID to unix id of process "Numbers"
set theCPU to do shell script "ps aux | grep " & PID & " | grep -v grep | awk '{print $3}'"
end tell
if theCPU < theLimit then
tell application "Numbers"
close the document theWindow without saving
end tell
exit repeat
end if
end repeat