Form submission via Safari and weird characters

Hi all,

wonder if anyone can give me a hand with this.

I have a script running that copies text from an InDesign file and passes it to Safari as a form submission (it url-encodes the data and sends it to a web server). It appears to run fine except that the web server accepts UTF-8 and ISO 8859 1 characters and (I believe) the text being grabbed from InDesign is formatted in MacRoman or something else.

Is there an easy way or something I’m missing that will encode the text for ISO 8859 1?

This seems to work for UTF-8

This is the code I found online to url encode:

on urlencode(theText)
	set theTextEnc to ""
	repeat with eachChar in characters of theText
		set useChar to eachChar
		set eachCharNum to ASCII number of eachChar
		if eachCharNum = 32 then
			set useChar to "+"
		else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
			set firstDig to round (eachCharNum / 16) rounding down
			set secondDig to eachCharNum mod 16
			if firstDig > 9 then
				set aNum to firstDig + 55
				set firstDig to ASCII character aNum
			end if
			if secondDig > 9 then
				set aNum to secondDig + 55
				set secondDig to ASCII character aNum
			end if
			set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
			set useChar to numHex
		end if
		set theTextEnc to theTextEnc & useChar as string
	end repeat
	return theTextEnc
end urlencode

If I manually set Safari to use UTF-8 it displays correctly, but Safari defaults to Latin 1 (ISO 8859 1) I think, and I can only imagine that all the Windows people who will be using the website will end up with wonky characters… ugh I’ve been fighting this for so long.

Best to use Python within AppleScript

do shell script "/usr/bin/python -c ‘import sys, urllib; print urllib.quote(sys.argv[1])’ " & quoted form of TheTextToEncode

This works like a charm and looks a lot simpler than your code.