Determine length of number returned

I trying to figure out the length of a number returned from a users input.

This is what I have so far…


tell application "Finder"
		activate
		set inset_number to text returned of (display dialog "Enter the Inset Number" default answer "000") as string
		repeat with i from 1 to (count inset_number)
			set thelength to item i of inset_number
			if thelength is 3 then
				set SpotColor to characters 2 thru 3 of inset_number as text
			end if
			if thelength is 2 then
				set SpotColor to inset_number as text
			end if
			if thelength is 1 then
				set SpotColor to "0" & inset_number as text
			end if
		end repeat


How about this:

set aNum to text returned of (display dialog "Enter a number" default answer "000") --> 012
-- aNum is now text:
count characters of aNum --> 3
-- do something else with (aNum as number)

Text returned from a dialog can be counted directly, fishheadtw:

set inset_number to text returned of (display dialog "Enter the Inset Number" default answer "000")
count inset_number

However, to achieve what I think you’re trying to do (end up with a string of 2 characters), you don’t really need to determine the length of the text. Instead, simply count from the end of the text, using negative indices (and, in case the input string is less than 2 characters, add some zero padding):

set inset_number to text returned of (display dialog "Enter the Inset Number" default answer "000")
set SpotColor to text -2 thru -1 of ("00" & inset_number)

(Note that no application tell statements, string coercions or repeat loops are required, either.) :slight_smile:

Pedants’ Corner: No explicit coercions, that is. In Tiger, the “00” is coerced to Unicode text implicitly. :wink:

Thanks kai, That was exactly what I was looking for, Works Great and shortens up my code a bit.

Thanks Again