Apple Script: Очистка текста в тексте

Я хочу очистить текст от текста 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 ответ

Решение

Несколько вызовов разделителей текстовых элементов могут быть хитрыми для 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

Таким образом, вы можете определить список терминов, которые будут просеиваться, и не беспокоиться о деталях кодирования.

Другие вопросы по тегам