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

AppleScript脚本在相似Pages文档中运行异常的排查求助

解决AppleScript处理Pages文档经文格式化的CR字符报错问题

问题概述

两段AppleScript脚本用于自动格式化Pages文档中的经文引用:

  • Code Set 1:将括号内的经文引用转换为[[经文]]格式
  • Code Set 2:将[[经文]]内容移至新行

脚本在一份文档中正常运行,但在另一份结构几乎相同的文档中报错,错误信息:

Error: Can’t make text 11 thru -1 of item 16 of {"Used at Canal heights 20241229b evening service

经排查,异常文档的行尾使用CR(回车符,\r),而正常文档使用LF(换行符,\n),这是导致脚本处理失败的核心原因。

报错原因

  1. AppleScript的文本处理逻辑(如offset、text items)对CR和LF的解析行为不一致,会导致文本范围计算出错
  2. 脚本中调用的grep、sed等shell工具默认以LF作为换行符识别标志,CR会被当作普通文本字符处理,破坏正则匹配和文本分割逻辑

修复方案

核心思路:先将文档中的所有CR换行符统一转换为LF,再执行原有的格式化逻辑,避免行尾字符不一致导致的异常。

修改后的Code Set 1

-- Code Set 1(修复CR字符问题)
tell application "Pages"
    activate
    try
        set theDoc to the front document
        tell theDoc
            set theText to body text as text
            -- 统一换行符为LF(替换CR为LF)
            set theText to replace_chars(theText, return, linefeed)
            set updatedText to my replaceScriptureReferences(theText)
            set body text to updatedText
        end tell
    on error errMsg
        display dialog "Error: " & errMsg buttons {"OK"} default button "OK" giving up after 5
    end try
end tell

on replaceScriptureReferences(theText)
    set AppleScript's text item delimiters to "("
    set textItems to text items of theText
    set AppleScript's text item delimiters to ""
    
    set updatedText to ""
    
    repeat with thisItem in textItems
        if thisItem contains ")" then
            set parenContent to text 1 thru ((offset of ")" in thisItem) - 1) of thisItem
            if parenContent contains ":" then
                set scriptureReferences to {}
                set scripturePattern to "\\d*\\s*[A-Za-z]+\\s*\\d+:\\d+(-\\d+)?"
                -- 处理shell命令中的换行符问题,确保grep正确识别
                set refMatches to paragraphs of (do shell script "echo " & quoted form of parenContent & " | grep -oE " & quoted form of scripturePattern) as list
                
                set replacedReferences to {}
                repeat with aLine in refMatches
                    set end of replacedReferences to "[[" & aLine & "]]"
                end repeat
                
                set updatedText to updatedText & (replacedReferences as text) & text ((offset of ")" in thisItem) + 1) thru -1 of thisItem
            else
                set updatedText to updatedText & "(" & thisItem
            end if
        else
            set updatedText to updatedText & thisItem
        end if
    end repeat
    
    return updatedText
end replaceScriptureReferences

-- 通用字符替换 handler
on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

修改后的Code Set 2

-- Code Set 2(修复CR字符问题)
tell application "Pages"
    activate
    try
        set doc to front document
    on error
        display dialog "No Pages document is open." buttons {"OK"} default button "OK"
        return
    end try
    
    try
        set textBody to the body text of doc
        -- 统一换行符为LF(替换CR为LF)
        set textBody to replace_chars(textBody, return, linefeed)
    on error
        display dialog "Failed to retrieve the body text from the document." buttons {"OK"} default button "OK"
        return
    end try
    
    try
        -- 处理经文引用换行,同时兼容LF格式
        set modifiedText to do shell script "echo " & quoted form of textBody & " | sed -E 's/\\[\\[([^]]+)\\]\\]/\\n[[\\1]]/g'"
        set body text of doc to modifiedText
    on error
        display dialog "An error occurred while processing the text." buttons {"OK"} default button "OK"
        return
    end try
end tell

-- 通用字符替换 handler
on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

验证说明

修改后的脚本会先将文档中的CR换行符统一转换为LF,确保AppleScript和shell工具的文本处理逻辑一致,之后再执行原有的经文格式化操作。Pages文档完全支持LF换行符,不会影响文档的显示和编辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 06:34:58