用于将docx导出为pdf的MacOS Automator + Applescript解决方案

问题描述 投票:2回答:2

在阅读了很多不同的线程之后抓住我的头,尝试了一堆脚本,但似乎都没有。

我想使用Automator自动将一系列docx文件的Word 2016转换为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出错:活动文档无法理解“另存为”消息。

macos ms-word applescript automator word-2016
2个回答
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

2
投票

为了防止在@ 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
© www.soinside.com 2019 - 2024. All rights reserved.