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:
- Launch Automator from your /Applications folder.
- From the initial Automator dialog select Application and then click Choose
- 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.
- Configure Workflow actions
- Files & Folders library
- Drag and drop Ask for Finder Items action onto the Workflow window
- Start at: Desktop
- Type: Folder
- Do not select Multiple Selection
- Utilities library
- Drag and drop Run Shell Script action onto the Workflow window
- Shell: /bin/zsh
- Pass Input: as arguments
- remove all content in this action as it will be replaced by Code shown below.
- Save the Automator application (e.g. name it Pages2Docx) and you do not need to give it an extension
- Quit Automator
- 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