higlighting conflicts between text

i’m making a typing tutor program, and i need the program to highlight the letters that were incorrectly typed. i thought of doing it with one black text field and then a red text field on top that has the incorrect letters when necessary. (i have a fixed width font and the field is not an input field…)

also i need to erase the contents of the field without deselecting it, so you dont have to press tab to reselect it after each lesson (without using UI scripting)…

anybdy?

Not sure about the highlighting. I’m surprised there’s not a way to control text color via applescript studio on a character/word basis (but not THAT surprised given the troubles I’ve had doing any kind of text formatting in TextEdit via AS).

As far as emptying the text field without deselecting it why not just set it’s contents to “”. That should even select it if it’s not already.

i did that and it deselected the text field, i need it to empty the contents and stay selected

is there a way to simply find out which character does not match (like the 5th letter)?

The code below will compare the contents of the text field ‘charInput’ with the predefined variable ‘targetChars’ and return a string of dashes and characters, dashes being the characters that were correct, and characters being those that were incorrect.

on clicked theObject
	if name of theObject is "findBadChars" then
		set targetChars to "aBcDeFgH"
		
		set theOutput to ""
		set theInput to contents of text field "charInput" of window "window"
		set theInputAsChars to (characters of theInput) as list
		
		set charIndex to 0
		repeat with tempChar in theInputAsChars
			set charIndex to (charIndex + 1)
			considering case
				if (tempChar as string) is equal to ((character charIndex of targetChars) as string) then
					set theOutput to (theOutput & "-")
				else
					set theOutput to (theOutput & tempChar)
				end if
			end considering
		end repeat
		
		set contents of text field "charOutput" of window "window" to theOutput
	end if
end clicked

For example if you enter “aBcDeFgH” into the text field, you’ll get “--------” back because all characters match their corresponding characters in the target string. If you enter “ABCDEFXH” you’ll get back “A-C-E-X-”, because the lower case ones were entered as upper case, and the X was not the correct letter. How you interpret the data and display the error results I’ll leave to your imagination. :smiley:

j

Found an even better way. You will not be able to use text fields, only text views (in scroll views). Set up a text view and add the following code…

on clicked theObject
	if name of theObject is "CheckSpelling" then
		set color of text of text view "contents" of scroll view "text" of window "window" to {0, 0, 0} 
		set targetChars to "What is the meaning of life?"
		set badCharIndexList to {}

		set theInput to contents of text view "contents" of scroll view "text" of window "window"
		set theInputAsChars to (characters of theInput) as list
		
		set charIndex to 0
		repeat with tempChar in theInputAsChars
			set charIndex to (charIndex + 1)
			considering case
				if (tempChar as string) is not equal to ((character charIndex of targetChars) as string) then
					set badCharIndexList to (badCharIndexList & charIndex)
				end if
			end considering
		end repeat

		set myTextRef to (a reference to (text of text view "contents" of scroll view "text" of window "window"))
		repeat with theChar in badCharIndexList
			set color of character theChar of myTextRef to {65535, 0, 0}
		end repeat
	end if
end clicked

Since only text views can handle formatted text, you have to use one instead of a regular text field. This code will highlight any incorrect character in the typing field itself in red and leave all correct ones black. I placed the actual highlighting code in a seccond repeat because it seems to make it update more closely to “all at once” than doing it while in the first repeat loop.

BTW, you should be able to use the following at any time to erase the contents of and/or focus your text view for immediate use…

set contents of text view "contents" of scroll view "text" of window "window" to ""
set first responder of window "window" to text view "contents" of scroll view "text" of window "window"

Hope this is a little more along the lines of what you’re looking for…
j

thanks, that worked nicely