I have coded an AppleScript that uses the installed Pages '08 or Pages '09 application to convert their respective Pages documents into Microsoft Word .doc format. Pages v5 documents are ignored if selected. It is both interactive (supports double-click/double-tap), or drag and drop onto the application icon. It also uses a Python/Objective-C custom file and folder chooser that allows multiple selection of file(s) and folder(s) – something ordinary AppleScript prevents.
If you have Pages '09, use that to convert Pages '08 and '09 documents. Pages '08 will not convert Pages '09 documents to Word.
This AppleScript has been tested on OS X 10.9.5 (Mavericks) and OS X 10.10.3 (Yosemite) with the AppleScript Editor, and Script Editor respectively. There is considerable code here, but think of it as a black box. No need to understand it, or be a programmer.
How to install
- Open the respective Apple Script editor (Applications/Utilities) on your major version of OS X above.
- Copy/paste the following code into that editor
- Click the compile (hammer) icon in the Script Editor toolbar.
Should change to different colors with error-free compile.
- File > Save…
- Save As: p89todoc
- In the Favorites, click on Documents.
- File Format: Text (the Save As extension will change to .applescript)
- No other checkboxes selected
- Save
- Option key + File > Save As…
- Save As: p89todoc
- In the Favorites, click Desktop
- File Format: Application (extension changes to .app)
- Check only Hide Extension
- Save
- Ready for use.
Code
-- Pages2doc.applescript
-- Convert Pages '08 and '09 documents to Word .doc format
-- Supports interactive file chooser, or drag and drop files (but not folders)
-- Word files saved at original Pages document location
-- Version 1.0, Tested on OS X 10.10.3 with Pages '09 v4.3
-- VikingOSX, 4 May 2015, Apple (Pages) Support Community
use scripting additions
-- Default open location for file/folder chooser
property defaultOpenFolder : POSIX path of (path to desktop folder)
-- property defaultOpenFolder : POSIX path of (path to home folder)
-- property defaultOpenFolder : POSIX path of (path to documents folder)
-- use the version of Pages below that you have installed
-- property PagesApp : ((path to applications folder) as text) & "iWork '08:" & "Pages.app:"
property PagesApp : ((path to applications folder) as text) & "iWork '09:" & "Pages.app:"
property fileCnt : 0 as integer
-- double-click to get a file and folder chooser
on run
display dialog "This script converts Pages '08/'09 documents into Microsoft Word .doc format. You can interact with a double-click, or simply drag and drop onto this application icon. Expect a chooser dialog to follow after this message." & return & return & "Word documents will be saved in the original location of the Pages document." & return & return & "This message will automatically disappear after 15 seconds." with title "Pages '08/'09 to Word Conversion Help" giving up after 15
try
set fileObjects to {}
set retObjects to filesandfolders(defaultOpenFolder)
if retObjects contains "None" then
error number -128
else if retObjects is equal to POSIX path of (text 1 thru -2 of defaultOpenFolder) then
display alert "You must make a selection, not just click the Select button."
return
end if
-- roll our own alias list
repeat with i from 1 to (count of paragraphs in retObjects)
copy (paragraph i of retObjects as POSIX file as alias) to end of fileObjects
end repeat
on error msg number nbr
display alert "[ " & nbr & " ] " & msg giving up after 10
error number -128
end try
-- pass on to drag and drop handler
open fileObjects
return
end run
-- drag and drop files only
on open fileObjects
repeat with i from 1 to count of items in fileObjects
set this_item to (item i of fileObjects)
tell application "Finder" to set fileType to kind of alias (this_item as text)
if fileType contains "Folder" then
tell application "Finder"
-- decided not to recurse into sub-folder hierarchy
set pages_list to (items in this_item's contents whose kind ¬
is "Pages Publication") as alias list
end tell
repeat with theFile from 1 to (count of pages_list)
process_file(item theFile of pages_list)
end repeat
set pages_list to {}
else if fileType contains "Pages Publication" then
process_file(this_item)
end if
end repeat
tell my application PagesApp
if it is running then
try
quit
end try
end if
end tell
delay 2
if fileCnt > 0 then
tell application "System Events" to ¬
display alert "Processing complete" & return & fileCnt & " Document(s) converted" giving up after 10
set fileCnt to 0 as integer
end if
return
end open
on process_file(afile)
set status to isPages89(afile) as boolean
-- exclude Pages v5 documents
if status is false then return
tell my application PagesApp
try
set thisDoc to open afile as alias
set docPathAndName to (text 1 thru (offset of "." in (afile as text)) ¬
of (afile as text) & "doc")
save thisDoc as "Microsoft Word 97 - 2004 document" in docPathAndName
close thisDoc saving no
set fileCnt to (fileCnt + 1) as integer
on error errmsg number errnbr
display alert "[ errnbr ] " & errmsg
tell my application PagesApp
if it is running then
try
quit
end try
end if
end tell
error number -128
end try
end tell
-- tell my application PagesApp to if it is running then quit
return
end process_file
on isPages89(afile)
set status to 0 as integer
set filePosixPath to quoted form of (POSIX path of (afile as alias))
tell application "System Events"
if (afile as alias) is package folder then
set status to (do shell script "/bin/ls -1 " & filePosixPath & "index.xml.gz | wc -l") as integer
else
set status to (do shell script "/usr/bin/unzip -l " & filePosixPath & " | egrep -c \"index\\.xml\"") as integer
end if
end tell
if status is 1 then
return true -- yes, we have a valid Pages '08 or '09 document
else
return false
end if
end isPages89
on filesandfolders(openfolder)
-- AppleScript only permits choose file, or choose folder.
-- This PyObjC code permits both selections in one panel.
return do shell script "/usr/bin/python <<'EOF' - " & openfolder & "
# -*- coding: UTF-8 -*-
from Foundation import *
from AppKit import *
import os
import sys
def openFile():
\"\"\"
Launch a custom file *and* folder open panel
Restrict files to Pages documents, and folders
Default open directory is taken as CL argument, otherwise Desktop.
\"\"\"
fileTypes = ['com.apple.iWork.pages.pages',
'com.apple.iWork.pages.sffpages',
'com.apple.iWork.Pages',
'public.folder']
theTitle = 'Open'
line1 = 'Select files and folders using the command key'
line2 = 'Only Pages (.pages) documents will be processed.'
theMsg = '{}\\n{}'.format(line1, line2)
# If command-line open directory is missing, default to Desktop.
if len(sys.argv) == 2:
openfolder = ''.join(os.path.expanduser(sys.argv[1]))
else:
openfolder = ''.join(os.path.expanduser('~/Desktop'))
opanel = NSOpenPanel.openPanel()
# Force to normal size open panel
opanel.setFrame_display_(NSMakeRect(0, 0, 720, 525), YES)
opanel.setTitle_(theTitle)
opanel.setMessage_(theMsg)
opanel.setPrompt_(\"Select\")
opanel.setAllowedFileTypes_(fileTypes)
opanel.setCanCreateDirectories_(False)
opanel.setCanChooseDirectories_(True)
opanel.setCanChooseFiles_(True)
opanel.setTreatsFilePackagesAsDirectories_(False)
opanel.setAllowsMultipleSelection_(True)
opanel.setResolvesAliases_(True)
opanel.setDirectoryURL_(NSURL.fileURLWithPath_isDirectory_(openfolder, YES))
# Make topmost window on open
NSApp.activateIgnoringOtherApps_(YES)
if opanel.runModal() == NSOKButton:
return opanel.filenames()
else:
return 'None'
def main():
try:
fileret = openFile()
if 'None' not in fileret:
print(\"\\n\".join(fileret))
else:
print(fileret)
except TypeError:
pass
if __name__ == '__main__':
sys.exit(main())
'EOF'"
end filesandfolders