I need a way to batch convert .pages files into .docx or .txt

I have around 800 .pages files I need to convert to .txt - and I found the usual way on terminal - except it doesn't allow .pages files to be converted? It keeps saying my file does not exist or that it's not in the right format. So. I need a way to either convert all my files into .docx so I can use the terminal textutil -convert txt -

way to convert them all to .txt. Or a way to convert them directly to .txt.

Please help!!!

(Also, I'm not a coder or anything, so please don't use complicated language, thanks :'))

Posted on Jul 21, 2017 11:38 AM

Reply
13 replies

Jul 21, 2017 4:13 PM in response to tracyx11

I have tested this AppleScript on El Capitan 10.11.6 with Pages v5.6.2, and on macOS Sierra 10.12.6 with Pages v6.2, and in both cases, a dropped folder of Pages documents was successfully exported as plain text to the designated Test export folder. When processing is complete, a dialog will appear with the total files processed. Once you click done, the application will then quit.

User uploaded file

Once the AppleScript is saved to your Desktop as an application, you only need to drag and drop a file, files, or a folder of any depth containing Pages documents onto the application. It will then interact with you once — allowing you to select an existing folder, or create one, before commencing the export to text process for each file. It is normal for Pages to briefly flash each document to the screen during the individual export process.


Setup.


  1. From Launchpad : Other : Script Editor.
  2. Copy/paste the content of the following scrollable AppleScript code into the Script Editor
    1. Click the hammer icon on the toolbar to compile it. The text will change from purple to colors if there are no errors in the copy process. The code won't run with compile time errors anyway.
    2. Save the AppleScript code as Applescript (your backup)

      File menu : Save...

      1. Save As: pages_text_export (Documents folder or your preferred location)
      2. File Format: Text (don't change the Line Endings)
      3. Hide extension: unchecked (this will allow the .applescript extensin in the Save As window
      4. Save
    3. Save the AppleScript code as an Application

      Option key + File menu : Save As...

      1. Save As: any name you want (on your Desktop for drag/drop)
      2. File Format: Application (leave the options unchecked)
      3. Hide Extension: Checked (your application name in Save As: will have no extension)
      4. Save
  3. Quit the Script Editor


Usage: drag and drop Pages documents or a folder of Pages documents onto the application icon.


Copy this content to the Script Editor per item 2 above:


property ca : current application property exportFormat : "txt" property fmsg : "Choose or Create Export Folder" property fdefault : (path to desktop) as alias property valid_kind : {"Pages Publication", "Pages document"} property export_folder : "" 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 or later use scripting additions 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 set export_folder to (choose folder with prompt fmsg default location fdefault ¬ without invisibles, multiple selections allowed and showing package contents) 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 my error_handler("main - repeat", 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 if not export_folder = "" then -- construct the new output folder file path with extension set basename to new_extension(theFile, exportFormat, 1) set exportDocument to (export_folder & basename) as text else -- back to original file path with new extension set exportDocument to new_extension(theFile, exportFormat, 0) as POSIX file end if on error errmsg number errnbr my error_handler("Export File", errnbr, errmsg) quit end try close access (open for access exportDocument) tell application "Pages" try set myDoc to open theFile with timeout of 1800 seconds -- export Pages document as plain text export myDoc to file exportDocument as unformatted text end timeout set fileCnt to (fileCnt + 1) as integer close myDoc saving no on error errmsg number errnbr my error_handler("Export File - Pages", 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 with new extension, but 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 error_handler(handler_name, nbr, msg) 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 export_folder to "" continue quit end quit

Dec 9, 2017 6:17 PM in response to VikingOSX

Hi VikingOSX


resurrecting a hopefully not too old thread. This script worked for one file of mine and then fails on the rest. It may be due to the fact that these files were originally written in Pages 4.1 on Snow Leopard. I have since opened and updated and saved each (ugh) on Pages 6.3.1, Sierra. I can't figure out why one worked and the rest give me errors:


Export File - Pages: [ 6 ] "<my file name>"


Export Complete.

Files exported to : 0


On top of that, when I test the file that worked the first time and send it to a different location, now it doesn't convert either! As far as I can tell there are no text boxes in any of these files. Weird. Please help if you get this, I can provide more info if needed.


thanks

jp

Dec 10, 2017 6:24 AM in response to redwood02

I am using macOS 10.13.2 and Pages v6.3.1. The script works on some files and not on others – and failing is not exclusive to Pages '09 documents. The script works as expected on El Capitan 10.11.6.


Aside from the new operating system, AppleScript got bumped up a version, and debugging a drag/drop script is time consuming. I have the offending handler identified, and when things are fixed with consistent results, I will post back here.

Dec 16, 2017 8:55 AM in response to redwood02

Update…


After considerable time debugging the previous AppleScript, I have determined that AppleScript v2.5 on High Sierra goes berserk when encountering user interaction (choose folder) in its drop handler. Thus, it loses track of the drop count, and repeats the user interaction multiple times. In AppleScript v2.4 (El Capitan, Sierra), this was not an issue.


Aside from the aberrant behavior, this means that on High Sierra, there can be no user prompt following the drag/drop of files to choose the export folder, and files will have to be exported back to their original filesystem location.


When I am satisfied that the revised AppleScript consistently works, I will post the update here.

Jul 21, 2017 12:52 PM in response to tracyx11

The only way to get Pages documents into text is 1) individually export them from Pages, or 2) Use AppleScript (yes, programmatically) to automate the process.


Why do you want these documents converted to text, and not just stop at Word .docx so formatting is retained? Almost everyone on the planet can open a Word document, and have the benefit of it already in a readable format.

Jul 21, 2017 2:12 PM in response to tracyx11

Ok. I have code to batch export Pages documents to Word or PDF, but it is a couple of lines to change to make it export to text. Do you want the output files to same exact folder hierarchy, with the .pages extension changed to .txt, or output to separate specified folder?


Also, understand that any text content inside Text boxes will not get exported, as all document objects will be dropped outbound to text.

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.

I need a way to batch convert .pages files into .docx or .txt

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