Re: Simple rot-13 encoding script
Re: Simple rot-13 encoding script
- Subject: Re: Simple rot-13 encoding script
- From: Paul Skinner <email@hidden>
- Date: Wed, 21 May 2003 01:40:13 -0400
Well, someone's already pointed out the missing 'set', and then you'd
run into a problem when the search_string matched a previously replaced
replacement_string. ie in the first replacement you replace 'a' with
'n' but later you replace 'n' with 'a'.
You've got the right idea with the handler to do the replacement, but
you should always look for repetition in your script and try to replace
it with loops where possible. In this case you can put all the search
strings and replacement strings in a list and then loop through them
feeding them to the handler.
This handler does basically what you were trying to do. It uses a
flagCharacter to avoid the replacement of replaced text issue.
Rot13("AppleScript looks easy.") -->"NccyrFpevcg ybbxf rnfl."
Rot13(the result) -->"AppleScript looks easy."
on Rot13(the textToRotate)
set the storedDelimiter to AppleScript's text item delimiters
set the alphabet to
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
set the rotAlphabet to
"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
set the flagCharacter to ASCII character 1
set AppleScript's text item delimiters to the flagCharacter
set the textToRotate to (the flagCharacter & the characters of
textToRotate) as text
repeat with x from 1 to 52
if the textToRotate contains (the flagCharacter & item x of the
alphabet) then
set AppleScript's text item delimiters to (the flagCharacter & item
x of the alphabet)
set the textToRotate to text items of the textToRotate
set AppleScript's text item delimiters to {item x of the rotAlphabet}
set the textToRotate to the textToRotate as text
end if
end repeat
set AppleScript's text item delimiters to the flagCharacter
set the textToRotate to (text items of the textToRotate)
set AppleScript's text item delimiters to ""
set the textToRotate to the textToRotate as text
set AppleScript's text item delimiters to the storedDelimiter
return the textToRotate as text
end Rot13
On Tuesday, May 20, 2003, at 08:27 PM, Ashlin Aronin wrote:
Hello all! I made this simple script to encode rot-13, but it isn't
working. Here's my code:
set currentChar to 1
display dialog "rot-13" with icon note default answer "" buttons
["Cancel",
"OK"] default button 2
if (text returned of result) is not equal to "" then
set the message_text to (text returned of result)
else
display dialog "You didn't enter anything." buttons ["OK"] default
button 1
quit
end if
set AppleScript's text item delimiters to ""
set allChars to every text item of the message_text
repeat until currentChar = (count allChars)
set allChars to replace_chars(item currentChar of allChars, "a",
"n")
set allChars to replace_chars(item currentChar of allChars, "b",
"o")
set allChars to replace_chars(item currentChar of allChars, "c",
"p")
set allChars to replace_chars(item currentChar of allChars, "d",
"q")
set allChars to replace_chars(item currentChar of allChars, "e",
"r")
set allChars to replace_chars(item currentChar of allChars, "f",
"s")
set allChars to replace_chars(item currentChar of allChars, "g",
"t")
set allChars to replace_chars(item currentChar of allChars, "h",
"u")
set allChars to replace_chars(item currentChar of allChars, "i",
"v")
set allChars to replace_chars(item currentChar of allChars, "j",
"w")
set allChars to replace_chars(item currentChar of allChars, "k",
"x")
set allChars to replace_chars(item currentChar of allChars, "l",
"y")
set allChars to replace_chars(item currentChar of allChars, "m",
"z")
set allChars to replace_chars(item currentChar of allChars, "n",
"a")
set allChars to replace_chars(item currentChar of allChars, "o",
"b")
set allChars to replace_chars(item currentChar of allChars, "p",
"c")
set allChars to replace_chars(item currentChar of allChars, "q",
"d")
set allChars to replace_chars(item currentChar of allChars, "r",
"e")
set allChars to replace_chars(item currentChar of allChars, "s",
"f")
set allChars to replace_chars(item currentChar of allChars, "t",
"g")
set allChars to replace_chars(item currentChar of allChars, "u",
"h")
set allChars to replace_chars(item currentChar of allChars, "v",
"i")
set allChars to replace_chars(item currentChar of allChars, "w",
"j")
set allChars to replace_chars(item currentChar of allChars, "x",
"k")
set allChars to replace_chars(item currentChar of allChars, "y",
"l")
set allChars to replace_chars(item currentChar of allChars, "z",
"m")
currentChar = currentChar + 1
end repeat
display dialog allChars buttons ["OK"] default button 1
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
Can anyone tell me what's wrong?
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.