Batch convert Word .docx files to Pages

Solution 1:

I do not have macOS Monterey or an M1 to test on, however, the example AppleScript code, shown below, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

The use of this script requires some initial setup for it to work.

  1. Place all .docx documents in their own folder so the target folder can be selected when the script runs. A list of all the .docx documents in the target folder will be created to loop through.
  2. Open Pages and create a blank document, saving it in the folder you want the other documents saved to, and then minimize it. This is to allow Pages to say open while opening and closing the target documents, thus making the overall process take less time. No other Pages documents should be opened. When naming the document make it something that is not within the names of the other documents to be processed.
  3. Open Script Editor and copy and paste the example AppleScript code into a new document, click the Compile button (a hammer icon) or press ⌘K to make sure there are not any compile related issues.
  4. Click the Run button (right-pointing triangle icon) or press: ⌘R

Let the script run uninterrupted until it's finished (or errors).


Example AppleScript code:

property theWordDocumentsList : missing value
property |Pages| : name of ¬
    application id "com.apple.iWork.Pages"

--  # Choose the target folder containing
--  # the docx documents for processing.    

tell application id "com.apple.finder" to ¬
    set theWordDocumentsList to ¬
        (files of container (choose folder) ¬
            whose name extension is "docx") ¬
            as alias list

--  # Loop through: theWordDocumentsList 

repeat with thisDocument in theWordDocumentsList
    
    --  # Open the document.
    
    tell application id "com.apple.iWork.Pages"
        activate
        open thisDocument
    end tell
    
    --  # Give a moment for 'Opening "filename.docx"…' to appear.
    
    delay 0.2
    
    tell application id "com.apple.systemevents"
        
        --  # Wait for 'Opening "filename.docx"…' to disappear.
        
        repeat while (role description of ¬
            UI element 4 of window 1 of ¬
            application process |Pages|) is "button"
            delay 0.1
        end repeat
        
        --  # Click 'Save As… ⌥⇧⌘S' to save as a Pages document.
        
        key code 1 using {shift down, option down, command down}
        
        --  # Wait for the 'Save' button to be available.
        
        repeat until exists ¬
            button "Save" of sheet 1 of window 1 of ¬
            application process |Pages|
            delay 0.1
        end repeat
        
        --  # Press the 'Save' button.
        
        key code 36 --  # enter key
        
        --  # Wait for the 'Save' button to to disappear.
        
        repeat while exists ¬
            button "Save" of sheet 1 of window 1 of ¬
            application process |Pages|
            delay 0.1
        end repeat
        
    end tell
    
    --  # Close the document.
    
    delay 0.2
    tell application id "com.apple.iWork.Pages" to close document 1
    delay 0.5
    
end repeat

--  # Notify user processing if finished.

tell current application to activate
display alert "Processing finished!…"

Notes:

As the script uses UI Scripting it can be kludgy and prone to failure for a number of different reasons. For example, changes in the UI from one version of macOS to another or the target application. Trying to multi-task while it's running, which is a no-no for this type of script! The value of the delay commands outside of the repeat loops need adjusting.

I tested with a 10 document sample set with the documents ranging from 1 to 7 pages and on my system, a 16" 2019 MacBook Pro 2.3 GHz 8-Core Intel Core i9, and it took less than 40 seconds. This would equate to ~6.5 minutes for 100 documents. Obviously this will vary based on size of the documents and the speed of the system it's run on.

You can always test with, or run in several groups, with a smaller number of documents.

Although it could be added, I did not include any error handling around existing documents if the script fails and needs to be restarted, so be aware that the .docx documents which have already been converted need to be manually removed before starting the script again.




Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.