Regarding AppleScript, is it possible to change “page layout” export option when exporting Numbers documents to PDF

applescriptnumberspdfsettings

A little while ago I cobbled together a short AppleScript for exporting multiple Pages documents to PDF at one time, and now I want to do the same with Numbers. The basic principle is the same and the script basically just carries over EXCEPT the actual exporting to PDF part.

here is an excerpt:

tell application "Numbers"
    export front document to file exportFile as PDF with properties {image quality:Best}
end tell

This succeeds in exporting a PDF, but the problem I have is that the above command uses the "fit each sheet to a single page" setting by default for whatever reason. I would rather it "use print settings" and export multiple pages for a single large sheet just like when printing (you know, the useful format).

These options are presented in the Export to PDF dialog box alongside "Image Quality" and "Require password to open" but not as export options in the script dictionary for Numbers. And the script command defaults to a different setting than the dialog box. I've tried to guess / search what I can do to change the setting automatically, but obviously with no such luck.

I am hoping, beyond all reason, that there is someway I can access this setting / property via AppleScript. And hopefully without using SystemEvents in any way. Any ideas?

much appreciated in advance…

EDIT with my final solution:

My original goal was to automatically export multiple Numbers files in different locations to PDF. Several hurdles I had were saving to any arbitrary location (including network mounted volumes) this requires a complete POSIX path, extracting the file location as alias (?) type and converting to POSIX, and sequencing the Finder -> Numbers operation so that the script slowed down enough and did enough checking that the document was open so that it didn't just crash every time. This is what I ended up with (please forgive my probably bad process confirmation and lack of error handling):

tell application "Finder"
set theSelection to selection
repeat with oneItem in theSelection     (*repeat the entire export process for 1 document at a time*)
    set finderLocation to oneItem as text
    set nameOfDocument to name of oneItem
    open oneItem

    tell application "Numbers"
        set idx to 0     (*wait until Numbers is open with a document*)
        repeat until (exists document nameOfDocument)
            delay 0.1
            set idx to idx + 1
            if idx > 1000 then quit
        end repeat

        set idx to 0     (*wait target document is at the front*)
        set pagesLocation to file of front document as text
        repeat until finderLocation = pagesLocation
            delay 0.1
            set pagesLocation to file of front document as text
            set idx to idx + 1
            if idx > 1000 then quit
        end repeat

        set exportFile to file of front document     (*get file location of document as file type*)
    end tell

    (*change file path for use when saving: convert file type to POSIX type, replace .numbers ext with .pdf, remove leading '/'*)
    set exportFile to POSIX path of exportFile
    set exportFile to text 1 thru -8 of exportFile
    set exportFile to exportFile & "pdf"
    set exportFileCharCount to count characters of exportFile
    set exportFile to text 2 thru exportFileCharCount of exportFile

    activate application "Numbers"

    delay 0.2

    tell application "System Events" to tell application process "Numbers"

        (*export whole document to pdf with settings*)
        click menu item 1 of menu 1 of menu item "Export To" of menu 1 of menu bar item "File" of menu bar 1

        tell sheet 1 of window 1
            click radio button "Use print settings" of radio group 1
            click pop up button 1
            click menu item "Best" of menu 1 of pop up button 1
            click button "Next…"
            delay 1
            tell application "System Events" to keystroke "/"     (*type leading '/' in order to trigger the ability to type any arbitrary location to save to*)
            delay 1
            tell application "System Events" to keystroke exportFile & return     (*type the rest of the target file location in POSIX and accept entry*)
            delay 0.5
            click button "Export"
        end tell

    end tell
    tell application "Numbers" to close front document without saving
end repeat
end tell

Best Answer

As I mentioned in a comment to the OP... If the properties do not exist in the dictionary then you'll probably have to use System Events.

The following example AppleScript code can be used to implement a UI Scripting solution even though that is not your first choice:

activate application "Numbers"

delay 0.2

tell application "System Events" to tell application process "Numbers"
    click menu item 1 of menu 1 of menu item "Export To" of ¬
        menu 1 of menu bar item "File" of menu bar 1
    tell sheet 1 of window 1
        click radio button "Use print settings" of radio group 1
        click pop up button 1
        click menu item "Best" of menu 1 of pop up button 1
        click button "Next…"
        delay 0.5
        click pop up button 1
        click menu item "Documents" of menu 1 of pop up button 1
        click button "Export"
    end tell
end tell

Note that as currently coded it assumes you have a document already opened in Numbers and will trigger this using a keyboard shortcut by one of the many means available, which includes third-party apps.


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between some events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.