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:

  1. Copy/paste the AppleScript into your Application/Utilites/Script Editor
  2. Click the toolbar hammer icon. No errors will create multi-color text
  3. Save…
    1. Something nmemonic as pages2PDF
    2. File Format: Text
    3. Line Endings: no change
    4. Keep changes in original document: unchecked
    5. Hide Extension: unchecked
    6. Toss it in your Documents folder
  4. Option+File menu ▸ Save As…
    1. Same first name
    2. File Format: Application
    3. Options: Both unchecked
    4. Keep changes in original document: unchecked
    5. Hide Extension: checked
    6. 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 fileof typevalid_Pageswith prompt "Choose a Pages document:" showing package contentsnodefault locationdefault_loc without invisibles)


if result = {button returned:"Cancel"} then error -128

on error errmsgnumbererrnbr


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 opentheFile


with timeout of 1200 seconds


exportmydoctofileexportDocumentasPDFwith properties {image quality:Best}

end timeout



closemydocsavingno

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 (fileexportDocument as alias) to false


reveal (fileexportDocument 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

Reply
2 replies

Sep 2, 2016 7:49 AM in response to f.marcel

The original interactive-only script processed a single file at a time. The following AppleScript is non-interactive, and allows you to drop any number of Pages documents, and/or folders of Pages documents on it. The script will check for the presence of an Exported_PDF folder on your Desktop, and make one if it does not exist. This is where all PDF documents are exported too.


If you are using Pages v5.6 or later, it will force PDF export quality to Best, and otherwise you will get the default PDF quality. It also stamps a Finder comment in the PDF displaying the version of Pages that created it, and the PDF quality.


I have restricted this script to OS X 10.10 and later, as I have not tested it on Mavericks or ea


  1. Copy the script and paste it into your Script Editor (Launchpad : Other : Script Editor).
  2. Compile the script (hammer icon), and initially, save as an AppleScript source document
    1. Name: pages2pdf
    2. Save it to your Documents folder
    3. File Format: Text
    4. Save
  3. Press the option key, and then select File menu : Save As...
    1. Name: pages2pdf
    2. Save it to your Desktop
    3. File Format: Application
    4. Check Hide Extension
    5. Save


AppleScript

-- copy below --

-- pages2pdf.applescript

-- Drag & Drop application that exports Pages documents to PDF

-- Pages v5.6 and above will export as PDF Best

-- Forces file extensions to show in Finder. Adds Finder comment to PDF with version, quality

-- Version 1.1. Tested with Pages v5.6.2 on OS X 10.11.6.

-- VikingOSX, Sept 2016, Apple Support Community. No warranty expressed or implied.


use scripting additions

use AppleScriptversion "2.4" -- Yosemite or later


-- export PDF documents to this folder location

property pdf_folder : ((path to desktop) as text) & "Exported_PDF:"

property pages_kind : {"Pages Publication"}


on opendropped_items


tell application "Pages" to set pages_version to its version

if pages_version < "5.5" then

display alert "You must be using Pages v5.5 or later, and Yosemite or later." giving up after 10

quit

end if


tell application "Finder"


-- As long as an export folder exists we won't prompt user more than once to create it

if not (exists folder pdf_folder) = true then

set folder_name to text (offset of "Exported_PDF" in pdf_folder) thru -2 of pdf_folder


makenewfolderat (path todesktop) with properties {name:folder_name}

end if

end tell


repeat with anItem in dropped_items

set aFile to (anItem as alias)

tell application "System Events" to set {akind, pf} to {kind, package folder} of aFile

if akind contains "Folder" and pf is false then

tell application "Finder"

set pagesList to (every item in entire contents of folder aFile whose kind is in pages_kind) as alias list

end tell

repeat with pages_file in pagesList


export_file(pages_file)

end repeat

else if akind is in pages_kind then


export_file(aFile)

end if

end repeat

tell application "Pages" to if it is running then quit


delay 2

display dialog "PDF Export processing is complete" giving up after 10

return

end open


on export_file(theFile)


tell application "System Events" to set aname to name of theFile


-- construct output file path and name

set exportDocument to (pdf_folder as text) & text 1 thru -6 of (aname as text) & "pdf"


tell application "Pages"


set export_best to (Best as constant)

set finder_comment to "Pages: " & its version

try

set mydoc to open theFile

with timeout of 600 seconds

if its version ≥ "5.6" then

set finder_comment to finder_comment & " PDF Best"


exportmydoctofileexportDocumentasPDFwith properties {image quality:export_best}

else

set finder_comment to finder_comment & " PDF Default Quality"


exportmydoctofileexportDocumentasPDF

end if

end timeout


closemydocsavingno

on error errmsg number errnbr

my error_handler(errnbr, errmsg)

tell application "Pages" to if it is running then quit

return

end try

end tell


tell application "Finder"


-- make extension visible

set extension hidden of fileexportDocument to false

set comment of fileexportDocument to finder_comment

end tell


return

end export_file


on error_handler(nbr, msg)

return display alert "[ " & nbr & " ] " & msg as critical giving up after 10

end error_handler

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Pages v5.6: AppleScript: How to use new export best

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.