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 Top-ranking reply

Posted on Dec 6, 2022 4:09 PM

I have revised the Run Shell Script content to produce this output with the added PDF export included:



The default folders in macOS are blue, so not sure what you meant by that, and at any rate, I cannot change folder colors with a script.


Replace the entire contents of your Run Shell Script action with the following code:


#!/bin/zsh

: <<"COMMENT"
Do a recursive descent on a folder hierarchy gathering Pages document. Export these
documents from Pages as Word .docx documents (and PDF) 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 pdfCnt=0

STARTDIR="${@}"

function pages_export () {

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

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

            set thisDoc to open infile 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
        on error errmsg number errNo
            display dialog "[ " & errNo & " ]: " & errmsg
        end try
    end tell
    -- unhide the exported filename extensions
    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
    return
AS
}

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

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

    tell application "Pages" to if it is running then quit

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

# 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 and PDF to be exported
    wordfile="${f:a:h:r}/Word/${f:r:t}.docx"
    pdffile="${f:a:h:r}/Word/${f:r:t}.pdf"

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

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

# give the user statistics
completion $pagesCnt $docxCnt $pdfCnt


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.

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.

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 Account.