macOS Automator + Applescript 将docx导出为pdf的解决方案

MacOS Automator + Applescript solution for exporting docx to pdf

在阅读了很多关于此的不同线程并尝试了一堆脚本后抓耳挠腮,但 none 似乎有效。

我想使用 Automator 自动将 Word 2016 中的一系列 docx 文件转换为 pdf。


使用了以下 Automator 服务:


使用了以下脚本:

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


导致错误:Microsoft Word 出错:活动文档不理解“另存为”消息。

主要问题是 inputlist。您必须使用重复循环来分别处理每个文件

我在转换后添加了一行关闭当前文档

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

为了防止@vadian 的回答中讨论的问题,首先将文件保存到 Word 的默认文件夹(通常是 ~/Library/Containers/com.microsoft.Word/Data/Documents),然后将文件移动到其他地方。

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