Recently, I needed to automate printing a Word document to a PDF file and have it saved to a predefined location. There were some good articles on the web about how to do the printing part, or the PDF part but not how to save the output.
So, here’s my stab at printing a Word document to a PDF file in a known location:
-- This AppleScript will open up our Word doc and save it as a PDF
set myDoc to "/Users/jaimerios/Documents/Developer notes.doc"
set pdfSavePath to "/Users/jaimerios/Documents/PDFs/"
tell application "Microsoft Word"
activate
open myDoc
end tell
tell application "System Events"
tell process "Microsoft Word"
-- Press command+p to open our print dialog
keystroke "p" using command down
-- Let's make sure our print dialog is up
repeat until exists window "Print"
end repeat
-- Click the PDF menu button
click menu button "PDF" of window "Print"
-- Make sure the menu is up
repeat until exists menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
end repeat
-- Select the "Save as PDF" menu item
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
-- Make sure the save dialog is visible
repeat until exists window "Save"
end repeat
-- Press command+shift+g to show the "Go" drop down sheet
keystroke "g" using {command down, shift down}
-- Set our location field to our pdfSavePath
set value of text field 1 of sheet of window "Save" to pdfSavePath
-- Now click the Go button
click button "Go" of sheet of window "Save"
-- Now that we are in our desired folder, set the file name and save
set value of text field 1 of window "Save" to "Special Developer Notes.pdf"
click button "Save" of window "Save"
end tell
end tell
Now the only limitation to this script is that it doesn’t handle the condition where the PDF file might already exist in our output location.
I have to give credit to the original author who came up with tis script. Their AppleScript code is located at this website link.
Well, I hope this helps you and if you have any comments to make this script better, let me know.
Happy coding!