Apple Event: May 7th at 7 am PT

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

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

This application will silently exclude documents created by Pages '09 or earlier, and no notification panel will appear under these circumstances. Otherwise, it just works, not only with what I posted, but what I copied back and compiled into an application per the instructions.


If you saved the AppleScript source before saving again as an Application, do the following:

Open Script Editor

  1. Open Recent : get the file representing the saved AppleScript source (.applescript)
  2. At the bottom of the Script Editor, click that icon that looks like a document.
  3. Put that so-called Pages v5.6.1 document on your Desktop
  4. Run the script, choosing the Desktop Pages document from (3).
  5. When done, scroll upward in the Replies log at the bottom of Script Editor
  6. Does the following show True or False? If False, it was a Pages '09 document and was not processed.
    User uploaded file

Feb 20, 2016 10:30 AM in response to VikingOSX

So this is the code I copy and paste:


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

log fileObjects as text

-- gets passed to the drag/drop handler below

open fileObjects


on error errmsg number errnbr

error_handler(errnbr, errmsg)

end try

return


end run


-- for drag & drop

on open fileObjects

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(item pages_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

log exportDocumentFile 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

export thisDoc to file exportDocumentFile as exportFormat

end timeout

close thisDoc saving no

set fileCnt to (fileCnt + 1) as integer

end tell


tell application "Finder"

-- Pages export hides extensions. Make visible.

set extension hidden of file exportDocumentFile 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


I run Compile, Save as, drag the Pages document on the app, but nothing happens. Am I missing something? Do I have to run or record the script from the editor before saving it?

Feb 20, 2016 10:58 AM in response to janonymous

You have not stated what version of OS X you are using. If Pages v5.6.1 is currently running (dot under the icon) then quit via the Pages menu : Quit.


Running output from the AppleScript source will appear in that bottom Replies window, if you selected the bottom icon as I mentioned. It will not appear if you saved the content as an application, because what is then shown in the Script Editor is the application code, not the source code.


You have simply reposted the same code that I posted. As I mentioned, I copy/pasted this same code back into my El Capitan Script Editor. Whether running from the AppleScript source, or the saved application, the code performs as it did in my testing — interactively, or via drag/drop. I no longer have Mavericks and Yosemite installed here, so can not test further on those platforms.


In the Finder, control-click on that Pages document that won't. Do you see a Show Package Contents menu item? If so, do you see an Index.zip, or an index.xml.gz file inside? The former is a Pages v5 document, and the latter in Pages '08, and '09 documents. If there is no Show Package Contents then in the Terminal:


# assumption: the Pages document in question is on your Desktop. Same internal file criterion applies as it did above.

$ cd ~/Desktop

$ unzip -l ./foo.pages

Quit the Terminal.

Feb 20, 2016 12:05 PM in response to VikingOSX

Ah man, I'm so sorry for wasting your time. I'm embarrassed.

So I just went through the whole thing line by line, and I realized that there are language-sensitive parts in there! Duh! Since I'm on my dad's iMac and the primary language was set to something other than English--nada. I set it back to English and did a restart . . . and voila! The script works like a charm. It took me 30 seconds to set up.

Again, thank you so much!

One more thing: Could I actually modify the script to work with Numbers and Keynote without a hassle? It's not that important, though, since most relevant files are Pages docs anyway.

Feb 20, 2016 1:19 PM in response to VikingOSX

Unfortunately, there's a downside to this, too. It really only works with documents created with the most recent versions of Pages. I just tried a couple docs from 2015 (Pages 5-something), and neither can I open them with Pages '09 or Pages 5.2.2 (which is the last supported version running on Mavericks), nor can I convert them using this script. That means the affected docs would be lost in nirvana when I downgrade to Mavericks--which is the whole point here. I would have to either export those as '09 docs, or re-open, re-save, and update them to 5.6.1 and then run the script on those. Both ways involve lots of manual steps after all . . .

Feb 20, 2016 1:46 PM in response to VikingOSX

By the way, this is the error message I get with some of the documents:


[ 1 ] -

Traceback (most recent call last):

File "<stdin>", line 14, in <module>

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile .py", line 756, in __init__

self.fp = open(file, modeDict[mode])

IOError: [Errno 2] No such file or directory: '/Users/########/########/########/########'

Feb 20, 2016 1:55 PM in response to VikingOSX

This is the error message I get when I try to open them with Pages '09 (Mavericks and El Capitan):


“########.pages” couldn’t be opened.

The required index.xml file is missing.

This is the error message I get when I try to open them with Pages 5.2.2 (Mavericks):

You need newer versions of OS X and Pages to open this document.

Install the latest version of OS X, then download the latest version of Pages from the Mac App Store.

Feb 20, 2016 2:08 PM in response to janonymous

I just dragged and dropped a Pages document created with Pages v5.1 (2014) on Mavericks onto this application and it exported a Pages '09 document. Same drill with a Pages 2.0 document created on IOS 6, and it converted correctly to a Pages '09 document on my Mac. I then took this script that you cannot get to work (!!) and copied it to another machine entirely, still running El Capitan 10.11.3 and Pages v5.6.1. It worked with every Pages 5 document that I fed it.


No document created by any release of Pages v5 can be opened in Pages '09, because the document formats are entirely different. Pages v5.6.1 documents are not backward compatible to Pages v5.2.2 until you change them to package folder documents from their default single file format. You do this from the Pages v5.6.1 File menu : Advanced : Change File Type : Package. And follow this with a File menu : Save. Now you have a Pages document that is backwards compatible with Mavericks.


I didn't write the script for those who are downgrading to Mavericks, but nothing in the second paragraph is the fault of my functional script.

Feb 20, 2016 2:19 PM in response to janonymous

The messages that you are receiving on Mavericks have absolutely nothing to do with my script or its exported documents.


All of these messages that you mention are the result of Pages '09 still running, and you attempt to open a Pages v5 document with it — which will warn you about a missing index.xml file, and about the need for a newer version of pages (because you cannot open Pages v5 documents in Pages '09).

Feb 20, 2016 2:32 PM in response to janonymous

That is a internal Python zipfile library error regarding something about that particular file. The specific code is looking inside a single file format Pages 5 document to only confirm that the Index.zip is present. If that document is damaged, or mounted from another filesystem with different permissions, that would wreak havoc. If it is an alias or link to a Pages document, I don't deal with those.


I have now passed about 50 Pages v5 documents from 2014 to present through this script and each one has exported without a whimper from the application. Certainly no Python errors.

Feb 20, 2016 4:07 PM in response to VikingOSX

“No document created by any release of Pages v5 can be opened in Pages '09, because the document formats are entirely different.”

I’m aware of that. I was only quoting the error messages for the sake of completeness. I wasn’t actually trying to open those, I was testing some of the docs on a different machine (with Mavericks installed). By the way, nowhere did I claim that it was your fault.

I already said that I intend to convert them to Pages ’09 docs on El Capitan with 5.6.1 so I can open them with older versions of pages on Mavericks in the future—and it works. So that’s not the issue here.

The issue is that, for some reason, I get the Python error for some of the files.

“That is a internal Python zipfile library error regarding something about that particular file. The specific code is looking inside a single file format Pages 5 document to only confirm that the Index.zip is present. If that document is damaged, or mounted from another filesystem with different permissions, that would wreak havoc. If it is an alias or link to a Pages document, I don't deal with those.”

I can assure you that none of the docs are corrupt or damaged. They’re not aliases or links either. I can open and modify them just fine.

The only thing I can think of right now is, could it be that those particular docs were once converted from Microsoft Office .doc files? Could that wreak the havoc?

Feb 20, 2016 6:17 PM in response to janonymous

The short answer to those Python error messages is that you are selecting a filename that has white-space, or some other UNIX illegal character in it. Because the Python script is invoked from an AppleScript do shell script construct, it effectively believes it is running from the UNIX command-line, and expects properly formed filenames as input. It should work fine for you with filenames without white-space.


I am looking to see how to manage this issue.

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.