Pages export to Microsoft Word failing with Applescript

115 views Asked by At

I am currently trying to export an iWork .pages file to a Microsoft Word .docx file with a simple applescript script.

on run {path, fileName}

tell application "Pages"
set theDoc to open (path & "/" & fileName)

set theDocName to name of theDoc
export theDoc as Microsoft Word to file ((path & "/" & theDocName & ".docx") as text)

close theDoc

end tell
end run

I am expecting the document to be exported as a Microsoft Word document however I am receiving the following error:

Pages got an error: The document “1” could not be exported as “/Users/joshgrimmett/Desktop/pages2docs/in/1”. (6)

1

There are 1 answers

1
voronoi On

I managed to export a Pages document to Word with the following script:

on run(_dirname, _filename)
    tell application "Pages"
        set theDoc to open (_dirname & _filename)
        set theDocName to name of theDoc
        set dst to (_dirname & theDocName & ".docx")
        export theDoc to POSIX file dst as Microsoft Word
        close theDoc
    end tell
end run

Call the script with e.g. /Users/joshgrimmett/Desktop/pages2docs/in/ and foo.pages (or any other Pages-support extension) as arguments. I believe the problem was the usage of path (it is a reserved keyword in AS) as a variable, and not specifying the destination class (Since we’re working with POSIX paths and not aliases, it’s POSIX file).