Apple脚本:清理textedit文本

时间:2015-06-04 15:46:15

标签: text applescript textedit

我想从textedit文本中清除一些文本,但它只有一半为我工作

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

出于某种原因,“新搜索”仍在文本文档中。 那是否可能只保留第2行到第3行的文本?

亲切的问候。

1 个答案:

答案 0 :(得分:1)

对于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

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