【问题标题】:closing documents with AppleScript and Adobe Acrobat使用 AppleScript 和 Adob​​e Acrobat 关闭文档
【发布时间】:2018-04-15 16:30:40
【问题描述】:

我正在尝试使用 AppleScript 和 Adob​​e Acrobat Pro 将数千个 PDF 转换为 TXT。我的代码循环遍历每个 PDF,打开它,然后将其保存为 TXT。那部分工作正常:PDF 被转换为 TXT,没有问题。我不知道该怎么做是如何在每个 PDF 保存为 TXT 后关闭它。 PDF 保持打开状态,最终我的内存不足(这是很多 PDF,其中一些很大)。

这是我的代码:

set sourceFolder to "Macintosh HD:Users:thiagomarzagao:pdffolder"

tell application "Finder"
    set fileSet to get every file of folder sourceFolder
end tell

tell application "Finder"
    repeat with i from 1 to (count of fileSet)
        set currentFile to (item i of fileSet) as string
        set outFile to "Macintosh HD:Users:thiagomarzagao:txtfolder:" & "newfile" & i & ".txt"
        with timeout of 6000 seconds
            tell application "Adobe Acrobat Pro"
                activate
                open currentFile
                save document i to file outFile using conversion "com.adobe.acrobat.plain-text"
                close currentFile saving no
            end tell
        end timeout
    end repeat
end tell

我明白了:

error "Adobe Acrobat Pro got an error: \"Macintosh HD:Users:thiagomarzagao:mypdffile.pdf\" doesn’t understand the “close” message." number -1708 from "Macintosh HD:Users:thiagomarzagao:Desktop:mypdffile.pdf"

如果我用close document i saving noclose every document saving no 替换有问题的行,我会得到:

error "Adobe Acrobat Pro got an error: document 2 doesn’t understand the “save” message." number -1708 from document 2

如果我尝试close window 1,我会得到:

error "Adobe Acrobat Pro got an error: window 1 doesn’t understand the “close” message." number -1708 from window 1

我在这里做错了什么?

Adobe Acrobat XI Pro 11.0.23、macOS High Sierra 10.13.4、AppleScript 2.7、脚本编辑器 2.10 (194)

【问题讨论】:

    标签: pdf applescript acrobat


    【解决方案1】:

    您混淆了不可互换的 HFS 字符串路径和 Adob​​e 文档说明符。

    Acrobat 有一个属性active doc,可以用作对前端文档的引用。

    一些注意事项:

    • 第二部分不需要Finder
    • 而不是newFile12我改了命名使用原名
    • 脚本使用相对路径
    • 某些 PDF 文件无法转换为文本,我添加了一个 try 块以忽略错误

    set homeFolder to path to home folder as text
    set sourceFolder to homeFolder & "pdffolder:"
    set txtFolder to homeFolder & "txtfolder:"
    
    tell application "Finder"
        set fileSet to get every file of folder sourceFolder
    end tell
    
    activate application "Adobe Acrobat Pro"
    repeat with aFile in fileSet
        set currentFile to aFile as text
        set currentFileName to name of aFile
        set outFile to txtFolder & text 1 thru -5 of currentFileName & ".txt"
        with timeout of 6000 seconds
            tell application "Adobe Acrobat Pro"
                open currentFile
                try
                    save active doc to file outFile using conversion "com.adobe.acrobat.plain-text"
                end try
                close active doc saving no
            end tell
        end timeout
    end repeat
    

    【讨论】:

      猜你喜欢
      • 2017-04-10
      • 2020-05-26
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2018-12-06
      • 1970-01-01
      • 2019-02-16
      相关资源
      最近更新 更多