@thebroz For what it’s worth, the non-generic solution you posted lead me to find a solution to my own similar problem. Thank you!!
I’m trying to automate Pages to open a .doc
file and export it as a .txt
file, but was getting a doesn’t understand the “export” message
error. I’m running macOS High Sierra version 10.13.4 (17E199) and Pages version 7.0 (5576). My understanding of macOS and AppleScript is probably not yet at the level of others on this forum, but I’ll describe my journey in case it’s helpful for anyone else who stumbles onto this search result.
tl;dr: Try open -a Pages
in a shell script before running the AppleScript that does the export.
This is a simplified version of my script:
on run argv
set inputFile to POSIX file (POSIX path of (item 1 of argv as string))
set outputFile to POSIX file (POSIX path of (item 2 of argv as string))
tell application "Pages"
open inputFile
export front document as unformatted text to file outputFile
quit
end tell
end run
I realize this may have stylistic issues, but based on trial and error it seemed to be mostly working. I’m running it from a shell script that assembles the arguments; I realize I could probably do everything I need to do with AppleScript, but I’m slightly more comfortable with bash. The inputFile
would open properly, but then I would get this error:
./export.scpt: execution error: Pages got an error: document 1 doesn’t understand the “export” message. (-1708)
For some reason my document was not responding to export
, but I got the same error when I tried close
. To make matters worse, it would sometimes work, perhaps after rebooting my machine or trying it on a clean install of macOS. In my attempt to isolate the issue, I tried make new document
, and Googling for subsequent errors lead me to this thread.
I updated my shell script to move the .plist
files out of ~/Library/Containers/com.apple.iWork.Pages/Data/Library/Preferences
before my AppleScript ran, and then move them back afterward. This worked reliably, but the only problem was that when Pages opened, it thought it was opening for the first time, and presented the two-step “Welcome to Pages” flow with a mandatory License Agreement acceptance. This blocked the script from continuing until I manually clicked through the flow, which undermines the reason to automate this task in the first place.
To work around that, I tried an open -a Pages
in my shell script before moving the files out of the Preferences directory. I realize that it is “almost always a bad idea to flail about in system files that you don’t understand”, and it’s even more likely to be a bad idea to do so while the application in question is running, but I was desperate.
Fortunately nothing caught on fire. As I was experimenting with the fix to make sure it was working properly, I noticed that my AppleScript export
completed successfully even if I didn’t actually move the .plist
files away – launching Pages before running my script seemed to be sufficient to resolve the issue.
A curious, but simple and safe, solution. I would never have thought to try it without first experimenting with the .plist
files, so thanks again everyone.