Pages to PDF Quick Action

I have found a Pages to PDF script on the community forums by VikingOSX, and adapted it to suit Quick Actions. Pages to PDF script - Apple Community


I am new to AppleScript, so please forgive any errors.


-- Pages2PDF.applescript
-- Select file(s), right click, select quick actions, and "Pages to PDF".
-- Requires Pages v5.6 or later thru Pages 10 (tested)
-- Tested: macOS 10.14.6, 10.15.4, 14.1
-- VikingOSX, 2020-05-04, threomc, 2023-11-14, Apple Support Communities, No warranties expressed or implied


use scripting additions


property valid_kind : {"Pages Publication", "Pages document"}
property fileCnt : (0 as integer)
property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"


on run {input, parameters}
    -- Check Pages version
    tell application "Pages" to set aversion to its version
    considering numeric strings
        if aversion < "5.6" then
            display alert "You need Pages v5.6 or later to run this script" giving up after 10
            return
        end if
    end considering
    
    set fileCnt to 0
    
    repeat with anItem in input
        try
            tell application "Finder"
                set akind to kind of anItem
                if akind is equal to "Folder" then
                    set docList to (every item in entire contents of folder anItem ¬
                        whose kind is in {"Pages Publication", "Pages document"}) as alias list
                    repeat with afile in docList
                        my exportFile(afile)
                    end repeat
                else if akind is in {"Pages Publication", "Pages document"} then
                    my exportFile(anItem)
                end if
            end tell
        on error errmsg number errnbr
            my errorHandler(errmsg, errnbr, "Dropped Items repeat - " & (anItem as text))
            return
        end try
    end repeat
    
    display dialog "Export Complete." & return & tab & "Files exported to PDF: " & (fileCnt as text) ¬
        buttons {"Done"} default button "Done" with title "Pages Export Facility"
    
    -- perform these cleanup items, and then really quit
    tell application "Pages" to if it is running then quit
    set fileCnt to (0 as integer)
    set exportDocument to ""


    return input
end run


on exportFile(afile)
    -- switch extension from .pages to .pdf
    set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ".pages"}
    set exportDocument to item 1 of text items of (afile as alias as text) & ".pdf"
    set AppleScript's text item delimiters to TID
    
    close access (open for access exportDocument)
    tell application "Pages"
        try
            set myDoc to open afile
            with timeout of 1800 seconds
                if (version of it) ≥ "5.6" then
                    export myDoc to file exportDocument as PDF with properties {image quality:Best}
                else
                    export myDoc to file exportDocument as PDF
                end if
            end timeout
            set fileCnt to (fileCnt + 1) as integer
            close myDoc saving no
        on error errmsg number errnbr
            my errorHandler(errmsg, errnbr, "Export file")
            return
        end try
    end tell
    
    -- force extension visibility on exported PDF
    tell application "Finder"
        if exists (item exportDocument as alias) then
            set extension hidden of (item exportDocument as alias) to false
        end if
    end tell
    
    return
end exportFile


on errorHandler(msg, nbr, handlerName)
    display alert handlerName & ": " & "[ " & nbr & " ] " & msg as critical giving up after 10
end errorHandler


The automator setup on macOS 14.1 Sonoma.


Posted on Nov 14, 2023 2:10 AM

Reply
Question marked as Best reply

Posted on Nov 14, 2023 5:38 AM

There are more recent iterations of this script that display a tally of exported files, etc. The attached was just successfully tested this morning on macOS Sonoma 14.1.1 with Pages v13.2 with individual and a dropped folder of Pages documents.


I have now begun including the requirement statement that the script is developed on an English language installation of macOS and cannot automatically perform translations for non-English language systems. That may result in a script not working as expected.






Similar questions

1 reply
Question marked as Best reply

Nov 14, 2023 5:38 AM in response to threomc

There are more recent iterations of this script that display a tally of exported files, etc. The attached was just successfully tested this morning on macOS Sonoma 14.1.1 with Pages v13.2 with individual and a dropped folder of Pages documents.


I have now begun including the requirement statement that the script is developed on an English language installation of macOS and cannot automatically perform translations for non-English language systems. That may result in a script not working as expected.






Pages to PDF Quick Action

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