MacOS Automator + Applescript solution for exporting docx to pdf

<a href=enter image description here" />

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:
Used the following script:

on run 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.

25.5k 21 21 gold badges 35 35 silver badges 44 44 bronze badges asked Aug 14, 2018 at 15:03 318 5 5 silver badges 13 13 bronze badges

2 Answers 2

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 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 
7,485 3 3 gold badges 18 18 silver badges 30 30 bronze badges answered Aug 14, 2018 at 15:39 284k 31 31 gold badges 373 373 silver badges 371 371 bronze badges

Thanks a lot for the quick answer @vadian!! One more small question: can the "additional permissions are required to access the following files: . " Word dialogue be suppressed by giving the script write access to the output file(s) in some way?

Commented Aug 14, 2018 at 17:09 No, you cannot change access privileges in the code Commented Aug 14, 2018 at 17:12 Thanks @vadian, though is it not even possible in the OS (accessibility, or folder permissions?) Commented Aug 14, 2018 at 17:16 I guess you can change the privileges in Finder Commented Aug 14, 2018 at 17:16

If using this solution as a service, I removed the Get Selected Finder Items to prevent processing the file twice.

Commented Dec 17, 2019 at 22:20

To prevent the problem discussed in @vadian's answer, save the file first to Word's default folder (that's usually ~/Library/Containers/com.microsoft.Word/Data/Documents) and then move the file somewhere else.

on run repeat with aFile in input tell application "System Events" set inputFile to disk item (aFile as text) set outputFileName to (((name of inputFile) as text) & ".pdf") end tell tell application id "com.microsoft.Word" activate open aFile tell active document save as it file name outputFileName file format format PDF close saving no end tell set defaultPath to get default file path file path type documents path end tell tell application "System Events" set outputPath to (container of inputFile) set outputFile to disk item outputFileName of folder defaultPath move outputFile to outputPath end tell end repeat return input end run