Here is new code that can export (only) Pages v5 documents to Pages '09 format. It avoids overwriting the source document by appending '_p9' to the exported document name. Foo.pages becomes foo_p9.pages. One can search in the Finder for _p9 and all exported documents will appear. The following script can be saved from the AppleScript editor as an AppleScript application, or one can make an Automator application, starting with a blank Run AppleScript action, into which this script is pasted.
This script is larger because it has dual nature (interactive, and drag/drop) capability, and uses Python handlers to do things that AppleScript would be awkward, or simply cannot provide. One of these is a custom Open panel that allows the simultaneous selection of multiple files and folders. Another Python handler inspects each Pages document and only permits version 5 documents for export processing. When processing is complete, a notification panel will slide in from screen right showing the number of files exported. Since these Notification displays are fleeting, that message is also found in your OS X Notification panel shown by clicking the right-most outline icon in the Finder menu bar.
This code was extensively tested on OS X El Capitan (10.11.3) with Pages v5.6.1. Testing included both the interactive, and drag/drop capability. Single documents, multiple documents, single (nested) folders, and multiple (nested) folders, and any combination of these all exported to Pages '09 format. Every exported file was opened, and viewed in Pages '09.
Installation Steps:
- From Launchpad (Dock) : Other, click on Script Editor to launch it.
- Choose New Document
- Copy/paste the indicated script code (below) into the Script Editor window (it will be purple)
- On the Script Editor toolbar, click the compile (hammer) button. The AppleScript will become multi-colored (good), and the Python code will remain monochrome (good).
- There are two save steps:
- Save as the AppleScript source
Script Editor File menu : Save…
- Name : p52p9export (no extension, and this name can be whatever you want to call it.
- Folder : Your documents folder
- File Format: Text (this will assign .applescript to your filename)
- Nothing else is checked, then click Save.
- Save as AppleScript application
Script Editor File menu: press the option key, and choose Save As…
- Name : same basename as above (without extension)
- Folder: Desktop
- File Format : Application (changes your filename to an .app extension
- Check Hide Extension, nothing else checked, and then Save
- Usage
- Interactive. Double-click on the Desktop application icon. An open panel will appear. You must select something, don't just click Select.
- Drag & Drop. Drag file(s) and folder(s) onto the Desktop application icon.
Code: (copy/paste everything below this word into Script Editor)
-- p5top9export.applescript
-- Both an interactive (double-click), and a drag/drop ready script that can handle
-- any combination of Pages files and (nested) folders containing them. Script will
-- check each Pages document and only process Pages v5 documents. Exported documents
-- written back to original location. A foo.pages will be exported as foo_p9.pages.
-- One can subsequently search for just _p9 in the Finder window to locate the files.
-- Version 1.1, Tested on OS X El Capitan 10.11.3, with Pages v5.6.1
-- VikingOSX, Feb 20, 2016, Apple Support Community
property default_folder : POSIX path of ((path to desktop) as text)
-- property default_folder : POSIX path of ((path to documents folder) as text)
-- property default_folder : POSIX path of ((path to home folder) as text)
property fileCnt : 0 as integer
property fileKind : "Pages 09"
-- user interactive via double-click
on run
try
set fileObjects to {}
set returnObjects to open_files_folders(default_folder)
if returnObjects contains "None" then
error number -128
else if returnObjects is equal to POSIX path of (text 1 thru -2 of default_folder) then
display alert ¬
"Actually choose something. Don't just click the Select button." as critical giving up after 10
return quit
end if
-- roll our own alias file list
repeat with i from 1 to (count of paragraphs in returnObjects)
copy paragraph i of returnObjects as POSIX file as alias to end of fileObjects
end repeat
logfileObjects as text
-- gets passed to the drag/drop handler below
openfileObjects
on error errmsg number errnbr
error_handler(errnbr, errmsg)
end try
return
end run
-- for drag & drop
on openfileObjects
set pages_list to {}
repeat with i from 1 to the (count of items of fileObjects)
set afile to (item i of fileObjects) as alias
tell application "Finder" to set akind to kind of afile
if akind contains "Folder" then
tell application "Finder"
set pages_list to (every item in entire contents of folder afile whose kind is "Pages Publication") as alias list
end tell
repeat with pages_file from 1 to (count of items of pages_list)
export_file(itempages_file of pages_list)
end repeat
else if akind contains "Pages Publication" then
export_file(afile)
end if
end repeat
if fileCnt > 0 as integer then
display notification "Files exported to " & fileKind & ": " & (fileCnt as text) with title "Pages v5 File Export Facility" subtitle "Processing Complete"
set fileCnt to 0 as integer
end if
return
end open
on is_pages5(afile)
return do shell script "python <<'EOF' - " & afile & "
#!/usr/bin/python
# coding: utf-8
''' Check if Pages v5 document. Returns True or False '''
import zipfile
import os
import sys
pfile = sys.argv[1]
p5zip = 'Index/Document.iwa'
p5pkg = 'Index.zip'
status = False
if not os.path.isdir(pfile):
with zipfile.ZipFile(pfile, 'r') as zf:
if p5zip in zf.namelist():
status = True
elif os.path.exists(os.path.join(pfile, p5pkg)):
status = True
print(status)
EOF"
end is_pages5
on export_file(efile)
try
set pfile to POSIX path of (efile as text)
if (is_pages5(pfile) as boolean) = true then
-- strip ".pages" extension from the filename
if (efile as text) ends with ":" then
-- package folder
set exportDocumentFile to text 1 thru -8 of (efile as text)
else
-- single file format
set exportDocumentFile to text 1 thru -7 of (efile as text)
end if
logexportDocumentFile as text
-- Distinctive filename that avoids overwriting Pages v5 document
-- foo.pages is now foo_p9.pages in the original folder location
set exportDocumentFile to exportDocumentFile & "_p9" & ".pages" as text
tell application "Pages"
set exportFormat to (Pages 09 as constant)
set thisDoc to open efile
delay 1
with timeout of 1200 seconds
exportthisDoctofileexportDocumentFileasexportFormat
end timeout
closethisDocsavingno
set fileCnt to (fileCnt + 1) as integer
end tell
tell application "Finder"
-- Pages export hides extensions. Make visible.
set extension hidden of fileexportDocumentFile to false
end tell
end if
on error errmsg number errnbr
error_handler(errnbr, errmsg)
set fileCnt to 0 as integer
tell application "Pages" to if it is running then quit
end try
return
end export_file
on open_files_folders(openfolder)
return do shell script "/usr/bin/python <<'EOF' - " & openfolder & "
#/usr/bin/python
# coding: utf-8
from Foundation import NSURL, YES
from AppKit import NSOpenPanel, NSMakeRect, NSApp, NSOKButton
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',
'public.directory']
theTitle = 'Open Panel'
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.setShowsHiddenFiles_(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 open_files_folders
on error_handler(nbr, msg)
display alert "[ " & nbr & " ] - " & return & msg giving up after 15
return
end error_handler