set rotd to rot13("Frank was a rotten bastard.")
set rotdback to rot13(rotd)
return rotdback & rotd
on rot13(g)
set theDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {""}
set x to 0
set theList to ""
repeat while x < 256
set theList to theList & {ASCII character x}
set x to x + 1
end repeat
set g to (characters 1 thru -1 of g) as list
set x to count g
repeat while x > 0
set thecharsnum to ((offset of (item x of g) in theList) - 1)
set rotby to 0
if (thecharsnum „ 65 and thecharsnum ¾ 77) or ¬
(thecharsnum „ 97 and thecharsnum ¾ 109) then set rotby to 13
if (thecharsnum „ 78 and thecharsnum ¾ 90) or ¬
(thecharsnum „ 110 and thecharsnum ¾ 122) then set rotby to -13
set item x of g to (ASCII character (thecharsnum + rotby))
set x to x - 1
end repeat
set AppleScript's text item delimiters to theDelimiters
return g as string
end rot13
--
J. B. Moreno
Pessimist: Half empty Optimist: Half full Engineer: Wrong size glass
Here's a faster one, using the Apple-provided method of converting a
character into a number:
on rot13(ttxt)
set ttxt to every character of ttxt as list
repeat with ix from 1 to count of ttxt
set item ix of ttxt to ¬
ASCII character (item (ASCII number¬
(item ix of ttxt)) of outtable)
end repeat
set ttxt to ttxt as text
return ttxt
end rot13
This version depends on a property-stored table (which is why it is faster)
that you can either build manually or have an initialization routine that
builds it on the first run after the last compile.
Here's the init code:
-- Somewhere not inside handlers:
property tableNotBuilt : true
property outtable : {}
---- at the top of the RUN handler:
if tableNotBuilt is true then
repeat with ix from 1 to 255
set outtable to outtable & ix -- faster than testing
if (((ix „ (ASCII number "a")) and¬
(ix ¾ (ASCII number "m"))) or¬
((ix „ (ASCII number "A")) and¬
(ix ¾ (ASCII number "M")))) then
set item ix of outtable to ix + 13
else
if (((ix „ (ASCII number "n")) and¬
(ix ¾ (ASCII number "z"))) or¬
((ix „ (ASCII number "N")) and¬
(ix ¾ (ASCII number "Z")))) then
set item ix of outtable to ix - 13
end if
end if
end repeat
set tableNotBuilt to false
end if
If you want, you can run this, have it return OutTable, and then paste the
table contents back into the OutTable property. Then you can eliminate all
references to TableNotBuilt and delete the init routine entirely.
Or you can just copy-paste it from here:
property outtable : {1, 2, 3, 4, 5, 6, 7, 8, ¬
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ¬
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, ¬
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, ¬
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, ¬
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, ¬
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, ¬
89, 90, 65, 66, 67, 68, 69, 70, 71, 72, 73, ¬
74, 75, 76, 77, 91, 92, 93, 94, 95, 96, 110, ¬
111, 112, 113, 114, 115, 116, 117, 118, 119, ¬
120, 121, 122, 97, 98, 99, 100, 101, 102, 103, ¬
104, 105, 106, 107, 108, 109, 123, 124, 125, ¬
126, 127, 128, 129, 130, 131, 132, 133, 134, ¬
135, 136, 137, 138, 139, 140, 141, 142, 143, ¬
144, 145, 146, 147, 148, 149, 150, 151, 152, ¬
153, 154, 155, 156, 157, 158, 159, 160, 161, ¬
162, 163, 164, 165, 166, 167, 168, 169, 170, ¬
171, 172, 173, 174, 175, 176, 177, 178, 179, ¬
180, 181, 182, 183, 184, 185, 186, 187, 188, ¬
189, 190, 191, 192, 193, 194, 195, 196, 197, ¬
198, 199, 200, 201, 202, 203, 204, 205, 206, ¬
207, 208, 209, 210, 211, 212, 213, 214, 215, ¬
216, 217, 218, 219, 220, 221, 222, 223, 224, ¬
225, 226, 227, 228, 229, 230, 231, 232, 233, ¬
234, 235, 236, 237, 238, 239, 240, 241, 242, ¬
243, 244, 245, 246, 247, 248, 249, 250, 251, ¬
252, 253, 254, 255}
I used this code to add ROT-13 to the script menu of OE5.
--
--------------------
Kirk Kerekes
Red Gate Ranch
kker...@earthlink.net
> in article 070320001718161800%pl...@newsreaders.com, J. Moreno at
> pl...@newsreaders.com may have written:
Just a suggestion, but the message id is supposed to include the
brackets (I know, OE leaves them off by default, but that's wrong).
> > I've seen several comments about ROT13 in the last couple of days, so
> > here's an applescript that does it (and also shows one way to turn a
> > character into a number, which is where I had the most difficulty).
> >
> >
-snip-
> >
> > on rot13(g)
> > set theDelimiters to AppleScript's text item delimiters
> > set AppleScript's text item delimiters to {""}
> > set x to 0
> > set theList to ""
> > repeat while x < 256
> > set theList to theList & {ASCII character x}
> > set x to x + 1
> > end repeat
-snip-
> > set thecharsnum to ((offset of (item x of g) in theList) - 1)
-snip-
> Here's a faster one, using the Apple-provided method of converting a
> character into a number:
I hadn't known (or had forgotten) about the ascii number method of
converting a character into a number -- in light of that I've rewritten
it.
> on rot13(ttxt)
> set ttxt to every character of ttxt as list
> repeat with ix from 1 to count of ttxt
> set item ix of ttxt to ¬
> ASCII character (item (ASCII number¬
> (item ix of ttxt)) of outtable)
> end repeat
> set ttxt to ttxt as text
> return ttxt
> end rot13
>
> This version depends on a property-stored table (which is why it is faster)
> that you can either build manually or have an initialization routine that
> builds it on the first run after the last compile.
set rotd to rot13("Frank was a rotten bastard.")
set rotd_back to rot13(rotd)
return rotd_back & return & rotd
on rot13(input_string)
set input_string to (characters 1 thru -1 of input_string) as list
repeat with x from 1 to count input_string
set thechar to item x of input_string
set rotby to 0
if (thechar „ "a" and thechar ¾ "m") or ¬
(thechar „ "A" and thechar ¾ "M") then set rotby to 13
if (thechar „ "n" and thechar ¾ "z") or ¬
(thechar „ "N" and thechar ¾ "Z") then set rotby to -13
set item x of input_string to (ASCII character ¬
((ASCII number thechar) + rotby))
end repeat
return input_string as string
end rot13
which is about twice as fast as it was (and still half as fast as
yours, that table is sure a speed up, I'd have thought it'd have been
faster to just do the number conversion -- must be all the
comparisons).
I've also changed it back to using alphabetic comparisons ("a", "m"
instead of 65, 77), I wonder if that makes a difference one way or
another.
> Do you know of any way to get the ASCII number of a particular character
> without using the scripting addition routine. Having to do it that way seems
> ridiculous, when the character will be stored in memory in an identical way to
> the resulting number.
No it won't. It'll be stored as a one-byte member of a zero-terminated
string - even if it's the only character in that string. An integer
number will be stored as a four-byte value.
NG
--
To e-mail me, omit the title from my name and '.invalid' from the domain.
> Any ideas? Or any scripting additions that can do Base64?
> Thanks in advance for any response.
Yes, stuffit. :)
Send the file to an app that will handle the Base64. AppleScript is
really too slow for this kind of stuff.
--
I don't believe we've met!
To send polite or professional replies remove the asterixes,
and everything in between