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

iWorks'08

Pages 5.5 will not open files from Pages'08 but will open .doc files exported from Pages'08. You can't simply choose all the files in Pages'08 and attempt to export. You have to open each file individually to export. Hence, the question: Is there a way to batch export Pages'08 files to .doc files using a script?

Posted on May 3, 2015 8:57 AM

Reply
4 replies

May 4, 2015 12:15 AM in response to Chuck000

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


  1. Open the respective Apple Script editor (Applications/Utilities) on your major version of OS X above.
  2. Copy/paste the following code into that editor
    1. Click the compile (hammer) icon in the Script Editor toolbar.

      Should change to different colors with error-free compile.

    2. File > Save…
      1. Save As: p89todoc
      2. In the Favorites, click on Documents.
      3. File Format: Text (the Save As extension will change to .applescript)
      4. No other checkboxes selected
      5. Save
    3. Option key + File > Save As…
      1. Save As: p89todoc
      2. In the Favorites, click Desktop
      3. File Format: Application (extension changes to .app)
      4. Check only Hide Extension
      5. Save
  3. 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

May 4, 2015 8:31 AM in response to VikingOSX

I copied, pasted this script back into the Script Editor on Yosemite and discovered that a Python indentation error occurs when the chooser appears. Python is picky about its code indentation, and apparently, posting to this site has messed up the proper indentation of the 10 lines near the end in its main() function. Once these are fixed with expected tabs (1 tab = 4 spaces, see Script Editor preferences - Editing tab) — the Python code works perfectly again.


Here is the affected code with proper indentation (at least when I posted it):


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()) 

May 4, 2015 8:48 AM in response to VikingOSX

Ignore the May 4, 11:31 AM post. Use this one. Got timed out before finished.


I discovered that the act of posting code here using the advanced editor's plain text template, disrupted the formatting of the following Python code. The full code post can be copied and pasted in the Script Editor, and will compile, but at runtime, when the custom chooser attempts to launch, Python complains about an indentation issue.


For those with the AppleScript Editor on Mavericks, copy/paste the entire application again. Apply the following Python formatting fix. Then, and only then click the compile (hammer icon) on the AppleScript Editor toolbar. AppleScript Editors prior to Yosemite scramble other guest script (Python) code when compiled. Looks like h*** but it does work when the formatting below is applied.


Here is the affected code with proper indentation (when posted). There are only tabs (1 tab = 4 spaces) for indentation which can be set in Script Edtior's Editing preferences. Once the following changes are made to the previously posted code, the application works as expected.


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())

iWorks'08

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