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

BATCH Convert Pages 5 to Pages '09

Is there a script or Automator that I can run to convert Pages 5 files to Pages '09 file?

I tried Pages 5 in November, created some files with it, didn't like it, now I need to convert those files into Pages '09 files.

Thanks

MacBook Pro (15-inch Late 2008), OS X Mavericks (10.9)

Posted on Feb 3, 2014 9:49 AM

Reply
32 replies

Mar 2, 2015 6:28 AM in response to tokeriis

This Pages 5 to Pages 9 workflow requires Yosemite so that Automator's Use Javascript action can be loaded, and your code executed. This workflow is entirely useless to anyone with Pages v5 on Mavericks, for that reason. You should have made this detail known. An AppleScript, or Scripting Bridge solution would have worked on both platforms.


Although the 4 lines of Javascript are concise, how many people that use it will remember to apply it to copies of originals, before this workflow overwrites them. There are no instructions in your zip file to accompany the workflow, or an initial Javascript alert that reminds users that this will overwrite their originals.

Mar 2, 2015 7:11 AM in response to Yellowbox

As I wrote, this is a quick hack adapted from other solutions, provided as is, and on the basis of my limited abilities. I tried to state that in my post and I thank you for clarifying the limitations of the solution.


I have provided a new version with warnings of file overwrite and Yosemite required: Pages 5 → Pages 09 Workflow.


Feel free to improve on the solution.

Feb 20, 2016 8:08 AM in response to zamboknee

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.

User uploaded file

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:

  1. From Launchpad (Dock) : Other, click on Script Editor to launch it.
    1. Choose New Document
    2. Copy/paste the indicated script code (below) into the Script Editor window (it will be purple)
    3. 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).
    4. There are two save steps:
      1. Save as the AppleScript source

        Script Editor File menu : Save…

        1. Name : p52p9export (no extension, and this name can be whatever you want to call it.
        2. Folder : Your documents folder
        3. File Format: Text (this will assign .applescript to your filename)
        4. Nothing else is checked, then click Save.
      2. Save as AppleScript application

        Script Editor File menu: press the option key, and choose Save As…

        1. Name : same basename as above (without extension)
        2. Folder: Desktop
        3. File Format : Application (changes your filename to an .app extension
        4. Check Hide Extension, nothing else checked, and then Save
  2. Usage
    1. Interactive. Double-click on the Desktop application icon. An open panel will appear. You must select something, don't just click Select.
    2. 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

Feb 20, 2016 9:12 AM in response to VikingOSX

Thank you, VikingOSX.


Unfortunately, it doesn't seem to work for me, even though I believe everything was done right. I followed the steps, and the script seems to be working just fine--at least it looks that way. The app opens, too, and I can select documents just fine, but there's no actual output after I run it. When I drag a document on the app, something seems to be happening, however, no additional file is being created. It does something, but I'm not sure what exactly.


I'm on El Capitan 10.11.2 and Pages 5.6.1.


PS: Is this script supposed to work with Numbers and Keynote, too?


Thanks again.

Feb 20, 2016 9:24 AM in response to janonymous

The script is exclusive to the Pages v5 (tested v5.6.1) application, and its documents. I just copied and pasted the script that I posted — into a new Script Editor document, compiled it (hammer icon), and ran it. It opened its file dialog, and when I selected a Pages v5 document (test.pages) on my Desktop, the file was immediately written back to the Desktop as test_p9.pages.


The only output from this application is the exported document and the final notification of the number of exported documents. It does not produce a dialog or report indicating what files were processed. If you selected, or drag/dropped a folder, the exported documents will be in that folder.

On what version of OS X, and Pages are you using this script? Are you getting any error messages?

Feb 20, 2016 9:41 AM in response to VikingOSX

I'm on El Capitan 10.11.2 and Pages 5.6.1.


Here's what I did:


1) Open the script editor

2) Paste the code (from "-- p5top9export.applescript" to "end error_handler")

3) Compile the code

4) Save the script as an app on desktop

5) Close the script editor

6) Drag and drop Pages 5.6.1 test document on app

7) No results whatsoever


I'm getting no error messages either.

BATCH Convert Pages 5 to Pages '09

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