Applescript 在一个文档上工作,但在几乎相同的文档上崩溃

问题描述 投票:0回答:1
-- Code Set 1

tell application "Pages"
    activate
    try
        set theDoc to the front document
        tell theDoc
            set theText to body text as text
            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+)?"
                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

-- Code Set 2

tell application "Pages"
    activate
    try
        -- Attempt to get the front document
        set doc to front document
    on error
        -- Display an error dialog if no Pages document is open
        display dialog "No Pages document is open." buttons {"OK"} default button "OK"
        return
    end try
    
    try
        -- Attempt to retrieve the body text of the document
        set textBody to the body text of doc
    on error
        -- Display an error dialog if unable to retrieve the body text
        display dialog "Failed to retrieve the body text from the document." buttons {"OK"} default button "OK"
        return
    end try
    
    try
        -- Move text within double brackets to the next line
        set modifiedText to do shell script "echo " & quoted form of textBody & " | sed -E 's/\\[\\[([^]]+)\\]\\]/\\n[[\\1]]/g'"
        
        -- Attempt to set the modified body text back to the document
        set body text of doc to modifiedText
    on error
        -- Display an error dialog if any issues occur during the text modification process
        display dialog "An error occurred while processing the text." buttons {"OK"} default button "OK"
        return
    end try
end tell

这是我的程序,可以自动为我的演示软件格式化我的经文。 经文引用应放在双括号 [[]] 中,以显示在我的演示软件中。我唯一的线索是在我的调试软件中,当我一步一步执行时,它在引入时在行尾显示 CR进入代码。有趣的是,有效的文件并没有显示 CR。我查看了隐藏的角色,看看是否有什么不同。我看不出有什么区别。 我会留下一份有效的讲道大纲和一份无效的讲道大纲。 本周这个讲道起了作用。这也是错误:错误:无法制作 {“Used at Canal heights 20241229b night service

”第 16 项的文本 11 到 -1

用于 Canal Heights 20241229a 早间服务

约翰书信:基督徒的行走。

简介

信徒的行走不是静止的,而是每天的旅程。 基督徒应该使我们的日常生活与基督的教导和榜样保持一致。 (约翰一书 2:6) 我们有一条明确的道路可以走。 (约翰福音 14:6) 本课将探讨这条路径。

行走在光明中

上帝没有黑暗。 (约翰一书 1:5;约翰福音 8:12) 对于那些跟随他的人来说,道路将是清晰的。 (诗 119:105;箴 3:5-6) 基督徒行事为人要纯洁。 (约翰一书 1:5-7,9;腓立比书 2:15)

行在真理中

基督徒要按照真理而行。 (约翰二书 1:4;箴言 3:3-4) 真理植根于神的话语。 (约翰福音 14:6;约翰福音 17:17) 偏离这条道路是要付出代价的。 (约翰二书 1:7-11)

行走在爱中

基督徒要凭爱心行事。 (约翰二书 1:5-6) 爱是上帝律法的成全。 (太 22:37-40;罗 13:8-10) 耶稣的爱的榜样。 (约翰一书 3:16;弗 5:2) 这份爱将成为世人的见证。 (约翰福音 13:35)

以忠实信徒的榜样

基督徒应该效仿好的榜样。 (约翰三书 1:12;诗篇 1:1-2) 保罗的例子。 (哥林多前书 11:1;腓立比书 3:17) 辨别好的例子。 (约翰一书 4:1;腓立比书 1:9-10)

结论

基督徒的行走是通往天堂目的地的旅程,将我们转变为基督的形象。 (罗马书 8:29) 我们每天必须选择符合神的心意的道路,为世界作见证。 让我们承诺走基督的道路。

不起作用的如下:

用于 Canal heights 20241229b 晚间服务

启示:基督胜利,邪恶失败,希望永存。

简介

启示录揭示了基督的胜利和邪恶的最终失败。 (启示录 1:17-18) 它鼓励基督徒坚持信仰,知道最终的结果。 (启示录 3:11) 这封信为各个时代的信徒提供了希望和保证。 (启示录 1:3) 本课将着眼于这场胜利。

基督得胜了

基督是万有的统治者。 (启示录 1:17-18,太 28:18) 基督战胜邪恶是有保证的。 (启示录 19:11-16;哥林多前书 15:24-26) 基督的胜利就是我们的胜利。 (约翰一书 5:4-5;约翰福音 16:33)

所有敌人都被击败

撒旦和他的势力将会被战胜。 (启示录 12:11) 死亡、罪恶和痛苦都将被摧毁。 (启示录 21:4;林前 15:54-57) 最终的审判将会带来正义。 (启示录 20:10-15;马太福音 25:31-33,46)

带着希望生活

基督徒要在生活中期待基督的再来。 (启示录 22:12) 过圣洁的生活,期待新的创造。 (启示录 21:1-4;约翰一书 3:2-3) 与他人分享基督再来的希望。 (启示录 22:17;彼得前书 3:15)

结论

基督徒要坚守基督的胜利,怀着希望和毅力生活。 (启示录 3:11) 我们的生活应该体现正义战胜邪恶的最终胜利。 (启示录 17:14) 当我们等待基督再来时,我们被呼召要生活在信心、希望和爱中,与世界分享他的信息。 (歌罗西书 1:5-6)

我已经使用这段代码几个月了,我认为有人可能知道发生了什么。 预先感谢您的任何见解。 布莱恩

applescript
1个回答
0
投票

您的脚本中存在一些问题,使调试变得更加困难。 第一个是让页面显示对话框(因为它是在页面告诉语句中完成的) - 正如您所注意到的,没有超时它就会挂起。 将

display dialog
语句移出 Pages Tell 语句(或使用
my
定位它)可以避免这种情况。 第二个是错误对话框被放大,因为您正在使用文本项中的某个项的reference,因此整个列表都被包含在内。 在主重复循环中,取消引用该项目将使您更好地了解错误来自何处,例如:

   set thisItem to thisItem as text

从那里你会发现你的错误是当经文引用位于一行的末尾并且你试图越过结束括号时。 快速解决方法是将偏移量与线的长度进行比较,例如:

   set theOffset to (offset of ")" in thisItem)
   if theOffset is (length of thisItem) then -- check if at end of line
      set updatedText to updatedText & (replacedReferences as text) & text 1 thru -2 of thisItem
   else
      set updatedText to updatedText & (replacedReferences as text) & text (theOffset + 1) thru -1 of thisItem
   end if
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.