Converting pages documents to other formats

I'm trying to convert an awful lot of pages documents into word or .pdf format for auditing and accounting purposes - the professionals requesting the docs are using windows machines so I can't just email a folder.


I know how to do this on individual documents by simply choosing to export in a particular format whilst using pages, but I'd like to know if it's possible with batches. I also know how to open a print queue and just send in paper format but this is a little expensive as I have about 500 files!


I had a quick look at automator but there is no means of accessing documents for a workflow I can find, and I have no experience of this function anyway, and multiple selection in pages doesn't seem to be an option as it only seems to deal with one file at a time in the window.


Anyone know a way of doing this please? I'd be very grateful indeed for a method.

iMac, macOS Sierra (10.12.3), OS X on SSD, thunderbolt enclosure.

Posted on Jun 24, 2017 1:38 PM

Reply
14 replies

Jun 25, 2017 7:55 AM in response to killcondo

The following AppleScript in the scrollable window, is a batch export facility for Pages v6 or later on macOS Sierra. Using Finder, one simply drags and drops Pages document(s), and/or folder(s) containing Pages documents onto the saved application icon on the Desktop. Every Pages document is then exported to its original location with only the file extension changed to the exported document type. On conclusion, a dialog will appear with the file count.


I do not deliberately set out to make an application with a single operating system or application version restriction, but this script with identical code, and using the same version of AppleScript, fails to run properly on El Capitan as it does on macOS Sierra. I did not want to write two different versions for each operating system with additional testing/debugging time investment. I left out the RTF export because it was only first available in Pages v6.2.


Instructions:


  1. From the Dock, click Launchpad : Other : Script Editor
  2. Copy and paste the entire contents of the AppleScript code below into the Script Editor.

    Click the hammer icon to change the purple text to multiple colors. The latter is a sign that there are no syntax errors (there won't be unless the copy/paste was incomplete).

  3. In the Script Editor
    1. Save the AppleScript source as a text file for safe keeping.

      File menu : Save...
      User uploaded file

    2. Now, save as an Application that you place on your Desktop

      Option key + File menu : Save As...
      User uploaded file

    3. The drag & drop application icon will appear on your Desktop
      User uploaded file
    4. Quit Script Editor (command + Q)
  4. You can now drag and drop multiple Pages documents, and/or folders containing Pages documents onto the application icon. You will observe a dark gray square appear around the application icon when you can drop the items on it.
    1. When it runs, the application will present you with an export type selection window:
      User uploaded file
    2. Once you have made your selection, each file will be processed by Pages to the export type. As each file is processed by Pages, it will open and appear momentarily on screen. There is no provision to avoid this flashing of documents during export.
    3. After each export, the Finder will be told to display the extension of the document, rather than the default of hiding it.
    4. On completion, a dialog will appear with the export kind selected, and the total file count of processed Pages documents.
      User uploaded file
    5. Done.


AppleScript source to copy/paste into the Script Editor:



-- pages_export.applescript -- -- Usage: Drag & Drop Pages document(s), and/or folder(s) containing them. -- Recursively processes folder contents, and output files written to same -- location with new extension. -- Version: 1.5 -- Tested: Pages v6.2 on macOS Sierra 10.12.5. Code will only work on Sierra or (maybe) later. -- Issues: 1) Attempts to test on El Capitan failed with same version of AppleScript, and -- with Pages v6.2 specific RTF code references removed. -- 2) RTF export appears to be text only, and not RTFD supporting document objects -- VikingOSX, 2017-06-25, Apple Support Communities property ca : current application property export_choices : {"PDF", "DOCX", "DOC"} property msg : "Choose Pages Export Format:" & return & "DOCX (default - return):" property exportFormat : "" property valid_kind : {"Pages document", "Pages Publication"} property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns" use framework "Foundation" use AppleScript version "2.4" -- Yosemite 10.10.0 or later use scripting additions on run -- only if the user double-clicks the application icon display alert "Drag and Drop Pages documents and/or folder(s) of Pages documents" & return & ¬ "onto the application icon. Make your export choice from the export type selection." return end run on open dropped_items global fileCnt set fileCnt to 0 tell application "Finder" to set sys_version to version considering numeric strings if sys_version < "10.12.0" then display alert ¬ "You need macOS Sierra 10.12 or later to run this script." giving up after 10 return end if end considering tell application "Pages" to set aversion to version of it considering numeric strings if aversion < "6.0" then display alert "You need Pages v6, or later to run this script" giving up after 10 return end if end considering try set exportFormat to (choose from list export_choices with prompt msg ¬ default items {"DOCX"} multiple selections allowed false ¬ with empty selection allowed) if exportFormat is false then error number -128 -- user cancelled set exportFormat to lowerCase(item 1 of exportFormat) on error errmsg number errnbr my error_handler(errnbr, errmsg, "dropped_items") set exportFormat to "" return end try repeat with anItem in the dropped_items try -- necessary for proper file counting with mixed drag & drop files and folders xattr_quarantine_remove(anItem) tell application "Finder" set isFolder to folder (anItem as text) exists if isFolder = true then set docList to (every item in entire contents of folder anItem ¬ whose valid_kind contains kind of it) as alias list repeat with afile in docList my export_file(afile) end repeat else if valid_kind contains kind of anItem then my export_file(anItem) end if end tell on error errmsg number errnbr my error_handler(errnbr, errmsg, "dropped Items repeat") tell application "Pages" to if it is running then quit set exportFormat to "" quit end try end repeat display dialog "Pages Export Complete." & return & tab & " Files exported to " & ¬ exportFormat & " : " & (fileCnt as text) ¬ buttons {"Done"} default button "Done" with title "Pages Export Facility" with icon POSIX file app_icon as alias tell application "Pages" to if it is running then quit set exportFormat to "" return end open on export_file(theFile) global fileCnt -- keep full path to theFile and only change its extension set exportDocument to new_extension(theFile, exportFormat) as POSIX file -- necessary to avoid export write permissions issues on Sierra close access (open for access exportDocument) tell application "Pages" try set myDoc to open theFile with timeout of 1200 seconds if {"docx", "doc"} contains exportFormat then export myDoc to file exportDocument as Microsoft Word else if (version of it) ≥ "5.6" then export myDoc to file exportDocument as PDF with properties {image quality:Best} else export myDoc to file exportDocument as PDF end if end timeout set fileCnt to fileCnt + 1 close myDoc saving no on error errmsg number errnbr my error_handler(errnbr, errmsg, "export file") tell application "Pages" to if it is running then quit set exportFormat to "" return end try end tell -- force extension visible on all exported documents tell application "Finder" if exists (file exportDocument as alias) then set extension hidden of (file exportDocument as alias) to false end if end tell return end export_file on xattr_quarantine_remove(afile) -- Apple sets a quarantine bit on every saved Pages document. This bit interferes -- with proper file counting when dropping mixed files and folders on drag/drop apps. -- We attempt to remove this quarantine bit on files and recursively within folders -- whether it is set or not. try set xItem to (POSIX path of afile as text)'s quoted form do shell script "xattr -dr com.apple.quarantine " & xItem end try return end xattr_quarantine_remove on new_extension(afile, ext) -- strip extension from file path and replace with new extension set fullPath to ca's NSString's alloc()'s initWithString:(POSIX path of afile) return ((fullPath's stringByDeletingPathExtension)'s stringByAppendingPathExtension:ext) as text end new_extension on lowerCase(astr) -- make passed string all lowercase return ((ca's NSString's alloc()'s initWithString:astr)'s lowercaseString()) as text end lowerCase on error_handler(nbr, msg, handler_name) return display alert handler_name & ": " & "[ " & nbr & " ] " & msg as critical giving up after 10 end error_handler

Jun 28, 2017 12:58 PM in response to killcondo

FWIW, I have been using a short script (no bells and whistles) that takes Pages documents it finds in a folder and exports them as Word documents that it saves in the same folder. Instead of drag-drop, it's run by clicking the 'run' button in Script Editor (in Applications > Utilities), and, at the prompt, navigating to the folder.


To get your Documents back into the Finder sidebar you can try going to Finder > Preferences and making sure Documents is checked:


User uploaded file


SG



-- exports Pages docs in folder to Word format in same folder

-- copy-paste into Script Editor, click 'run' and navigate to folder

set theFolder to choose folder

tell application "Finder" to set theDocs to theFolder's items

repeat with aDoc in theDocs

set docName to aDoc's name as text

if docName ends with ".pages" then

set exportName to (theFolder as text) & docName

set exportName to exportName's text 1 thru -7 & ".docx"

tell application "Pages"

open aDoc

delay 1.3 -- may need to increase this

tell front document


close access (open for accessexportName)


exporttofileexportNameasMicrosoft Word


close

end tell

end tell

end if

end repeat

Jun 30, 2017 6:20 AM in response to VikingOSX

Previously posted, the v1.5 version of this AppleScript drag & drop application did not support exporting content to a user specified folder. The v2.0 release, posted here today, does offer a query about whether the user wants a separate export folder (default), or just write the exported documents back to their original filesystem location. I have also successfully tested v2.0 with a mounted USB stick.


The v2.0 version will expect OS X Yosemite (10.10) or later, and Pages v5.5.3 or later, though my scope of testing was restricted to El Capitan and Sierra.


In the following, new panel, press return (default) to receive a choose folder prompt, where you can either create a new export folder, or select an existing one. If you change the 'Y' to 'n', and press return, then no export folder will be used.

User uploaded file


The list of export formats will be presented as before, with a return key selecting DOCX by default. This script will run 3x faster on El Capitan (Pages v5.6.2) than on Sierra with Pages v6.2. Depending on the quantity of Pages documents dropped on it, this application can take awhile before actual exports begin. The opening, and flashing of Pages for each document is an unavoidable distraction.


Installation, and usage instructions remain as they were for v1.5.


-- pages_export.applescript -- -- Usage: Drag & Drop Pages document(s), and/or folder(s) containing them. -- Recursively processes folder contents, and exported files written to specified -- folder (default), or to original file location with new extension. -- Export supported and tested: PDF, docx, and doc. -- Export to USB stick tested successfully. -- To be saved as AppleScript Application -- Version: 2.0 -- Tested: OS X El Capitan 10.11.6, macOS Sierra 10.12.5 -- 3x faster on El Capitan with same version of AppleScript -- Require: OS X 10.10 or later. Pages v5.5.3 or later. Application will check. -- VikingOSX, 2017-06-30, Apple Support Communities property ca : current application property fmsg : "Choose or Create Export Folder:" property fdefault : (path to desktop) as alias property export_choices : {"PDF", "DOCX", "DOC"} property msg : "Choose Pages Export Format:" & return & "DOCX (default - return):" property exportFormat : "" property expFolder : "" property valid_kind : {"Pages Publication", "Pages document"} property fileCnt : (0 as integer) property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns" use framework "Foundation" use AppleScript version "2.4" -- Yosemite 10.10 or later use scripting additions on run -- only if the user double-clicks the application icon display alert "Drag and Drop Pages documents and/or folder(s) of Pages documents" & return & ¬ "onto the application icon. Make your export choice from the export type selection." quit end run on open dropped_items tell application "Finder" to set sys_version to version considering numeric strings if sys_version < "10.10" then display alert ¬ "You need OS X Yosemite 10.10 or later to run this script." giving up after 10 quit end if end considering tell application "Pages" to set aversion to version of it considering numeric strings if aversion < "5.5.3" then display alert "You need Pages v5.5.3 or later to run this script" giving up after 10 quit end if end considering try set choice to text returned of (display dialog ¬ "Exported files to separate folder (Y/N)?" default answer "Y") if {"Y", "y"} contains choice then repeat while true set expFolder to (choose folder with prompt fmsg default location fdefault ¬ without invisibles, multiple selections allowed and showing package contents) if not expFolder is equal to fdefault then exit repeat end repeat end if set exportFormat to (choose from list export_choices with prompt msg ¬ default items {"DOCX"} multiple selections allowed false ¬ with empty selection allowed) if exportFormat is false then error number -128 -- user cancelled set exportFormat to lowercase (item 1 of exportFormat) on error errmsg number errnbr my error_handler("dropped items prep", errnbr, errmsg) quit end try repeat with anItem in the dropped_items try tell application "Finder" set akind to kind of anItem if akind is equal to "Folder" then set docList to (every item in entire contents of folder anItem ¬ whose kind is in valid_kind) as alias list repeat with afile in docList my export_file(afile) end repeat else if akind is in valid_kind then my export_file(anItem) end if end tell on error errmsg number errnbr set errval to "Dropped Items repeat - " & (anItem as text) my error_handler(errval, errnbr, errmsg) quit end try end repeat display dialog "Export Complete." & return & tab & " Files exported to " & ¬ exportFormat & " : " & (fileCnt as text) ¬ buttons {"Done"} default button "Done" with title "Pages Export Facility" with icon POSIX file app_icon as alias quit end open on export_file(theFile) try set exportDocument to new_extension(theFile, exportFormat, 0) as POSIX file if not expFolder = "" then -- because Finder can't get name of non-existent file at this point set basename to new_extension(theFile, exportFormat, 1) set exportDocument to (expFolder & basename) as text end if -- necessary to avoid export write permissions issues on Sierra close access (open for access exportDocument) on error errmsg number errnbr my error_handler("Export file exportDocument", errnbr, errmsg) quit end try tell application "Pages" try set myDoc to open theFile with timeout of 1800 seconds if {"docx", "doc"} contains exportFormat then export myDoc to file exportDocument as Microsoft Word else if (version of it) ≥ "5.6" then export myDoc to file exportDocument as PDF with properties {image quality:Best} else export myDoc to file exportDocument as PDF end if end timeout set fileCnt to (fileCnt + 1) as integer close myDoc saving no on error errmsg number errnbr my error_handler("Export file", errnbr, errmsg) quit end try end tell -- force extension visible on all exported documents try -- occasionally get -8058 error which is caused by trying to duplicate -- a mounted drive with the Finder -- which is nonsense here, so ignore. tell application "Finder" if exists (item exportDocument as alias) then set extension hidden of (item exportDocument as alias) to false end if end tell on error errmsg number errnbr if not errnbr = -8058 then set errval to "Finder ext hidden: " & exportDocument as text my error_handler(errval, errnbr, errmsg) quit end if end try return end export_file on new_extension(afile, ext, name_flag) -- handler has two roles based on name_flag value -- 0) return full path of original filename with replacement extension -- 1) return just the basename without path set fullPath to ca's NSString's alloc()'s initWithString:(POSIX path of afile) set newfile to (fullPath's stringByDeletingPathExtension)'s stringByAppendingPathExtension:ext if name_flag = 1 then return (newfile's lastPathComponent()) as text end if return newfile as text end new_extension on lowercase (astr) -- make passed string all lowercase return ((ca's NSString's alloc()'s initWithString:astr)'s lowercaseString()) as text end lowercase on error_handler(nbr, msg, handler_name) return display alert handler_name & ": " & "[ " & nbr & " ] " & msg as critical giving up after 10 end error_handler on quit {} -- perform these cleanup items, and then really quit tell application "Pages" to if it is running then quit set fileCnt to (0 as integer) set exportFormat to "" set expFolder to "" continue quit end quit

Jun 28, 2017 1:03 PM in response to killcondo

Open a new Finder window. Click on your Documents folder, and then press and hold the cmd key, as you drag, position, and drop the Documents folder to its preferred placement among the sidebar Favorites. That simple.


Separating the PDF from the Pages documents.

Make a separate folder that you want the exported PDF documents to be placed.

  1. In the Finder Preferences : Advanced tab, you want to select, “Search the Current Folder” for the When performing a search: entry.
  2. Open a new Finder window, and then open the folder where the original Pages documents were dropped onto the application. You can obtain a list, or icon view of all of the PDF documents in this folder hierarchy by typing the following in the Finder window's search field: kind:PDF
    1. No other document kind will display but PDF. Click on one of these PDF, then press cmd+A to select the remainder. Right-click on any of these to get the copy nn items menu item, and select it. Double-click the recipient folder for all of these PDF, press and hold the option key, then right-click in the PDF folder, and select move nn items.
    2. You will have now moved all of the exported PDF files from the original Pages folder.


Yes, that is a bandaid for now. I can programmatically prompt you for a PDF destination folder, allow you to create it if not already present, and then change the code to write exported PDF into that destination. Not too much additional code, but it will require testing time. I won't get to it until June 29, for possibly a June 30 repost here.

Jun 24, 2017 2:03 PM in response to killcondo

What specific version of Pages are you using? Pages application menu : About Pages. What version of OS X/macOS?


Batch processing file exports from Pages involves a custom Applescript that allows you to select either a single Pages document, or folder of Pages documents, which are then either exported to Word .docx, or PDF. I have batch scripts for both export formats. Each exported document is written back to the same location with just the file extension changed.


If the recipients on Windows will only be reading the exported content, then I recommend PDF. Pages, regardless of the version, is not a Word clone, and it translates from Pages proprietary format to Word docx. There is no assurance that what Windows users receive in the form of an exported Word document will appear as you see it in Pages — with their Word application.


Once I know the version of Pages, I can post the script here, and instructions for you to install and use it.

Jun 24, 2017 2:47 PM in response to VikingOSX

Sorry, forgot to mention I'm on Mac OS Sierra 10.12.5


I've sent stuff in Word format before and almost as much success as .pdf, they like Word as they can edit irrelevant components - it's one of many reasons I prefer macs, they open pretty much anything! After four years I'm still finding my way round them, Script Editor being another feature that's lain dormant and I don't know the first thing about Automator.


Thanks again for taking the trouble.

Jun 28, 2017 11:20 AM in response to VikingOSX

Thank you!


It worked beautifully, except I need to separate, somehow, all the pdfs from the pages documents...


Also, I rather stupidly tried to drop the documents folder from the sidebar onto the export icon and it disappeared.


I don't think I can work with computers any more, this is a real watershed moment - 'documents' with its icon disappeared from the pages and finder sidebar, I can only hope a TM backup will restore whatever I've messed up. The 'kind' column has also disappeared (used to be next to the date modified column until 5 minutes ago) and for the last year anything I save in iCloud pages will not go into iCloud drive, even though Apple's tech guys insist it will populate at some undefined time. It's another world.


Otherwise ok, no problems.


I really think I'm going to give up and just sell my macs now.


I am so grateful for the script though, it did work!

Jun 29, 2017 2:50 AM in response to SGIII

Thanks SG, wits' end and all that.

Documents doesn't appear in this list, only in iCloud - I wonder if it's because I'm running an external SSD for macOS?

There are documents on the HDD but maybe I've changed paths or iCloud settings have disabled this feature?

Sorry to answer an answer with a question but I'm a little mystified. I'll try booting from the HDD but it takes about five minutes on the later OS (was pretty quick before Yosemite) as I know there are some there, and they appear in iCloud archive, a new folder I didn't know existed.

Thanks again for your help with this one and the script above, which I'll give a spin.

Best wishes

SL

Jun 29, 2017 2:55 AM in response to VikingOSX

Thanks Viking, I thought I was getting the hang of things after a few years but there's obviously a lot to learn!

That's a great band-aid, by the way, I'll try it later when I get the time, looks pretty straightforward and don't know why I didn't know this beautifully simple wheeze, although the cmd-A is something I must have forgotten...

I wonder if automator could do this sort of thing, although documents doesn't appear in the sidebar so probably not. Bit odd, I'd have thought document handling would be something it would do very well.

Hey thanks again, you're a star.

SL

Jun 29, 2017 4:16 PM in response to killcondo

I have augmented the previous AppleScript drag & drop application to now default to an export folder selection (either existing, or create a new folder) panel. The option remains to just allow the exported documents to be written back to their original filesystem path. Otherwise, it works as previous.


I have wrapped testing on macOS Sierra 10.12.5, and will test further on El Capitan 10.11.6 in the morning. I expect to have it reposted here by mid-afternoon Friday 6/30.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Converting pages documents to other formats

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