How do I convert a dialog asnwer to a number to use in a formula

OK, I’m new to this, but this excellent site I’m so close to finishing my project. I need to remove X characters from a text entry. I want to ask the user how many characters to remove. I can get the dialog to pop up and ask, and when I enter, say “3”, I then want to use that answer in a forumla.

The input code I’m using looks like this:

--Ask number of digits
to input_number(p, v, i)
	set v to text returned of (display dialog p default answer v with icon i) as integer
	try
		return v as number
	end try
	beep
	input_number("The entry must be a number:", v, 2)
end input_number
input_number("Enter digits of numbering scheme:", "", 1)

Now, from what I can see, its setting v to text returned. But when I reference V later, I get “The variable v is not defined.” I’ve tried “set v to number” but that doesn’t work, and neither did integer. I also tried adding “global v” to the top of the script, but that didn’t help either.

Oh, just in case it matters, I’m using the following line of code to truncate the text:

set shorter_Text to text 1 thru ((v + 1) * -1) of original_Text

So, I give in … can someone tell me what I’m missing?

Hi DJ,

I would do it like this

--Ask number of digits
to input_number(p, v, i)
	repeat
		set t to text returned of (display dialog p default answer v with icon i)
		try
			return t as integer
		on error
			beep
			display dialog "The entry must be a number" buttons {"OK"} default button 1 with icon caution giving up after 3
		end try
	end repeat
end input_number
input_number("Enter digits of numbering scheme:", "", 1)

would I then reference t or v down below where the formula actually is?

Oh, and slightly off-topic … how to I insert a quote (“)into a string of text? I tried “”” but that didn’t work.

t or v are only local variables within the handler.
The returned result is not stored into a variable.

But you can write:

set v to input_number("Enter digits of numbering scheme:", "", 1)

this v is a different v from the handler’s v

That still didn’t work for me. I made the change above, then went down to the formula and used v there … and I still get “The variable v is not defined”

Do I need to remove all other reference to v, I noticed in the changes you made, there’s v in the to and first set statements, but then you use t.

Also, I actually ask two questions of the user that are the same issue, but I’m just using x for one, v for the other.

And thank you very much for your help … I truly appreciate it.

is the formula also within a handler?

You can define a global variable as you did
but then it is valid in all levels of code.
I prefer to use more significant names for global variables than single letters to avoid confusion :wink:

Thank you, that was the solution. I set global variables as digits_In_Scheme and that worked.

I have another problem now, but I’ll start a new thread for easier searching.

with a backslash, for example: “how to I insert a quote (")into a string of text? I tried """ but that didn’t work.”

Thank you, worked perfectly.