5

在阅读了很多关于此的不同线程并尝试了一堆脚本但似乎没有一个工作后,我挠了挠头。

我想使用 Automator 将选择的 docx 文件自动转换为 pdf 的 Word 2016。


使用了以下 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 出现错误:活动文档不理解“另存为”消息。

4

2 回答 2

7

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

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

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
于 2018-08-14T15:39:50.883 回答
3

为了防止@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
于 2018-09-09T14:40:20.463 回答