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

applescriptautomatorfinderms officepages

How do I adjust this script to allow me to choose files not in a 'pages only' folder, and instead multiple "free" files?

Keyword here is 'single'. I'm by no means a coding expert but I've learned about automator recently and it's been incredibly useful when converting PNG images to .ICNS

I was sending a classmate a folder with my notes for a course and I realized he's got a PC and I have a Mac, so I should convert the pages documents to .docx so he can open them. I have many other instances where this is necessary. I did some looking online and I created a quick action, and it works! I've tested it a few times and its almost exactly what I need.

The only problem is it only works if all the documents are in a separate folder of only pages documents and I want a way to get around that. If I could select multiple pages documents at a time without putting them in a separate folder first that would be great!

Quick Action Automation

This is the script I'm using:

    
    --Select from where you will pick up the pages files
    set theSourceFolder to choose folder with prompt "Select folder with original pages files :"
    --Do it
    tell application "Finder"
        set theNames to name of files of theSourceFolder ¬
            whose name extension is "pages"
    end tell
    
    --Select where the files will go
    set theDestinationFolder to choose folder with prompt "Select folder where files will go :"
    
    -- How many files to export
    set item_count to (get count of items in theNames)
    
    --Get files and export them
    repeat with i from 1 to item_count
        
        set current_file to item i of theNames -- get a file
        set lean_file to text 1 thru -7 of current_file & ".docx" -- change the originalfile (.pages) to a .MS Word name
        set out_file to (theDestinationFolder as Unicode text) & (lean_file) -- get the fully qualified output name
        set in_file to (theSourceFolder as Unicode text) & (current_file) -- get the fully qualified input file name
        
        tell application "Pages"
            set mydoc to open file in_file -- open input file in Pages
            export mydoc to file out_file as Microsoft Word --do the exporting
            close mydoc saving no -- close the original file without saving
        end tell
        
    end repeat
    
    return input
end run 

I think I have to change "theSourceFolder" to maybe "theSourceFile"? but I also am pretty sure there's more to the script than I understand and I have a feeling that if I try tinkering with it I'll ruin it.

edit:

also maybe if I could just select the file in finder without the pop-up window prompting me to select a file that would be even better!

Thanks for your help in advance!

Best Answer

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