Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Batch Convert pages to Word Document in multiple folders

I found an answer for one folder and must select both folders input and output by: VikingOSX

 Reference: Batch Convert pages to Word Document- Mac… - Apple Community

It worked perfectly but in my case I have multiple folders within main folder some have pages files and other don't, is there a way to look through including folders and if there is a pages file create a folder named "word" and convert all pages files into it?




MacBook Pro 15″, macOS 13.0

Posted on Dec 6, 2022 3:01 AM

Reply
Question marked as Best reply

Posted on Dec 6, 2022 10:09 AM

For this particular exercise, I chose an Automator application solution over a more complicated (read time consuming to debug) Shortcut solution. The code works on Ventura 13.0.1, and should also work on earlier versions of macOS.


It will prompt your for the parent folder that contains your subfolders and Pages document hierarchy. It works on an list of your Pages documents, determines for a given document location if a "Word" folder exists and makes one if missing. It then requests that Pages export that Pages document to .docx with that respective Word folder as the export target. When done, a completion dialog will appear showing how many Pages documents were processed and a count of Word documents exported.



Because of your volume (6000) of Pages documents, I chose to use the Zsh shell instead of AppleScript. With this volume of files, it will still run for a long while as the Pages export process will take its toll on the clock.


Steps:

  1. Launch Automator from your /Applications folder.
    1. From the initial Automator dialog select Application and then click Choose
    2. In order left to right, you will have a list of libraries, actions in those libraries, and the much larger (and currently) empty Workflow window.
  2. Configure Workflow actions
    1. Files & Folders library
      1. Drag and drop Ask for Finder Items action onto the Workflow window
      2. Start at: Desktop
      3. Type: Folder
      4. Do not select Multiple Selection
    2. Utilities library
      1. Drag and drop Run Shell Script action onto the Workflow window
      2. Shell: /bin/zsh
      3. Pass Input: as arguments
      4. remove all content in this action as it will be replaced by Code shown below.
    3. Save the Automator application (e.g. name it Pages2Docx) and you do not need to give it an extension
    4. Quit Automator
    5. Try the new application


Copy and paste the following code into the now blank Run Shell Script window.


Code:


#!/bin/zsh

: <<"COMMENT"
Do a recursive descent on a folder hierarchy gathering Pages document. Export these
documents from Pages as Word .docx documents into a Word folder at the file location.
If the Word folder does not exist at the particular folder location, then create it.
Tested: Ventura 13.0.1
VikingOSX, 2022-12-06, Apple Support Communities, No implicit warranty or support.
COMMENT

typeset -gi pagesCnt=0 docxCnt=0

STARTDIR="${@}"

function pages_export () {

    /usr/bin/osascript <<-AS
    use scripting additions

    tell application "Pages"
        activate
        try
            set infile to POSIX file "${1}"
            set outfile to POSIX file "${2}"

            set thisDoc to open infile as alias

            with timeout of 1200 seconds
                export thisDoc to file outfile as Microsoft Word
            end timeout
            close thisDoc saving no
        on error errmsg number errNo
            display dialog "[ " & errNo & " ]: " & errmsg
        end try
    end tell
    -- unhide the exported filename extensions
    tell application "Finder"
        if exists (item outfile as alias) then
            set extension hidden of (item outfile as alias) to false
        end if
    end tell
    return
AS
}

function completion () {
    /usr/bin/osascript <<-AS
    use scripting additions

    set DialogIcon to "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"

    display dialog "Pages documents found: " & "${1}" & return & ¬
        "Pages documents exported to Word docx: " & "${2}" with title "Processing Complete" with icon POSIX file DialogIcon
    return
AS
}

# ask user for the starting folder from a choose folder dialog
parent_folder="$(folder_prompt)"

# do a case-insensitive recursion finding Pages documents and sorting by name
setopt nocaseglob
for f in ${STARTDIR}/**/*.pages(.Non);
do
    (( ++pagesCnt ))
    # if there is no Word folder at this file location, then make one
    [[ -d "${f:a:h:r}/Word" ]] || mkdir -p "${f:a:h:r}/Word"

    # construct the full path to the word docx document to be exported
    wordfile="${f:a:h:r}/Word/${f:r:t}.docx"

    pages_export "${f}" "${wordfile}"

    # do we have an exported docx that is non-zero in size?
    [[ -s $wordfile ]] && (( ++docxCnt ))
done

# give the user statistics
completion $pagesCnt $docxCnt



29 replies

Dec 11, 2022 8:08 AM in response to VikingOSX

They do exist with size as I can open them and see their content, plus the first picture where I named the files with numbers are from the files I had in folders and renamed them so it would show that the file with the .pages is not being opened and exported and it has size more than zero.

but as said so far so good, will do the rest manually as this app did most of the job, thanks again.

Dec 11, 2022 12:08 PM in response to Moaweyah

Launch the Script Editor and copy/paste the following AppleScript code into it. Click the hammer icon to compile it, and then click Run. It will prompt you for the folder containing your Pages documents and then run a repeat loop on that list of Pages documents, exporting them as Word and PDF documents of the same name into that folder.


use scripting additions

property DOCX : ".docx"
property PDF : ".pdf"

-- pick a folder containing Pages documents
set pagesFolder to (choose folder)

tell application "Finder"
	set pagesList to (every item in folder pagesFolder whose kind contains "Pages") as alias list
end tell

if (count of pagesList) = 0 then return

repeat with afile in pagesList
	set basename to text 1 thru ((offset of ".pages" in (afile as text)) - 1) of (afile as text)
	set wordOutFile to basename & DOCX
	set pdfOutFile to basename & PDF
	
	tell application "Pages"
		activate
		set thisDoc to open afile as alias
		with timeout of 1200 seconds
			export thisDoc to file wordOutFile as Microsoft Word
			export thisDoc to file pdfOutFile as PDF with properties {image quality:Best}
		end timeout
		close thisDoc saving no
	end tell
	
	tell application "Finder"
		if exists (item wordOutFile as alias) then
			set extension hidden of (item wordOutFile as alias) to false
		end if
		if exists (item pdfOutFile as alias) then
			set extension hidden of (item pdfOutFile as alias) to false
		end if
	end tell
end repeat
return


Batch Convert pages to Word Document in multiple folders

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