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

求助:批量成对Word文档修订对比的AppleScript脚本修复

批量Word文档对比AppleScript脚本修复方案

原脚本存在的核心问题

  • 对比参数顺序错误:Word的compare命令要求第一个参数是旧文档(原文件),第二个是新文档(修订文件),原脚本写反了,导致对比结果逻辑错误。
  • 未定义变量引用:脚本最后尝试关闭oldDoc和newDoc,但这两个变量从未创建,运行时会抛出“变量未定义”错误。
  • 文件名处理缺陷:直接用basename获取的文件名包含原扩展名,保存后会出现xxx.docx_TC.docx这种重复后缀的情况。
  • 路径格式兼容性问题:直接传入字符串路径给Word的compare命令,可能因为路径格式(HFS/POSIX)不兼容导致无法识别文件。
  • 空行未过滤:若CSV文件存在空行,会导致空路径对比,触发错误。

修正后的完整脚本

-- 选择CSV文件
set csvFile to choose file with prompt "选择包含文件路径的CSV文件:"

-- 选择输出文件夹
set outputFolder to choose folder with prompt "选择对比结果保存文件夹:"

-- 读取CSV内容并分割行
set fileContent to read csvFile as text
set fileLines to paragraphs of fileContent

-- 分割文本函数
on splitText(theText, theDelimiter)
    set AppleScript's text item delimiters to theDelimiter
    set theTextItems to text items of theText
    set AppleScript's text item delimiters to ""
    return theTextItems
end splitText

-- 去除空白字符函数
on trimText(theText)
    set theText to (do shell script "echo " & quoted form of theText & " | xargs")
    return theText
end trimText

-- 提取文件名(不含扩展名)
on getFileNameWithoutExtension(filePath)
    set AppleScript's text item delimiters to "/"
    set fileNameWithExt to last text item of filePath
    set AppleScript's text item delimiters to "."
    set fileNameParts to text items of fileNameWithExt
    if count of fileNameParts > 1 then
        set AppleScript's text item delimiters to "."
        return text items 1 thru -2 of fileNameParts as text
    else
        return fileNameWithExt
    end if
end getFileNameWithoutExtension

-- 遍历CSV行执行对比
repeat with i from 1 to count of fileLines
    set fileLine to item i of fileLines
    -- 跳过空行
    if fileLine is "" then continue repeat
    
    set pathItems to my splitText(fileLine, ",")
    -- 确保每行有两个路径
    if count of pathItems < 2 then continue repeat
    
    set oldFileStr to my trimText(item 1 of pathItems)
    set newFileStr to my trimText(item 2 of pathItems)
    -- 跳过空路径
    if oldFileStr is "" or newFileStr is "" then continue repeat
    
    -- 转换为Word可识别的文件引用
    set oldFile to alias oldFileStr
    set newFile to alias newFileStr
    
    -- 生成输出文件名(去除原后缀加_TC.docx)
    set baseFileName to my getFileNameWithoutExtension(newFileStr)
    set comparisonFilePath to (outputFolder as text) & baseFileName & "_TC.docx"
    
    tell application "Microsoft Word"
        activate
        -- 执行对比:旧文档在前,新文档在后
        set compareDoc to compare oldFile with newFile
        -- 保存对比结果
        save as compareDoc file name comparisonFilePath file format format document
        -- 关闭所有相关文档
        close compareDoc saving no
        -- 关闭自动打开的新旧文档(Word对比后会打开这两个文件)
        close oldFile saving no
        close newFile saving no
    end tell
end repeat

关键修复说明

  1. 修正对比顺序:将compare newFile with oldFile改为compare oldFile with newFile,符合Word对比逻辑(原文件 vs 修订文件)。
  2. 定义文件引用变量:将字符串路径转换为alias类型的文件引用,既解决Word识别问题,也能直接用于关闭文档。
  3. 优化文件名处理:新增getFileNameWithoutExtension函数,去除原文件扩展名后再添加_TC.docx,避免重复后缀。
  4. 增加错误防护:过滤CSV空行、不完整行和空路径,避免无效对比触发错误。
  5. 正确关闭文档:使用转换后的oldFile和newFile变量关闭对比时自动打开的原文件,解决变量未定义问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 09:51:18