Search code examples
macosms-wordapplescriptautomatorword-2016

MacOS Automator + Applescript solution for exporting docx to pdf


Scratching my head after reading lots of different threads on this and tried a bunch of scripts but none seem to work.

I'd like to use Automator to automate Word 2016 conversion of a selection of docx files to pdf.


Used the following Automator Service:

enter image description here


Used the following script:

on run {input, parameters}
    tell application id "com.microsoft.Word"
        activate
        open input
        set doc to name of active window
        set theOutputPath to (input & ".pdf")
        save as active document file name theOutputPath file format format PDF
    end tell
end run


Which results in error: Microsoft Word got an error: active document doesn’t understand the “save as” message.


Solution

  • The main issue is that input is a list. You have to use a repeat loop to process each file separately

    I added a line to close the current document after having been converted

    on run {input, parameters}
        tell application id "com.microsoft.Word"
            activate
            repeat with aFile in input
                open aFile
                set theOutputPath to ((aFile as text) & ".pdf")
                tell active document
                    save as it file name theOutputPath file format format PDF
                    close saving no
                end tell
            end repeat
        end tell
    end run