PDFs from Word

I love David Sparks’ ⌘+P ⌘+P shortcut to print things to PDF. But because Word is insane, I get GIANT files when I do it. Using the Save As… dialog in Word lets me save a PDF to a more reasonable size.

Type Size
.docx 54KB
.pdf (Print) 7,031KB
.pdf (Save As…) 68KB

I set up an applescript to handle the saving, but for some reason, it only works about half the time.

property theList : {"doc", "docx"}
tell application "/Applications/Microsoft Word.app"

    set theActiveDoc to the active document
    set theFileName to name of theActiveDoc
    set theFilePath to path of theActiveDoc
    set ext to do shell script "echo '" & theFileName & "' | awk -F. '{print $NF}'"
    if ext is in theList then
        set theName to name of theActiveDoc
        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"
        set FilePath to theFilePath & ":" & theFileName
        save as theActiveDoc file name FilePath file format format PDF
    end if
end tell

If someone knows why it throws “active document doesn’t understand ‘Save As…’ ” errors, let me know.

Applescript error message

When it fails, it fails predictably, so I can use Keyboard Maestro to check for success. The first script saves its output to a variable, so the macro checks to see if PDFSuccess has any content. If it does, the macro falls back on interface scripting, which is slower and potentially less reliable. But it seems to work for me.

This script uses the accessability interface to change the filetype to PDF and clicks save.

tell application "System Events"
    tell process "Microsoft Word"
        tell window 1
            tell sheet 1
                tell group 1
                    tell pop up button 1
                        click
                        click menu item "PDF" of menu 1
                    end tell
                end tell
                tell button "Save"
                    click
                end tell
            end tell
        end tell
    end tell
end tell

Download the macro