我有这个字符串,有我想删除的非法字符,但我不知道可能存在什么样的字符。Applescript:清除字符串
我构建了一个我不希望被过滤的字符列表,并且我构建了该脚本(从我在网上找到的另一个字符串)。
on clean_string(TheString)
--Store the current TIDs. To be polite to other scripts.
set previousDelimiter to AppleScript's text item delimiters
set potentialName to TheString
set legalName to {}
set legalCharacters to {"a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "0", "?", "+", "-", "Ç", "ç", "á", "Á", "é",
"É", "í", "Í", "ó", "Ó", "ú", "Ú", "â", "Â", "ã", "Ã", "ñ", "Ñ",
"õ", "Õ", "à", "À", "è", "È", "ü", "Ü", "ö", "Ö", "!", "$", "%",
"/", "(", ")", "&", "€", "#", "@", "=", "*", "+", "-", ",", ".",
"–", "_", " ", ":", ";", ASCII character 10, ASCII character 13}
--Whatever you want to eliminate.
--Now iterate through the characters checking them.
repeat with thisCharacter in the characters of potentialName
set thisCharacter to thisCharacter as text
if thisCharacter is in legalCharacters then
set the end of legalName to thisCharacter
log (legalName as string)
end if
end repeat
--Make sure that you set the TIDs before making the
--list of characters into a string.
set AppleScript's text item delimiters to ""
--Check the name's length.
if length of legalName is greater than 32 then
set legalName to items 1 thru 32 of legalName as text
else
set legalName to legalName as text
end if
--Restore the current TIDs. To be polite to other scripts.
set AppleScript's text item delimiters to previousDelimiter
return legalName
end clean_string
问题是,这个脚本是慢地狱,并给我超时。
我在做什么是检查字符的字符和比较legalCharacters列表。如果角色在那里,那很好。如果不是,请忽略。
有没有一种快速的方法来做到这一点?
像
“看TheString的每一个字符,并删除那些不legalCharacters”
?
感谢您的任何帮助。
谢谢,但这个循环给我这个错误结果: 错误“AppleEvent超时。”号码-1712 ...我想文本太长,applescript不愿意等它完成。 – SpaceDog 2010-05-06 22:00:02
我已经给代码添加了一个超时模块,但是您不应该在这里获取(我相信默认超时时间为60秒)。我在这个页面的完整文本上运行代码没有任何问题。我认为你可能不得不把周围的调用包装到子程序的调用或堆栈中更高的地方。 – 2010-05-07 00:03:51