Applescript batch export Pages docs to PDF

Discussion in 'Mac Programming' started by TimRMortis, Jul 11, 2016.

  1. TimRMortis macrumors newbie

    Joined:
    Jul 27, 2012
    #1
    Have some Applescript adapted from somewhere a few years ago, but it's got a problem I can't understand: it quits at the "export" line with the message error "The variable targetDocument is not defined." number -2753 from "targetDocument"

    Here's the script - what's wrong with the variable "targetDocument"? Thanks for any help!

    set theSourceFolder to (choose folder with prompt "Select a folder whose files you wish to convert to pdf:")

    tell application "Finder"
    set filesArray to every file of theSourceFolder whose name extension is "pages"
    end tell

    set theDestinationFolder to (choose folder with prompt "Select a folder where you want the converted files to be placed: ")

    tell application "Pages" to activate

    repeat with aFile in filesArray

    tell application "Finder"
    set fileAlias to aFile as alias
    set fileName to name of fileAlias
    set fileExtension to name extension of fileAlias
    end tell

    set theOriginalName to fileName

    -- remove extension

    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "." & fileExtension as string
    set fileName to first text item of fileName
    set AppleScript's text item delimiters to prevTIDs

    -- prepare desktop folder path and name

    set docPathAndName to theDestinationFolder & fileName & ".pdf" as string

    tell application "Pages"

    -- open the file
    set targetDocument to open aFile

    -- convert it
    export targetDocument to docPathAndName as PDF

    -- close it
    close targetDocument saving no

    end tell

    end repeat

    tell application "Pages" to quit

    display dialog "Done!"
     
  2. kryten2, Jul 11, 2016
    Last edited: Jul 11, 2016

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Please use code tags when posting code. Use this :

    Code:
    -- open the file
    set targetDocument to open fileAlias
    
    -- convert it
    export targetDocument to file docPathAndName as PDF
    
    You were trying to open Finder object references but those are only recognized by the Finder and by System Events.app. If you look at the open command of the Standards Suite of the Finder dictionary it doesn't return a file reference therefore targetDocument wasn't set.
    The script could use a cleanup, there're a lot of unnecessary things in it. A good starting example is found at Batch convert pages to PDF with automator or script?
    Excellent documentation is found at the info link.

    Info: https://iworkautomation.com/pages/index.html
    Info: AppleScript file reference forms
     
  3. TimRMortis thread starter macrumors newbie

    Joined:
    Jul 27, 2012
    #3
    --- Post Merged, Jul 12, 2016 ---
    Thank you, kryten2! ... for the fix as well as a good explanation and the link to a better approach. The fix got me farther, but now the export step is complaining that I don't have permission. Manually exporting from Pages doesn't have any problem, so maybe I need to set a parameter in the export step.
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    That shouldn't happen. Post the exact error message or attach a screenshot of the error to your post. So it doesn't happen when you export manually to the same location as specified in the script? What version of OS X and Pages are you using?
     
  5. TimRMortis thread starter macrumors newbie

    Joined:
    Jul 27, 2012
    #5
    The error message is attached. Using MacOS 10.11.5 Pages 5.6.2
    --- Post Merged, Jul 12, 2016 ---
    OK - I found a hint by googling the permission problem. Where I had:

    export targetDocument to docPathAndName as PDF

    I should have instead:

    export targetDocument to file docPathAndName as PDF

    Making that change, the script now works. Thanks for all your help. FYI, I elaborated the export line a bit:

    export targetDocument to file docPathAndName as PDF with properties {image quality:Best}



     

    Attached Files:

  6. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    If you read my answer more carefully and looked at the code section you would have noticed that it's right there.:rolleyes::(
     

Share This Page