简体   繁体   English

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

[英]MacOS Automator + Applescript solution for exporting docx to pdf

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.我想使用 Automator 自动将 Word 2016 的精选 docx 文件转换为 pdf。


Used the following Automator Service:使用了以下 Automator 服务:

在此处输入图片说明


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.这会导致错误: Microsoft Word 出现错误:活动文档不理解“另存为”消息。

The main issue is that input is a list .主要问题是input是一个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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM