Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TimRMortis

macrumors newbie
Original poster
Jul 27, 2012
4
0
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!"
 
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!
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
 
Last edited:
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
[doublepost=1468339395][/doublepost]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.
 
[doublepost=1468339395][/doublepost]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.
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?
 
The error message is attached. Using MacOS 10.11.5 Pages 5.6.2
[doublepost=1468347830][/doublepost]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}



 

Attachments

  • Script Error.png
    Script Error.png
    53.3 KB · Views: 307
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.