首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AppleScript和Acrobat关闭文档

使用AppleScript和Acrobat关闭文档
EN

Stack Overflow用户
提问于 2018-04-15 16:30:40
回答 1查看 664关注 0票数 0

我正在尝试使用AppleScript和Acrobat将数千个PDF转换为TXTs。我的代码循环遍历每个PDF,打开它,并将其保存为TXT。这个部分工作得很好:PDF转换成TXTs,没有问题。我不知道如何在每个PDF保存为TXT之后,如何关闭它。PDF仍然是开放的,最终我的内存用完了(这是很多PDF,其中一些很大)。

这是我的密码:

代码语言:javascript
复制
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

我在这里做错什么了?

Acrobat 11.0.23,macOS High塞拉利昂10.13.4,AppleScript 2.7,脚本编辑器2.10 (194)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-15 16:53:42

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

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

一些注意事项:

  • 在第二部分中不需要查找器。
  • 我没有使用newFile12,而是将命名更改为使用原始名称
  • 脚本使用相对路径。
  • 有些PDF文件不能转换为文本,我添加了一个try块来忽略错误。
代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49844260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档