【问题标题】:How to use Applescript text item delimiters to find and replace text in Pages?如何使用 Applescript 文本项分隔符在 Pages 中查找和替换文本?
【发布时间】:2022-02-05 12:56:33
【问题描述】:

我有多个 Pages 文档,我需要在其中替换一组特殊字符 - 在我们的语言中,我们有一个字符的介词(例如 v、s、k、u、a),这些介词最后不能被孤立行数,所以我需要用介词和不可破坏的空格替换介词和下一个空格。一直在尝试像这样使用 AppleScript(我是编程新手):

set findList to {"v ", "s "}
set replaceList to {"v ", "s "}

set AppleScript's text item delimiters to ""

tell application "Pages"
    activate
    
    tell body text of front document
        repeat with i from 1 to count of findList
            set word of (words where it is (item i of findList)) to (item i of replaceList)
        end repeat
    end tell
end tell
return

只要findListreplaceList 参数中有空格,这将不起作用。

所以我发现text item delimiters 可能对我有帮助。我能够制作这个脚本

set theText to "Some of my text with v in it"
set AppleScript's text item delimiters to "v "
set theTextItems to text items of theText
set AppleScript's text item delimiters to "v " --this is v with non-breakable space (alt+space)
set theText to theTextItems as string
set AppleScript's text item delimiters to {""}
theText

这可行,但只能在代码的第一行设置纯文本(当我将结果复制到 Pages 时,确实有一个不可破坏的空间)。

但现在我需要编写一个脚本,它适用于 Pages 文档的整个文本。

我尝试过这样的事情:

tell application "Pages"
    activate
    
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "v "
    set textItems to body text of front document
    set AppleScript's text item delimiters to "v " --again v with non-breakable space (alt+space)
    tell textItems to set editedText to beginning & "v " & rest --again v with non-breakable space (alt+space)
    set AppleScript's text item delimiters to astid
    set text of document 1 to editedText
end tell

但我得到了错误

无法从“这是 Pages 文档的整个文本”开始。“从插入点 1 开始的编号 -1728 以及“”的整个文本文件"

如果我将脚本更改为:

tell application "Pages"
    activate
    
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "v "
    set textItems to text items of body text of front document
    set AppleScript's text item delimiters to "v "
    tell textItems to set editedText to beginning & "v " & rest
    set AppleScript's text item delimiters to astid
    set text of document 1 to editedText
end tell

我得到另一个错误

页面出现错误:无法获取文档 1 正文的每个文本项。" 从文档 1 正文的每个文本项中获取编号 -1728

谁能指出正确的方向如何正确编写此脚本?

谢谢。

【问题讨论】:

    标签: replace applescript document delimiter


    【解决方案1】:

    我希望这会对你有所帮助。这使用 AppleScript 的文本项目分隔符来拆分/连接文本。它可以更紧凑,但这是一种全面的编写方式。因为您可以在脚本中经常使用它,所以将它放在一个特殊的子例程中是一件好事。 我在一个地方建立了一个更容易维护的对 {search,replace} 列表,以及一个“重复”循环来应用每对更正。不要忘记“my”语句,因为 Pages 不拥有 strRepl() 并且会触发错误。

    不幸的是,提取文本并将其放回 Pages 会丢失任何文本属性。所以这里是:

    set findReplaceList to {{"a", "A"}, {"b ", "B"}, {"this", "that"}}
    
    tell application "Pages"
        set bodyText to body text of front document -- get the content as text
        repeat with thisFindReplaceValues in findReplaceList
            copy thisFindReplaceValues to {findItem, replaceItem} -- put first and second item resp. in findItem and replaceItem
            set bodyText to my strRepl(bodyText, findItem, replaceItem) -- search and replace text
        end repeat
        set body text of front document to bodyText -- put the new text back. Loosing attributes.
    end tell
        
    on strRepl(SourceStr, searchString, newString)
        set saveDelim to AppleScript's text item delimiters
        set AppleScript's text item delimiters to searchString -- change ATID : the search item
        set temporaryList to every text item in SourceStr -- split the text in parts removing searched items
        set AppleScript's text item delimiters to newString -- New ATID : the replace item
        set SourceStr to temporaryList as text -- this put back the parts to text with newString between
        set AppleScript's text item delimiters to saveDelim -- clean up ATIDs
        return SourceStr
    end strRepl
    

    【讨论】:

    • 这个工作方式与上面的一样,它将所有段落的样式更改为第一段的样式。是否有机会保留文本属性?
    【解决方案2】:

    此脚本是 @Chino22 的变体。鉴于这里的一致要求(始终是单个字母,由其自身替换),我已移至单个元素的简单列表并在调用处理程序时设置替换。

    -- List of prepositions to seek out (added the 'z' as it was prevalent in the article used for testing)
    set chList to {"v", "s", "k", "u", "a", "z"}
    
        tell application "Pages"
        set bodyText to body text of front document
        repeat with prep in chList
            -- call replacement handler
            set bodyText to my strRepl(bodyText, space & prep & space, space & prep & character id 160)
        end repeat
    
        set body text of front document to bodyText
    end tell
    
    on strRepl(srcStr, oldStr, newStr)
        
        set AppleScript's text item delimiters to oldStr
        considering case
            set temporaryList to every text item in srcStr
        end considering
        set AppleScript's text item delimiters to newStr
        set srcStr to temporaryList as text
        
        return srcStr
    end strRepl
    

    NB 我的搜索和替换字符串在字母前后都包含一个空格。这确保只有单字母单词受到影响。我添加了considering case 以进一步将搜索限制为小写字母。 'character id 160' 指定不间断空格。最后我省略了第一个和最后一个分隔符命令以减少混乱。您可以自行决定将它们添加回来。不会处理单个字母后跟标点符号。

    关于您看到的一些错误……它们可能是由于 Pages 在其 tell 块中的文本项分隔符存在问题。一般来说,您需要将脚本分成三个部分,如下所示:

    tell application "Pages" to set bt to body text of front document
    
    myriad delimiters stuff, including 'set editedText to…'
    
    tell application "Pages" to set body text of front document to editedText
    

    按照 Chino22 的建议使用处理程序,通过将所有工作放在处理程序中(在 tell 块之外)来规避这个问题。此外,“开始”和“休息”并不意味着您认为它们在 applescript 中所做的事情。最后,我阅读了有关在段落级别而不是在整个正文中工作的建议。这对您来说可能不是问题,但如果您正在处理非常大的文档并且遇到问题,可能值得对脚本进行一些修改。

    【讨论】:

    • 非常感谢,这个可行,但是它将所有段落的样式更改为第一段的样式。不知道为什么。
    • 那是一团糟。可能的原因是 Pages 很烂。也就是说,如上所述,也许可以将整个文本分成段落,然后为每个段落获取颜色/字体/大小,执行搜索和替换,然后重新设置颜色/字体/大小.就我个人而言,我发现页面很适合使用......它很容易做一些有趣的事情,然后让简单的事情几乎不可能。
    猜你喜欢
    • 1970-01-01
    • 2016-11-27
    • 2014-01-14
    • 2017-12-29
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多