Я хочу удалить части строки после указанной c подстроки. Пример ввода:
"abc def 1987 ghi jkl" "1987"
Требуемый вывод:
"abc def"
Теперь я нахожу этот код. Это прекрасно работает.
set themovn to "abc def 1987 ghi jkl"
on splitText(theText, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theTextItems to every text item of theText
set AppleScript's text item delimiters to ""
return theTextItems
end splitText
set newn to splitText(themovn, "1987")
set newn to text item 1 of newn
Но подстрока может иметь и другие значения. Это группа. Поэтому я использую repaet:
set themovn to "abc def 1987 ghi jkl"
set allkeys to {"1987", "1080", "720"}
repeat with akey in allkeys
set newn to splitText(themovn, akey)
set newn to text item 1 of newn
end repeat
on splitText(theText, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theTextItems to every text item of theText
set AppleScript's text item delimiters to ""
return theTextItems
end splitText
Что интересно, это не работает. Может быть, это не правильный способ использовать обработчик. Поэтому я удаляю обработчик для проверки:
set themovn to "abc def 1987 ghi jkl"
set AppleScript's text item delimiters to "1987"
set theTextItems to every text item of themovn
set AppleScript's text item delimiters to ""
set newn to theTextItems
set newn to text item 1 of newn
Работает хорошо. Вывод правильный. Затем я поместил его в повторяющуюся структуру:
set themovn to "abc def 1987 ghi jkl"
set allkeys to {"1987", "1080p", "720"}
repeat with akey in allkeys
try
set AppleScript's text item delimiters to akey
set theTextItems to every text item of themovn
set AppleScript's text item delimiters to ""
set newn to theTextItems
set newn to text item 1 of newn
end try
end repeat
return newn
Не работает. Поэтому я хочу знать, почему тот же код не работает в повторяющейся структуре.