Setting a character countdown field

In my mini twitter client I need to be able to count of the number of characters in a text field and show how many they have left. This is what I tried, but it minuses 1 the first time you type something and then doesn’t do anything from there.


property maxChars : 140
on changed theObject
	if the name of theObject is "tweetField" then
		set theCount to count (contents of text field "tweetField" of window "main")
		set charsLeft to (maxChars - theCount)
		set the contents of text field "charsLeft" of window "main" to (charsLeft as string)
		end if
end changed

Hi,

the magic word is get

 set theCount to count (get contents of text field "tweetField" of window "main")

Here is your code slightly changed

on changed theObject
	if the name of theObject is "tweetField" then
		set theCount to count (get string value of theObject)
		set integer value of text field "charsLeft" of window "main" to (maxChars - theCount)
	end if
end changed

Ahh ok, thanks a lot! Works great :smiley:
Is there a way to stop the user from entering text once the counter hits 0? I could disable the text field but then the user couldn’t edit anything.

EDIT: Solved by StefanK

if theCount ≥ 140 then
			set string value of text field "tweetField" of window "main" to text 1 thru -2 of (get string value of text field "tweetField" of window "main")
			beep
		end if