我有這個字符串,有我想刪除的非法字符,但我不知道可能存在什麼樣的字符。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