MacOS Automator + Applescript solution for exporting docx to pdf
Asked Answered
S

2

6

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.

Salliesallow answered 14/8, 2018 at 15:3 Comment(0)
S
7

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
Spermophile answered 14/8, 2018 at 15:39 Comment(5)
Thanks a lot for the quick answer @vadian!! One more small question: can the "additional permissions are required to access the following files: ..." Word dialogue be suppressed by giving the script write access to the output file(s) in some way?Salliesallow
No, you cannot change access privileges in the codeSpermophile
Thanks @vadian, though is it not even possible in the OS (accessibility, or folder permissions?)Salliesallow
I guess you can change the privileges in FinderSpermophile
If using this solution as a service, I removed the Get Selected Finder Items to prevent processing the file twice.Stomodaeum
M
3

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
Margheritamargi answered 9/9, 2018 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.