MacOS Automator + Applescript解决方案,可将docx导出为pdf

时间:2018-08-14 15:03:48

标签: macos ms-word applescript automator word-2016

在阅读了许多不同的线程并尝试了一堆脚本之后,我挠了挠头,但似乎没有用。

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


使用了以下Automator服务:

enter image description here


使用了以下脚本:

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遇到错误:活动文档不理解“另存为”消息。

2 个答案:

答案 0 :(得分:3)

主要问题是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

答案 1 :(得分:1)

为防止@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