This uses:
- AppleScript to open microsoft word and save as a PDF
- An Automator Application to wrap the AppleScript
- Find from the Terminal to find word documents to call the Automator Applicator
And it works on Catalina and with Office 365.
The purpose of the script is, for the filename passed in, to open the file in word, and then save the file with a .pdf extension instead of .doc or .docx.
on run {input, parameters}
repeat with aFile in input
repeat 1 times -- # fake loop
-- Create the output filename
tell application "System Events"
set inputFile to disk item (aFile as text)
tell (info for input) to set Nm to name
if (text -5 thru -1 of Nm) is ".docx" then
set outputFileName to ((text 1 thru -6 of Nm) & ".pdf")
else if (text -4 thru -1 of Nm) is ".doc" then
-- .doc
set outputFileName to ((text 1 thru -5 of Nm) & ".pdf")
else
-- If this isn't a word document, continue to the next file.
exit repeat
end if
end tell
-- Open word, save as PDF in default path
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
-- Move to output location
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
end repeat
return input
end run
- Open automator
- File / New then select Application
- From the actions Library in the left hand nav, select "Run AppleScript" and drag it into the window on the righ
- Copy and paste the code above into the Run AppleScript window.
- Save the application as "WordToPdf.app" on the Desktop
- Open Terminal
- Change Directory to the one you wish to run the script in. Note that it is recursive and will process all files in all subdirectories
- Run the following code:
find . -type f \( -iname \*.doc -o -iname \*.docx \) -print0 | while read -d $'\0' file do automator -i "$file" ~/Desktop/WordToPdf.app done
- Double check that you are happy, and then optionally remove all of the word documents:
find . -type f \( -iname \*.doc -o -iname \*.docx \) -exec rm {} \;