max character length in prompt

hey I was just wondering how you can make it so when your prompt comes up it only allows the user to type a set maximum number of characters. thnx!:slight_smile:

You can control how many characters you accept, but not how many they type in.

You could indicate the length crudely and then grab the first part thus:

set sp to ""
repeat 30 times
	set sp to sp & "n"
end repeat
set sp to "|" & sp & "|"

set ans to text returned of (display dialog "Your answer should fit above the second line" default answer "" & return & sp)
set o to text 1 thru ((offset of "|" in ans) - 1) of ans

Or, something like this:

repeat
	set ans to text returned of (display dialog "Your answer should be maximum 30 characters long" default answer "")
	if length of ans is not greater than 30 then exit repeat
end repeat

A repeat loop could also be used to propose a truncation of any answer that’s too long:

to get_answer to q for n
	set a to ""
	repeat
		set a to text returned of (display dialog q & return & "(maximum " & n & " characters)" default answer a)
		if a's length is not greater than n then return a
		set a to a's text 1 thru n
	end repeat
end get_answer

get_answer to "Please enter some text." for 30

Or with just a wee mod to change the question:

to get_answer to q for n
	set a to ""
	repeat
		set a to text returned of (display dialog q & return ¬
			& "(maximum " & n & " characters)" default answer a)
		if a's length is not greater than n then return a
		set a to a's text 1 thru n
		set q to "Would this much be Okay?"
	end repeat
end get_answer

get_answer to "Please enter some text." for 10

{I changed the limit to 10 for easier illustration}