Search code examples
applescript

Export all .pages files from one folder to pdf in another folder


Here is my script

set docsFolder to choose folder with prompt "Select the folder containing Pages documents:"
set pdfFolder to choose folder with prompt "Select the folder to save PDFs:"

tell application "Finder"
    set pagesFiles to every file of docsFolder whose name extension is "pages"
end tell

repeat with pagesFile in pagesFiles
    set docPath to pagesFile as alias
    set docName to name of pagesFile
    set baseName to text 1 thru ((length of docName) - 6) of docName -- Retire ".pages"
    set pdfPath to ((pdfFolder as text) & baseName & ".pdf") as text -- Force un chemin valide

    tell application "Pages"
        open docPath
        delay 1 -- Laisser le temps au document de s'ouvrir
        tell front document
            export to file pdfPath as PDF
            close saving no
        end tell
    end tell
end repeat

and the error is: Erreur dans Pages : Forme de clé invalide.

What's wrong ??? thanks for your help

I'm expecting pages to export to pdf but I have this message.


Solution

  • What happens if you change your pages tell block to this:

        tell application "Pages"
            activate
            open docPath
            delay 1 -- Laisser le temps au document de s'ouvrir
            export front document to file pdfPath as PDF
            close front document saving no
        end tell
    

    The problem is likely that you had the export command inside a tell front document block, which means that it was adding an unwanted specification to your export… something like this:

    export document 1 to file "MacHD:Users:whomever:Desktop:destination:docco.pdf" of document 1 as PDF

    You don't want that of document 1 in there.

    I think that the invalid key form of the error message suggests that the file specifier is not properly formed.

    If you look at the log you can see how the export line changes depending on the tell block.

    Alternative approach

    If you remove the word file from the export command line, then the export should not be affected by this issue. The reason being that if you exclude that word then you are working with a string rather than a file specifier.

    In effect:

    "MacHD:Users:whomever:Desktop:destination:docco.pdf" instead of
    file "MacHD:Users:whomever:Desktop:destination:docco.pdf"

    So either fo these should work:

    without front document block

        tell application "Pages"
            activate
            open docPath
            delay 1 -- Laisser le temps au document de s'ouvrir
            export front document to pdfPath as PDF
            close front document saving no
        end tell
    

    with front document block

        tell application "Pages"
            open docPath
            delay 1 -- Laisser le temps au document de s'ouvrir
            tell front document
                export to pdfPath as PDF
                close saving no
            end tell
        end tell
    

    Of course, apple changes stuff with each OS version so maybe things have changed since Sierra but hopefully this helps.