Batch Converting Pages Documents to Word Documents with Applescript

So if you read this blog you already know that I have pretty much come to the decision that my next computer will not be a Mac. I love my old MacPro 4,1 but it is getting long in the tooth, and I haven’t even been able to do the Firmware Upgrade to get it to 5,1, so the next OS Sierra will not work on my current MacPro as it will only work on a 5,1 (I haven’t tried putting in my old video card before the upgrade, which might help, but it looks like El Capitan makes it harder).

Unfortunately in 2007 I moved over to Pages to do my invoices, resumes and cover letter. Now you can individually export your documents to the modern word format docx, but I am talking hundreds and hundreds of files that I may want to access at some point, so I wanted a way to batch those files.

This requires applescript, and while I used to be OK at Applescript all those skills have since gone away.

A quick google search showed one page dedicated to scripting the iWorks suite, and it is iWorkAutomation. Specifically there is a script to Export to Word, but it is not a batch export, just a single use, and so not of use to me, except for stealing some of it’s commands.

So then I went back to the old faithful applescript forums MacSpripter, and did a search. And I found an awesome script from Yvan Keonig, which automates the process, allowing you to pick a folder with you pages documents and then chose a destination folder, and it batch converts to PDF files.

Luckily my limited Applescript skills and the fact that the script was so well written, let me use the Microsoft Word commands from iWork Automation and combine them to make a script to convert to Word documents.

Just copy this script and paste it into Script Editor and run the script. You can also save the script to your scripts folder.

set theSourceFolder to (choose folder with prompt “Select a folder whose files you wish to convert to word:”)
tell application “Finder”
set filesArray to every file of theSourceFolder whose name extension is “pages”
end tell
set theDestinationFolder to (choose folder with prompt “Select a folder where you want the converted files to be placed: “)
tell application “Pages” to activate
repeat with aFile in filesArray
tell application “Finder”
set fileAlias to aFile as alias
set fileName to name of fileAlias
set fileExtension to name extension of fileAlias
end tell
set theOriginalName to fileName
— remove extension
set prevTIDs to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to “.” & fileExtension as string
set fileName to first text item of fileName
set AppleScript’s text item delimiters to prevTIDs
— prepare desktop folder path and name
set docPathAndName to (theDestinationFolder as text) & fileName & “.docx” # EDITED
tell application “Pages”
— open the file
set targetDocument to open fileAlias — EDITED : was aFile which is a Finder reference, not what Pages requires
repeat 10 times # I added a loop which may be useful if the original document is very long
try
export targetDocument to file docPathAndName as Microsoft Word
exit repeat
on error
tell me to delay 0.5
end try
end repeat
— close it
close targetDocument saving no
end tell
end repeat
tell application “Pages” to quit
display dialog “Done!”

Awesome, now I have batch updated all my pages documents to Word documents. Sure the formatting needs some updating, but at least they will all work on a PC!

Leave a Reply