encoder / decoder

is there any easy way to make a simple letter substitiution encoder/decoder with applescript?

Could you be little more specific about what you mean by encode/decode and what you’re trying to accomplish? The following code works on a static key/value pairs concept. It gets the contents of a text field named “theInput”, and will switch any character it matches from the “keys” list with the corresponding value in the “values” list. Then it spits it out into the contents of a “theOutput” text field. You’ll have to manually enter all characters to search for and replace in both of the two lists. The function searches them by index id, so you’ll have to make sure all of your items line up between the two lists, too.

property Keys : {"a", "b", "c", "A", "B", "C", "D", "E", "F"}
property Values : {"u", "v", "w", "U", "V", "W", "X", "Y", "Z"}
property KeyValue : missing value

on clicked theObject
	if name of theObject is "Encode" then
		set theOutput to ""
		set theInput to contents of text field "theInput" of window "window"
		set theInputAsChars to (characters of theInput) as list
		
		repeat with currentChar in theInputAsChars
			set keyIndex to 0
			
			repeat with KeyTemp in Keys
				set keyIndex to (keyIndex + 1)
				set KeyValue to ""
				considering case
					if (currentChar as string) is equal to (KeyTemp as string) then
						set KeyValue to item keyIndex of Values
						set theOutput to (theOutput & KeyValue)
					end if
				end considering
			end repeat

		end repeat

		set contents of text field "theOutput" of window "window" to theOutput
	end if
end clicked

For example, if you enter “aAbBcC” in ‘theInput’, you’ll get “uUvVwW” in ‘theOutput’.

Hope that helps…
j

that works perfectly, except it removes the spaces…

Just add an item in both of your lists that contains a space…

property Keys : {"a", "b", "c", "A", "B", "C", "D", "E", "F", " "}
property Values : {"u", "v", "w", "U", "V", "W", "X", "Y", "Z", " "} 

Any time it reads a space, it will just add one to the output variable.

j

okay, great i figured everything out, and now i’d like a code that gets the ascii number of each letter and and makes it a long code of numbers, maybe spaced apart by zeroes or something, but for some reason xcode coughs up when i try to use the ascii number code, which works in script editor. any ideas?

This worked for me…

on clicked theObject
	if name of theObject is "getAscii" then
		set theOutput to ""
		set theInput to contents of text field "asciiInput" of window "window"
		set theInputAsChars to (characters of theInput) as list
		
		repeat with currentChar in theInputAsChars
			set theOutput to theOutput & ((ASCII number of currentChar) as string)
		end repeat
		
		set contents of text field "asciiOutput" of window "window" to theOutput
	end if
end clicked

j

how would i make it so the encoder substituted a diffrent letter the second time it saw a letter.

like i hate cats would translate to

i hbte cgts

???

What you are looking for is a polyalphabetic substitution cipher, and as it happens, I wrote just such a thing in vanilla AppleScript a few years ago. Note: I haven’t looked at this in quite a while, it may be buggy…


VigenereEncode("Hello World", "my secret password") --> "?‹ä›“Å«“‰ä“"
VigenereDecode(result, "my secret password") --> "Hello World"


property ks256 : run script "
	set a to {}
	repeat with i from 0 to 255
		set a's end to ascii character i
	end
	return a as string"

on AsciiChar(n)
	return my ks256's item (n + 1)
end AsciiChar

on AsciiNum(c)
	set o to AppleScript's text item delimiters
	set AppleScript's text item delimiters to c
	set n to my ks256's text item 1's length
	set AppleScript's text item delimiters to o
	return n
end AsciiNum

on AsciiNum_t(c) --> tids are not protected (for speed)
	set AppleScript's text item delimiters to c
	return my ks256's text item 1's length
end AsciiNum_t

on VigenereEncode(s, k)
	
	set o to AppleScript's text item delimiters -- using AsciiNum_t()
	
	set k to k's items
	repeat with c in k
		set c's contents to AsciiNum_t("" & c) - 1
	end repeat
	
	set a to ks256 & ks256
	
	set ki to 1
	set klen to k's length
	
	set z to {}
	repeat with c in s
		set z's end to a's item (AsciiNum_t("" & c) + (k's item ki))
		if (ki = klen) then
			set ki to 1
		else
			set ki to ki + 1
		end if
	end repeat
	
	set text item delimiters to o
	
	return z as string
	
end VigenereEncode

on VigenereDecode(s, k)
	
	set o to AppleScript's text item delimiters -- using AsciiNum_t()
	
	set k to k's items
	repeat with c in k
		set c's contents to AsciiNum_t("" & c) - 1
	end repeat
	
	set a to ks256 & ks256
	
	set ki to 1
	set klen to k's length
	
	set z to {}
	repeat with c in s
		set z's end to a's item (AsciiNum_t("" & c) + 258 - (k's item ki))
		if (ki = klen) then
			set ki to 1
		else
			set ki to ki + 1
		end if
	end repeat
	
	set AppleScript's text item delimiters to o
	
	return z as string
	
end VigenereDecode

Note: I also have a version that is more “traditional” in that it only works with the letters A thru Z, (the above works with the 256 extended-ascii character set). If you want that version, just ask.

Whoops… it was buggy. :frowning: Here is a (quickly thrown together) complete re-write:


VigenereEncode("Hello World", "my secret password") --> "µ?å?‘É…‘Êå‘"
VigenereDecode(result, "my secret password") --> "Hello World"


property ks256 : run script "
	set a to {}
	repeat with i from 0 to 255
		set a's end to ascii character i
	end
	return a as string"

on AsciiChar(n)
	return my ks256's item (n + 1)
end AsciiChar

on AsciiCode(c)
	set o to AppleScript's text item delimiters
	set AppleScript's text item delimiters to c
	set n to my ks256's text item 1's length
	set AppleScript's text item delimiters to o
	return n
end AsciiCode

on AsciiCode_t(c) --> tids are not protected (for speed)
	set AppleScript's text item delimiters to c
	return my ks256's text item 1's length
end AsciiCode_t

on VigenereEncode(plain_text, key_text)
	set astids to AppleScript's text item delimiters
	try
		
		set key_table to {}
		
		repeat with i from 1 to key_text's length
			
			set key_table's end to AsciiCode_t(key_text's character i)
			
		end repeat
		
		set key_index to 1
		set key_length to length of key_table
		
		set alpha_text to my ks256 & my ks256
		
		set cipher_text to ""
		
		repeat with i from 1 to plain_text's length
			
			set plain_char_num to AsciiCode_t(plain_text's character i)
			
			set key_char_num to key_table's item key_index
			
			set cipher_char_num to plain_char_num + key_char_num
			
			set alpha_char to alpha_text's character (cipher_char_num + 1)
			
			set cipher_text to cipher_text & alpha_char
			
			if (key_index = key_length) then
				set key_index to 1
			else
				set key_index to key_index + 1
			end if
			
		end repeat
		
	on error e number n from f to t partial result p
		set AppleScript's text item delimiters to astids
		error e number n from f to t partial result p
	end try
	set AppleScript's text item delimiters to astids
	
	return cipher_text
	
end VigenereEncode

on VigenereDecode(cipher_text, key_text)
	set astids to AppleScript's text item delimiters
	try
		
		set key_table to {}
		
		repeat with i from 1 to key_text's length
			
			set key_table's end to AsciiCode_t(key_text's character i)
			
		end repeat
		
		set key_index to 1
		set key_length to length of key_table
		
		set alpha_text to my ks256 & my ks256
		
		set plain_text to ""
		
		repeat with i from 1 to cipher_text's length
			
			set cipher_char_num to AsciiCode_t(cipher_text's character i)
			
			set key_char_num to key_table's item key_index
			
			set plain_char_num to 256 + cipher_char_num - key_char_num
			
			set alpha_char to alpha_text's character (plain_char_num + 1)
			
			set plain_text to plain_text & alpha_char
			
			if (key_index = key_length) then
				set key_index to 1
			else
				set key_index to key_index + 1
			end if
			
		end repeat
		
	on error e number n from f to t partial result p
		set AppleScript's text item delimiters to astids
		error e number n from f to t partial result p
	end try
	set AppleScript's text item delimiters to astids
	
	return plain_text
	
end VigenereDecode

ok thats wayyyyy to complicated for my small mind

heres my code

on clicked theObject
	set Keys to {"r", "w", "x", "d", "-"}
	set Values to {" Read", " Write", " Open", "You can:", ", Group can:", "Others can:"}
	set KeyValue to missing value
	
	set destination_folder to (choose file with prompt "Unlock what file?") as string
	set new_destination to FindAndReplace(destination_folder, ":", "/")
	
	set listing to do shell script "cd new_destination; ls -l"
	set thelines to (paragraphs of listing)
	repeat with eachline in thelines
		set word1 to first word of eachline
	end repeat
	
	
	--decodeing here
	set theOutput to ""
	set theInput to word1
	set theInputAsChars to (characters of theInput) as list
	
	repeat with currentChar in theInputAsChars
		set keyIndex to 0
		
		repeat with KeyTemp in Keys
			set keyIndex to (keyIndex + 1)
			set KeyValue to ""
			considering case
				if (currentChar as string) is equal to (KeyTemp as string) then
					set KeyValue to item keyIndex of Values
					
					set theOutput to (theOutput & KeyValue)
				end if
			end considering
		end repeat
	end repeat
	--decoding ends here	
	display dialog theOutput buttons {"OK"} default button 1
	set the_button to the button returned of (display dialog "would you like to edit anything?" buttons {"Yes", "No"} default button 1)
	if the_button = "Yes" then
		set whatchange to the button returned of (display dialog "what do you want to change?" buttons {"All", "Group", "Other"})
		
		
	else if the_button = "No" thern
	end if
end clicked


on FindAndReplace(OriginalText, searchTerm, replacement)
	set AppleScript's text item delimiters to searchTerm
	set OriginalText to OriginalText's text items
	set AppleScript's text item delimiters to replacement
	set OriginalText to "" & OriginalText
	set AppleScript's text item delimiters to {""}
	return OriginalText
end FindAndReplace

it get sthe file permissions of eh file you choose,
i want it to decode it in a way everyone can understand, so far it says:
“you can: read write open read, group can: open read, group can: open”
i need it to change the second group can to others can

any easy wa to do that?

It was also not relavent to the issue, (I didn’t read your original posting very well, and got confused by the words “encoding” and “decoding”). Sorry.

My first inclination would be to keep the parsing as simple as possible, ie: I wouldn’t even bother with a repeat loop:


repeat with i from 2 to count lines of listing

    set unixFlags to text 2 thru 10 of line i of listing -- "rwxrw-r-x"

    set flagWords to ReadableUnixPermissions(unixFlags)
    --
    --> "you can: read write execute" & return &
    --  "group can: read write" & return & 
    --  "others can: read execute"

    -- do something with flagWords
end repeat

on ReadableUnixPermissions(flag_string)
	
	set s to ""
	
	set s to s & "you can:"
	if (flag_string's character 1 = "r") then set s to s & " read"
	if (flag_string's character 2 = "w") then set s to s & " write"
	if (flag_string's character 3 = "x") then set s to s & " execute"
	set s to s & return
	
	set s to s & "group can:"
	if (flag_string's character 4 = "r") then set s to s & " read"
	if (flag_string's character 5 = "w") then set s to s & " write"
	if (flag_string's character 6 = "x") then set s to s & " execute"
	set s to s & return
	
	set s to s & "others can:"
	if (flag_string's character 7 = "r") then set s to s & " read"
	if (flag_string's character 8 = "w") then set s to s & " write"
	if (flag_string's character 9 = "x") then set s to s & " execute"
	set s to s & return
	
end ReadableUnixPermissions

Here is a looping version, but it doesn’t offer any particular advantages:


on ReadableUnixPermissions(flag_string)
	set a to {"you can:", "group can:", "other can:"}
	set s to ""
	repeat with i from 1 to 3
		set s to s & item i of a
		if (flag_string's character 1 = "r") then set s to s & " read"
		if (flag_string's character 2 = "w") then set s to s & " write"
		if (flag_string's character 3 = "x") then set s to s & " execute"
		set s to s & return
		try
			set flag_string to flag_string's text 4 thru -1
		end try
	end repeat
	return s
end ReadableUnixPermissions