Apple 脚本:清理 textedit 文本

Apple Script : Cleaning textedit text

我想从文本编辑文本中清除一些文本,但它只对我有用

tell application "TextEdit"

    set documentText to text of document 1
    set {oldTID, my text item delimiters} to {my text item delimiters, "Administration"}
    set textBits to text items of documentText
    set my text item delimiters to ""
    set text of document 1 to textBits as text
    set my text item delimiters to oldTID

    set documentText to text of document 1
    set {oldTID, my text item delimiters} to {my text item delimiters, "New Search"}
    set textBits to text items of documentText
    set my text item delimiters to ""
    set text of document 1 to textBits as text
    set my text item delimiters to oldTID

    set documentText to text of document 1
    set {oldTID, my text item delimiters} to {my text item delimiters, "Advanced"}
    set textBits to text items of documentText
    set my text item delimiters to ""
    set text of document 1 to textBits as text
    set my text item delimiters to oldTID


    set documentText to text of document 1
    set {oldTID, my text item delimiters} to {my text item delimiters, "Tips"}
    set textBits to text items of documentText
    set my text item delimiters to ""
    set text of document 1 to textBits as text
    set my text item delimiters to oldTID

end tell

出于某种原因 "New Search" 仍在文本文档中。 例如,是否可以只保留第 2 行到第 3 行的文本?

亲切的问候。

多个文本项定界符调用对于 AppleScript 来说可能很棘手。试试这个:

tell application "TextEdit" to set documentText to text of document 1

tell application "TextEdit" to set text of document 1 to my RemoveThis(documentText, {"Administration", "New Search", "Advanced", "Tips"})

to RemoveThis(txt, termList)
    repeat with eachTerm in termList
        set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, (eachTerm as text)}
        set textBits to text items of txt
        set AppleScript's text item delimiters to oldTID
        set txt to (textBits as text)
    end repeat
    return txt
end RemoveThis

通过这种方式,您可以定义要筛选的术语列表,而不必担心编码细节。