Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MacOS Automator + Applescript solution for exporting docx to pdf

Scratching my head after reading lots of different threads on this and tried a bunch of scripts but none seem to work.

I'd like to use Automator to automate Word 2016 conversion of a selection of docx files to pdf.


Used the following Automator Service:

enter image description here


Used the following script:

on run {input, parameters}
    tell application id "com.microsoft.Word"
        activate
        open input
        set doc to name of active window
        set theOutputPath to (input & ".pdf")
        save as active document file name theOutputPath file format format PDF
    end tell
end run


Which results in error: Microsoft Word got an error: active document doesn’t understand the “save as” message.

like image 607
Base_v Avatar asked Aug 14 '18 15:08

Base_v


People also ask

How do I batch convert docx to PDF on Mac?

From the library panel on the left, select Files & Folders then double-click Get Specified Finder Items. Add the all the files to convert. From the library panel, now select Documents , then double click Convert Format of Word Documents. From the dropdown menu, select Portable Document Format (PDF)

How do I convert a large Word document to PDF on Mac?

Have the Word doc you want to convert to PDF open in Word for Mac. Go to the 'File' menu and choose “Export” Select “PDF” in the file format selection. Choose to Export the Word doc as a PDF.

How do I convert Word to PDF and keep formatting on a Mac?

Step 1: Open your Word file with Microsoft Office for Mac and then go to File> Save As. Step 2: Name the file in the “Save As” box and then from the “Format” drop down options, select PDF. Step 3: Click the “Save” button and your Word file will be turned into PDF in no time.


2 Answers

The main issue is that input is a list. You have to use a repeat loop to process each file separately

I added a line to close the current document after having been converted

on run {input, parameters}
    tell application id "com.microsoft.Word"
        activate
        repeat with aFile in input
            open aFile
            set theOutputPath to ((aFile as text) & ".pdf")
            tell active document
                save as it file name theOutputPath file format format PDF
                close saving no
            end tell
        end repeat
    end tell
end run
like image 80
vadian Avatar answered Nov 02 '22 11:11

vadian


To prevent the problem discussed in @vadian's answer, save the file first to Word's default folder (that's usually ~/Library/Containers/com.microsoft.Word/Data/Documents) and then move the file somewhere else.

on run {input, parameters}
    repeat with aFile in input
        tell application "System Events"
            set inputFile to disk item (aFile as text)
            set outputFileName to (((name of inputFile) as text) & ".pdf")
        end tell

        tell application id "com.microsoft.Word"
            activate
            open aFile
            tell active document
                save as it file name outputFileName file format format PDF
                close saving no
            end tell
            set defaultPath to get default file path file path type documents path
        end tell

        tell application "System Events"
            set outputPath to (container of inputFile)
            set outputFile to disk item outputFileName of folder defaultPath
            move outputFile to outputPath
        end tell
    end repeat
    return input
end run
like image 32
olalahti Avatar answered Nov 02 '22 12:11

olalahti