Skip to content

Instantly share code, notes, and snippets.

@sourtin
Created September 20, 2020 15:36
Show Gist options
  • Save sourtin/0c04a3e3cf2be86b4f254455a932cf6c to your computer and use it in GitHub Desktop.
Save sourtin/0c04a3e3cf2be86b4f254455a932cf6c to your computer and use it in GitHub Desktop.
Applescript utility for batch word to pdf conversion
#!/usr/bin/osascript
on isValid(item_info)
try
return (folder of the item_info is false) and (package folder of the item_info is false) and (alias of the item_info is false) and (name extension of item_info is "docx")
end try
return false
end isValid
on open these_items
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if isValid(item_info) then
process_file(this_item)
end if
end repeat
end open
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if isValid(item_info) then
process_file(this_item)
end if
end repeat
end process_folder
on process_file(this_item)
set path_docx to POSIX path of this_item
set AppleScript's text item delimiters to "."
set components to every text item of path_docx
set last text item of components to "pdf"
set path_pdf to components as string
set AppleScript's text item delimiters to ""
tell application "Microsoft Word"
set doc to open file name path_docx with read only without add to recent files
save as doc file name path_pdf file format format PDF
close doc saving no
end tell
end process_file
@sourtin
Copy link
Author

sourtin commented Sep 20, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment