You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

Jan 3, 2017 2:28 PM in response to Flint Million

I have been struggling with odd, inconsistent Applescript errors ever since upgrading to Sierra. I run most of my scripts as Automator services, not apps, but I tried saving them as apps and they still failed. Today, I ran CleanMyMac, repaired the disk twice with Disk Utility, and everything is now FINE. I have no idea what changed, but this did work for me (FINALLY).

Jan 31, 2017 5:59 PM in response to VikingOSX

Update. I have updated my previously posted AppleScript code to incorporate fixes suggested by Yvan Koenig at MacScripter. Testing on macOS 10.12.3 with the following, modified AppleScript (from an earlier post) now allow exports to work correctly.


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)


-- original path and base filename with new extension added.

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

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

-- log exportDocumentFile


-- fix for macOS Sierra 10.12.2/3 export/save permissions error. Create the empty file before exporting to it.

-- source: Yvan Koenig, 2017-01-18, http://macscripter.net/viewtopic.php?pid=188721

close access (open for accessexportDocumentFile)

-- end fix

try

tell application "Pages"

launch

openafile

delay 2

with timeout of 1200 seconds

-- Pages v5.6 or later, otherwise drop the with properties clause

-- export front document to file exportDocumentFile as PDF with properties {image quality:Best}

export front documenttofileexportDocumentFileasMicrosoft Word

end timeout

close front documentsavingno

end tell

on error errmsg number errnbr

my error_handler(errnbr, errmsg)

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

return

end try


tell application "Finder"

-- make the file extension appear

set extension hidden of fileexportDocumentFile to false

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

end tell

return

on error_handler(nbr, msg)

return display alert "[ " & nbr & " ] " & msg as critical giving up after 5

end error_handler

Feb 1, 2017 4:52 AM in response to Barry

Barry,


You ran code from Jun 5 2014, that as of that date, had a white-space filename bug that was fixed with the following correction. I also subsequently posted that the Jun 5 script was to be ignored, and have posted additional refinements in 2015, 2016, and even last (2017-01-31) evening.


set ppath to quoted form of POSIX path of pagesPath

I copy/pasted the Jun 5 script into Script Editor on OS X 10.11.6, made the above correction, and ran it with your example white-space filename opened in Pages v5.6.2. The script worked correctly, without errors. No part of the script attempts to insert content into the Pages document.

I recommend that you use the 2017-01-31 posted version.

Apr 19, 2014 5:38 PM in response to Flint Million

Incorporating Jeff's advice, here is an AppleScript that outputs a Word document derived from the opened Pages filename with the extension replaced by .docx. No hardcoding of file paths and filenames. This example puts the Word document on the Desktop. One annoyance is that Pages exports with the extension hidden flag enabled. This example was tested in Pages v5.2.


tell application "Pages"


activate

set pagesFile to document 1


tell application "Finder"


set fname to name of pagesFile

set delims to AppleScript'stext item delimiters

set AppleScript'stext item delimiters to "."

--strip .pages extension from the filename

if fname contains "." then set fname to (text items 1 thru -2 of fname) as text

set AppleScript'stext item delimiters to delims

set docxName to fname & "." & "docx"

set docxPath to POSIX path of (path to desktop from user domain) & docxName


end tell


exportpagesFiletoPOSIX filedocxPathasMicrosoft Word

end tell

Jun 4, 2014 8:18 PM in response to VikingOSX

Hi VikingOSX, I found your post incredibly useful, thank you. I have just one small question.


I was wondering if there is some way to modify your script so that the docx file saves in the same location as the original pages file, rather than on the deskotp?


I have had a bit of a look at the AppleScript documentation but I'm very new to this and couldn't figure out how it would be done.


Thanks in advance for any help.

Jun 5, 2014 12:12 PM in response to jnew9342

The following script, unlike the previous post, exports the Word document back to the original location of the Pages document. It also makes the Word document extension visible.


tell application "Pages"


activate


set pagesFile to document 1

set pagesPath to file of document 1

set ppath to POSIX path of pagesPath

set fpath to do shell script "/usr/bin/dirname " & ppath


tell application "Finder"


set fname to name of pagesFile


set delims to AppleScript'stext item delimiters

set AppleScript'stext item delimiters to "."


--strip .pages extension from the filename

if fname contains "." then set fname to (text items 1 thru -2 of fname) as text

set AppleScript'stext item delimiters to delims

set docxName to fname & "." & "docx"


--set docxPath to POSIX path of (path to desktop from user domain) & docxName


-- put exported file back in original document location

set docxPath to fpath & "/" & docxName


--display dialog docxPath


end tell


exportpagesFiletoPOSIX filedocxPathasMicrosoft Word

end tell

tell application "Finder"

set extension hidden of ((POSIX file docxPath) as alias) to false

end tell

Nov 23, 2014 9:25 AM in response to VikingOSX

-- This small Applescript will take a "pages" file and export it to a "pdf" file I struggled a little bit with the "export" function, but got the answer from here - thank you Jeff Shenk and VikingOSX for pointing out that I need the 'file' qualifier before the file variable. I really struggled with that. I hope somebody will find this useful - as I have found many things useful in this forum - and elsewhere. I am using the 'native' file description.


-- I am using Pages 5.5.1, MacOSX 10.10.1 and ScriptEditor 2.7 - Applescript 2.4



--Select from where you will pick up the pages files

set theFolder to choose folder with prompt "Select folder with original pages files :"

--Do it

tell application "Finder"

set theNames to name of files of theFolder ¬

whose name extension is "pages"

end tell


--Select where the PDF files will go

set pdf_Folder to choose folder with prompt "Select folder where PDF files will go :"


-- How many files to export

set item_count to (get count of items in theNames)


--Get files and export them

repeat with i from 1 to item_count


set current_file to itemi of theNames-- get a file

set lean_file to text 1 thru -7 of current_file & ".pdf" -- change the originalfile (.pages) to a .pdf name

set out_file to (pdf_Folder as Unicode text) & (lean_file) -- get the fully qualified output name

set in_file to (theFolder as Unicode text) & (current_file) -- get the fully qualified input file name


tell application "Pages"

set mydoc to openfilein_file-- open input file in Pages


exportmydoctofileout_fileasPDF--do the exporting


closemydocsavingno-- close the original file without saving

end tell


end repeat



display dialog "done" -- Job done

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 Account.