Last active
December 17, 2019 15:34
-
-
Save masnick/88238c9806751cffbd3129cfaa4ae7df to your computer and use it in GitHub Desktop.
Batch convert Word files (.docx) to PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Based on https://discussions.apple.com/thread/250068127 (thanks, VikingOSX!) | |
property word_docs : {"org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc"} | |
property default_path : (path to desktop) as alias | |
property Delim : {".docx", ".doc"} | |
property PDF : ".pdf" | |
set outPDF to {} | |
set selected_files to (choose file of type word_docs default location default_path with multiple selections allowed without invisibles and showing package contents) | |
-- replace Word document extensions with PDF, and append to outPDF list | |
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, Delim} | |
repeat with afile in selected_files | |
copy item 1 of text items of (afile as text) & PDF to the end of outPDF | |
end repeat | |
set AppleScript's text item delimiters to TID | |
tell application id "com.microsoft.Word" | |
activate | |
repeat with i from 1 to count of selected_files | |
set theOriginalPath to POSIX path of (item i of selected_files) | |
open theOriginalPath | |
delay 0.5 | |
set theOutputPath to POSIX path of (item i of outPDF) | |
-- Of course macOS randomly requires Word to request permission for accessing the .docx file, | |
-- even if Word has full disk access turned on. This kludge works around that. | |
try | |
tell active document | |
save as it file name theOutputPath file format format PDF | |
close saving no | |
end tell | |
on error err | |
tell application "System Events" | |
key code 53 -- press esc key to close disk access request window | |
end tell | |
delay 2 | |
tell active document | |
save as it file name theOutputPath file format format PDF | |
close saving no | |
end tell | |
end try | |
end repeat | |
end tell | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to VikingOSX for writing the script this is based off of!