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 7:12 PM in response to VikingOSX

Ok. I have solved (and tested) the script handling of files with white-space in their names. Replace the previous v1.1 script with this updated v1.2 version. To accommodate Numbers and Keynote would require substantial changes due to scripting dictionary differences. The Python code would not change except for the initial Open Panel allowed filetypes. I don't have time to spend on extending this beyond Pages.


Code:


-- 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.2, Tested on OS X El Capitan 10.11.3, with Pages v5.6.1, fixed white-space filename issue

-- 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 (number of paragraphs of returnObjects)

set theFile to (paragraph i of returnObjects) as POSIX file as alias

copy theFile to the end of fileObjects

end repeat


-- 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's quoted form & "

#!/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

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.