Need help adjusting AppleScript automation for converting .pages to .docx?

Solution 1:

This AppleScript code will take the currently selected files in the front most Finder window, looping through each item one at a time, and only if the file is a “.pages” document, a duplicate will be exported as a Microsoft Word document.

You can play around with the code if you would prefer to have the option to choose the output folder where the exported files will be saved to. Otherwise, the exported files will be saved to the same folder as the original files.

tell application "Finder" to set inputFiles to selection as alias list

repeat with i from 1 to count of inputFiles
    set thisFile to item i of inputFiles
    tell application "Finder"
        set fileName to name of thisFile
        set nameExtension to name extension of thisFile
        tell current application to set theOffset ¬
            to offset of nameExtension in fileName
        set baseName to text (theOffset - 1) thru 1 of fileName
        set outputFolder to container of thisFile as text
    end tell
    if nameExtension is "pages" then
        tell application "Pages"
            set myDoc to open thisFile
            export myDoc to file ((outputFolder & baseName) ¬
                & "docx" as text) as Microsoft Word
            close myDoc saving no
        end tell
    end if
end repeat

Caution: this code will overwrite any existing .docx files with the same name that currently may exist