Q: Pages v5.6: AppleScript: How to use new export best
Pages v5.6 just introduced additional AppleScript support so that one can specify Good, Better, or Best on exports done from AppleScript. Thus, with the following compiled and parked on your Desktop, you can perform a file open on any Pages document, and instantly export it as the quality of PDF that you need, right back to the original file location. The script defaults to Best. It does not support drag and drop at this time.
Steps:
- Copy/paste the AppleScript into your Application/Utilites/Script Editor
- Click the toolbar hammer icon. No errors will create multi-color text
- Save…
- Something nmemonic as pages2PDF
- File Format: Text
- Line Endings: no change
- Keep changes in original document: unchecked
- Hide Extension: unchecked
- Toss it in your Documents folder
- Option+File menu ▸ Save As…
- Same first name
- File Format: Application
- Options: Both unchecked
- Keep changes in original document: unchecked
- Hide Extension: checked
- Save it to your Desktop.
Usage: Double-click the application icon on your Desktop. A file chooser will open and only permit Pages documents for selection. The application will ensure that the output PDF has a file extension visible, and will write the PDF in the same location, as the selected Pages document. A Finder window will open with the resulting PDF selected.
AppleScript
<--- copy below --->
-- pages2PDF.applescript
-- PDF written back to the original Pages document location
-- v1.0
-- VikingOSX, Oct. 2015, Apple Support Community
property valid_Pages : {"com.apple.iwork.pages.sffpages", "com.apple.iwork.pages.pages"}
property default_loc : ((path to desktop) as text) as alias
try
set theFile to (choose file of type valid_Pages with prompt "Choose a Pages document:" showing package contents no default location default_loc without invisibles)
if result = {button returned:"Cancel"} then error -128
on error errmsg number errnbr
error_handler(errnbr, errmsg)
return
end try
set pf_info to {package folder} of (info for theFile) as boolean
if false is in pf_info then
set exportDocument to text 1 thru -6 of (theFile as text) & "pdf"
else
-- package folders extensions end in ':'
set exportDocument to text 1 thru -7 of (theFile as text) & "pdf"
end if
tell application "Pages"
activate
try
set mydoc to open theFile
with timeout of 1200 seconds
export mydoc to file exportDocument as PDF with properties {image quality:Best}
end timeout
close mydoc saving no
on error errmsg number errnbr
error_handler(errnbr, errmsg)
tell application "Pages" to if it is running then quit
quit
end try
end tell
tell application "Finder"
set extension hidden of (file exportDocument as alias) to false
reveal (file exportDocument as alias)
end tell
return
on error_handler(nbr, msg)
return display alert "[ " & nbr & " ] " & msg as critical giving up after 10
end error_handler
Posted on Oct 15, 2015 8:20 PM