You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用AppleScript在Adobe Acrobat Pro中转换PDF后关闭文档

解决Acrobat Pro批量转换PDF到TXT时的内存耗尽问题

我来帮你搞定这个内存爆掉的麻烦!核心问题就是你没在每一份PDF转换完成后精准关闭对应的Acrobat文档实例,导致所有打开的PDF都留在内存里,大文件多了自然就撑爆了。下面是修复后的完整代码,我会把关键细节给你拆解清楚:

set sourceFolder to "Macintosh HD:Users:thiagomarzagao:pdffolder"
set destinationFolder to "Macintosh HD:Users:thiagomarzagao:txtfolder" -- 建议单独指定输出文件夹,更整洁

tell application "Adobe Acrobat Pro"
    activate
    -- 筛选源文件夹里的所有PDF文件
    set pdfFiles to every file of folder sourceFolder whose name extension is "pdf"
    
    repeat with aFile in pdfFiles
        -- 打开PDF并保存文档引用(这是后续关闭的关键)
        set docRef to open (aFile as alias)
        -- 提取不带扩展名的文件名,用来命名输出的TXT
        set fileName to name of aFile
        set baseName to text 1 thru ((offset of "." in fileName) - 1) of fileName
        -- 构建TXT文件的完整路径
        set txtPath to destinationFolder & ":" & baseName & ".txt"
        
        -- 导出为TXT格式
        save docRef to file txtPath as text format
        -- 重中之重!关闭当前文档,saving no避免弹出无意义的保存提示
        close docRef saving no
    end repeat
    -- 所有任务完成后可以直接退出Acrobat,彻底释放内存(不需要的话可以删掉这行)
    quit saving no
end tell

关键修复点说明

  • 保存文档引用docRef:打开PDF时一定要捕获返回的文档对象,这样我们才能精准关闭单个文档,而不是做模糊的全局操作。
  • close docRef saving no:这是解决内存泄漏的核心!每转换完一份就立刻关闭对应的文档,saving no确保不会因为原PDF没修改而弹出保存对话框,打断批量任务。
  • 单独设置输出文件夹:把TXT和原PDF分开存放,既避免文件混乱,也方便后续批量处理TXT文件。

额外优化建议(可选)

如果你的PDF里有加密文件或者损坏文件,可能会导致脚本中断。可以加个错误捕获块,让脚本跳过出错的文件继续运行,同时也确保出错的文档被关闭:

repeat with aFile in pdfFiles
    try
        set docRef to open (aFile as alias)
        set fileName to name of aFile
        set baseName to text 1 thru ((offset of "." in fileName) - 1) of fileName
        set txtPath to destinationFolder & ":" & baseName & ".txt"
        
        save docRef to file txtPath as text format
        close docRef saving no
    on error errMsg number errNum
        display dialog "处理文件 " & name of aFile & " 时出错:" & errMsg buttons {"继续"} default button 1
        -- 即使出错也要尝试关闭文档,避免内存泄漏
        if exists docRef then close docRef saving no
    end try
end repeat

内容的提问来源于stack exchange,提问作者Parzival

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:59:15