A script to encode foreign characters for the web in Filemaker

I recently had to write a script to encode foreign characters for the web in Filemaker, and since it can be reused by just modifying the first line which contains the field names, I thought others might find it useful. Here it is:


set cleanfields to {"f_e_producer", "f_director", "f_screenwriter", "f_cinematographer", "f_composer", "f_sound", "f_credits", "f_print_source", "f_cast", "f_phrase", "f_dr_bio", "f_title", "f_synopsis"}

set exceptions to {" ", ",", "-", ":", ".", "?", "!", "$", "(", ")", "<", ">", "/", "'", "\"", "@", "&", "#", ";"}

set listf to {"� ", "è", "ì", "ò", "ù", "á", "é", "í", "ó", "ú", "�?", "�?", "�?", "�?", "�?", "Á", "�?", "Í", "�?", "�?", "˘", "¯", "�?", "�?", "�?", "�?", "�?", "â", "ê", "î", "ô", "û", "�?", "�?", "�?", "ã", "ñ", "õ", "�?", "�?", "Ï", "�?", "�?", "Ÿ", "ä", "ë", "ï", "ö", "ü", "ÿ"}
set listc to {224, 232, 236, 242, 249, 225, 233, 237, 243, 250, 200, 204, 210, 217, 193, 201, 205, 211, 218, 192, 253, 221, 194, 202, 206, 212, 219, 226, 234, 238, 244, 251, 195, 209, 213, 227, 241, 245, 196, 203, 207, 214, 220, 159, 228, 235, 239, 246, 252, 255}
repeat with y in cleanfields
	tell application "FileMaker Pro"
		tell window 1
			set synopsis to cell y of current record
		end tell
	end tell
	set len_syn to length of synopsis
	if len_syn > 0 then
		set char_syn to characters 1 through len_syn of synopsis as list
		set newstring to ""
		repeat with x in char_syn
			--if character is a number, a letter, or one of our accepted characters...
			if ((ASCII number x) > 48 and (ASCII number x) < 58) or ((ASCII number x) > 64 and (ASCII number x) < 91) or ((ASCII number x) > 96 and (ASCII number x) < 123) or exceptions contains x then
				set newstring to newstring & x as string
			else
				--encode
				if listf contains x then
					set mynum to offset of x in (listf as string)
					set newx to item mynum of listc
					
				else
					set newx to (ASCII number x) as string
					
				end if
				set newstring to newstring & "&#" & newx & ";" as string
				
			end if
		end repeat
		
		tell application "FileMaker Pro"
			tell window 1
				set cell y of current record to newstring
			end tell
		end tell
	end if
end repeat