how to set minimum string length?

I have a script which basically pops ip a display dialog and asks to enter a number. Technically the numbers should be 3 characters long, such as XXX however what i’d like is let’s say if someone enters 3, then it automatically adds two zeros in front of the number, like 3 would become 003 and if someone enters 18 then it becomes 018… and if someone enters 121 then it remains the same.

so far, i have this


	display dialog "What is the Number?" default answer ""
	set the orig_page to the text returned of the result
	if (count orig_page) is not in 3 then
		set low_page to "0" & orig_page
	end if

what this does is, if the character is eg. 8, then it only adds one zero to make it 08 and if its 18 then it makes it 018, but if i enter 121 then it does nothing. I think i need to set a “try” statement or something… can someone guide me in the right direction? thanks!

Hi,

Taking a slightly different tack. Try:

display dialog "What is the Number?" default answer ""
set the orig_page to the text returned of the result
--Add some buffer text
set long_string to "000" & orig_page
--Get last three characters -- comes out in an array.
set text_array to characters -3 thru -1 of long_string
-- Coerce the array into a string
set low_page to text_array as text

Or in one line:

display dialog "What is the Number?" default answer ""
set the orig_page to the text returned of the result
set low_page to (characters -3 thru -1 of ("000" & orig_page)) as text

Best wishes

John M

thanks John, i’m using the second (one liner) and it’s working perfectly!

Read this:

Nigel Garvey: http://bbs.applescript.net/viewtopic.php?id=11332