Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript+Pages: Export command always fails with permission error

Here is my script:


tell application "Pages"


set thisDoc to open "/Users/fmillion/test.pages"

set docFile to file of thisDoc

log "The file is " & docFile


export thisDoc to "/Users/fmillion/test2.docx" as Microsoft Word


end tell

The script runs, Pages opens and loads the document. However, the Export step fails. Here's the full events output:


tell application "Pages"


open "/Users/fmillion/test.pages"


--> document "test.pages"


get file of document "test.pages"


--> file "Macintosh HD:Users:fmillion:test.pages"


(*The file is Macintosh HD:Users:fmillion:test.pages*)


export document "test.pages" to "Macintosh HD:Users:fmillion:test2.docx" as Microsoft Word


--> error "The document “test.pages” could not be exported as “test2”. You don’t have permission." number 6

Result:

error "Pages got an error: The document “test.pages” could not be exported as “test”. You don’t have permission." number 6


No amount of fiddling with parameters will change this. Obviously, I have write permission to my home directory, which just to be absolutely sure I verified. (I do.) I even tried creating a directory with chmod 777 to give it world-writable state, but Pages still wouldn't export there.


I feel like I'm missing something - could anyone tell me what it is?


Thanks!


F

OS X Mavericks (10.9.2)

Posted on Apr 19, 2014 10:16 AM

Reply
33 replies

May 19, 2015 5:34 PM in response to Flint Million

I decided to update my previously posted AppleScript with a file prompt, error handling, and less clutter. It will ensure that the file extension is always visible, and optionally, you can put a color tag on the exported document. This AppleScript has been tested without modification on both Mavericks 10.9.5, and Yosemite 10.10.3 with Pages v5.2.2 and v5.5.3 respectively.


property pages_type : {"com.apple.iwork.pages.pages", "com.apple.iwork.pages.sffpages"}

property labelColor : {none:0, orange:1, red:2, yellow:3, blue:4, purple:5, green:6, gray:7}


set afile to ¬

(choose file with prompt "Choose Pages documents" of type pages_type ¬


default location (path todesktop folder) ¬

without invisibles)


set exportDocumentFile to text 1 thru (offset of ".pages" in (afile as text)) of (afile as text) & "docx"

-- log exportDocumentFile

try

tell application "Pages"

openafile

delay 2

with timeout of 1200 seconds

export front documenttofileexportDocumentFileasMicrosoft Word

end timeout

close front documentsavingno

end tell

on error msg number enbr

display alert "[ " & enbr & " ] " & msggiving up after 8

tell application "Pages" to if it is running then quit

endtry


tell application "Finder"

set extension hidden of fileexportDocumentFile to false

-- set label index of file exportDocumentFile to blue of labelColor

end tell

May 20, 2015 3:37 PM in response to rmt1218

Syntactically correct AppleScript code that is error/warning free in the AppleScript Editor, or saved from it as an Application — will run and export a Word document from Pages as I have tested and stated.


When you replace the boilerplate text in the Automator Run AppleScript action, with the identical latest AppleScript that I posted above, it gets a bogus error about that second colon in my labelColor property declaration. That is the only error that I receive with Automator on Mavericks. There is nothing to fix. Just comment out that line, and the script will compile and run as an Automator application should — unless you introduced something additional. It produces the expected Microsoft Word document exported from Pages.

May 21, 2015 6:32 AM in response to VikingOSX

Here are a couple of screen shots. This is the workflow before adding the script:

User uploaded file

After taking this screen shot, I inserted your script from June 5 where it says (* Your script goes here *). First I removed the whole line. Then I tried keeping the parentheses, but it didn't like that. I tried curly brackets and it didn't like that either. At the top, I've tried "no input" and "text", and "Pages" and "any application", and all combinations thereof. I've both checked the "Output replaces selected text" box and unchecked it. I've added nothing else other than the script. I have not introduced anything additional.


Here is the log when I run it:

User uploaded file

There may be nothing to fix when you run it on your machine, but there clearly is on mine.

May 21, 2015 6:56 AM in response to VikingOSX

Ok... so deleting the entire boilerplate text worked with the script from two days ago, but it did NOT work with the script from June 5. But I don't think it worked quite as it's supposed to. When running the service, it asked me which document I'd like to open, even though I already had an open document. So that's what seems to be the problem - for some reason it isn't recognizing that the document is open. This only happens with previously created documents though. If I create a new document and export it, that works.

May 21, 2015 7:13 AM in response to rmt1218

Here is how you get this working.

  1. Start over. In Automator, select new document, then Application (not Service), and click Choose. Drag the Run AppleScript action into the workflow window. By default, Apple places a boilerplate run handler in there that a Service would use. My AppleScript does not use a run handler, so select all of the text in that Run AppleScript action and delete it.
  2. In the now empty Run AppleScript action window, copy/paste my AppleScript code of May 19, 2015 into it. At the top, place two dashes (AppleScript comment --) before the second property labelColor line. Click the hammer icon to compile. The appearance of multi-color syntax means that it found no errors in the code. Now, save this application to your Desktop. Double-click to have a file chooser prompt you for the Pages document that you want to export as Word .docx.


When you are in the File Chooser, you can double-click on folder icons to drill down into a folder hierarchy to access deeper Pages documents. This script is not going to magically do that for you. Exported Word documents are writtten back to the original Pages document location.

May 21, 2015 7:32 AM in response to VikingOSX

That works the way you said, but that's not exactly what I'm trying to do. I don't want to be prompted to choose a document. I want to export the already open document in a single click or keyboard shortcut. I saved the script as an application in Automator, then I created a second workflow (a service) so I could run the application while having an open document as the active window, but it still prompted me to select a document.


Let me clarify my error at the top of this page: I am not expecting it to open a document for me. The document was already open and it wouldn't export back to the original file location of the (new ) Pages doc when the Pages doc was buried. However, when I ran it on a (new) Pages doc that was located on the desktop, it worked. Also - the script didn't work on documents that had been created prior to creating the workflow.

May 21, 2015 8:40 AM in response to VikingOSX

Ha! Finally! I got your June 5 script to work.


I changed:

setfpathtodo shell script "/usr/bin/dirname " & ppath

to:

setfpathtodo shell script "/usr/bin/dirname " & quoted formofppath


Apparently it was just the spaces in the folder names and file names that were giving me trouble. Works like a charm now, thanks for the script. Much appreciated 🙂

May 21, 2015 10:04 AM in response to rmt1218

Absolutely forget about that June 2014 script. The role of this 2015 script is not to automatically export an open Pages document to Word — instead of File > Export > Word. It assumes that no Pages is running, will prompt you for the Pages document in the file chooser, and then launch Pages to export this selected document to Word.


You should not be running it as a Service. As I previously stated it needs to be an Automator Application. I explained how to do that.

AppleScript+Pages: Export command always fails with permission error

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.