Convert Word files (DOC/DOCX) to PDF in OSX


If you need to convert a file from Word to PDF you probably select print to PDF from Word but when you need to convert multiple files you need to script it.

AppleScript for converting Word into PDF

  • Open Automator.
  • Select Service and click Choose.
  • In the top of the screen choose Service receives selected: files or folders in Finder.app.

  • Choose Run AppleScript (under Utilities category or use the Search input field).
  • Paste the following AppleScript.
property theList : {"doc", "docx"}
on run {input, parameters}
    set output to {}
    tell application "Microsoft Word" to set theOldDefaultPath to get default file path file path type documents path
    repeat with x in input
        try
            set theDoc to contents of x
            tell application "Finder"
                set theFilePath to container of theDoc as text

                set ext to name extension of theDoc
                if ext is in theList then
                    set theName to name of theDoc
                    copy length of theName to l
                    copy length of ext to exl

                    set n to l - exl - 1
                    copy characters 1 through n of theName as string to theFilename

                    set theFilename to theFilename & ".pdf"

                    tell application "Microsoft Word"
                        set default file path file path type documents path path theFilePath
                        open theDoc
                        set theActiveDoc to the active document
                        save as theActiveDoc file format format PDF file name theFilename
                        copy (POSIX path of (theFilePath & theFilename as string)) to end of output
                        close theActiveDoc
                    end tell
                end if
            end tell
        end try
    end repeat
    tell application "Microsoft Word" to set default file path file path type documents path path theOldDefaultPath
    return output
end run
  • Save it as Word2PDF (or any other name)

  • For running it just right click your mouse on top of a file or folder and you will see the new service in the context menu.

Advertisements

25 thoughts on “Convert Word files (DOC/DOCX) to PDF in OSX

    • I got the error in two places. the first is when I tried right clicking and then choosing the “word2pdf” option. then went back into AS and hit the play button. the specific error was what I wrote and then it highlighted the semi colon in the line: set theFilename to theFilename & “.pdf”

      • I’ve just found that when copying the source to the blog and ampersand got translated into “&”. This character appears twice and then the errors that you found. I’ve updated the post with the source fixed.
        Sorry and thanks for your comment

  1. Hey Onabai, I’m looking to add to the script by creating a subfolder within the folder where the documents are located to store the resulting pdf. what would that script look like and where would I put it? thanks in advance

    • I’ve also set it to a keyboard shortcut using System Preferences – so now I just select a file, hit cmd-shift-P, and out comes a PDF! so cool!

  2. Great script – thank you. Just wondering if there is a way to amend it so that I can drop a word file into a folder to convert to pdf + a way to automatically reduce file size.

  3. Thanks for sharing, great idea.
    I could not manage it to work, though, keep getting error
    “Microsoft Word got a syntax error: Can’t continue get default file path.”
    Any idea what could that be?

  4. Mod to question,

    I was able to paste text as shown into “Automator”, but I do not know what to do next since I can not find “Find.app”.

    Still stumped.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s