5

I am needing to convert about 100 .pages documents to either pdf or docx. With the AppleScript script below, I am able to open .pages documents from the folder that I select. However I receive the following error when trying to export.

Question: What is causing the error and how do I fix it?

Error Message with personal information replaced:

error "Pages got an error: Can’t make alias \"Macintosh HD:Users:Path:To:File:foo.pages\" into type document." number -1700 from alias "Macintosh HD:Users:Path:To:File:foo.pages." to document

Line of code producing the error:

export this_file to exportFileName as PDF

Script:

set exportFileExtension to "pdf"
set this_folder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "System Events"
    set these_files to every file of folder this_folder
end tell
repeat with i from 1 to the count of these_files
    set this_file to (item i of these_files as alias)
    set this_info to info for this_file
    tell application "Finder"
        set {fType, nExt} to ({file type, name extension} of file this_file)
        set documentName to the name of this_file
        set exportFileName to documentName & "." & exportFileExtension
    end tell
    if nExt contains "pages" then
        tell application "Pages"
            open this_file
            export this_file to exportFileName as PDF
            close saving no
        end tell
    end if
end repeat

###Update###: I have added this script to github with the hopes of it benefiting others as it continues to be improved. Contributions appreciated.

1
  • Yes, indeed :) Never touched AppleScript before the major copy/paste from multiple sources exhibited here. I'm not using fType, but I am using nExt. can I just set nExt to (name extension of file this_file)? Regarding naming conventions, agreed. This is from the multiple sources that I copied from. I will clean that up. Thank you. May 17, 2017 at 16:25

1 Answer 1

2

Replace the line of code producing the error with the following:

export front document to file (this_folder & exportFileName) as PDF

There were two issues here:

  • export takes a document, so you need to pass it the document as opened by Pages.
  • You gave the file name, but not a containing folder to put the file with this name, so concatenate this with the containing folder path so Pages knows where to export to.
5
  • Thank you. I was able to successfully export using the changes that you suggest. However, my code opens all 100 files before executing and does not close them. May 16, 2017 at 23:21
  • 1
    @BrianWilson, Re: your comment... change close saving no to close front document saving no May 16, 2017 at 23:52
  • hmm. Now set this_file to (item i of these_files as alias) is causing error message: error "Can’t make «class cpkg» \"Macintosh HD:Users:Path:to:file.pages:\" of application \"System Events\" into type alias." number -1700 from «class cpkg» "Macintosh HD:Users:Path:to:file.pages:" to alias May 17, 2017 at 0:42
  • Thanks for the help in answering the original questions with followups. The code is still very buggy, but the original problem has been solved. As a service to humanity, I am putting the script on github. link May 17, 2017 at 14:42
  • Here is a better link to github. I have included a text file so it is not necessary to download the .scpt file to see what is going on. May 17, 2017 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .