using delay with a variable?

I was wondering if anybody knew how to use a variable with the delay command.

thanks,

ben

set theDelay to 2
beep
delay theDelay
beep

j

that works like that but it doesn’t work if I try to have a prompt ask you how many seconds you want :frowning:

If you are using a dialog to get the delay value, it is returned as text - not a number. Try something like this to coerce the value to a number.

set dd to (display dialog "How many seconds?" default answer "")

try  -- in case a non-numerical value is entered
	set theDelay to text returned of dd as integer
on error e -- display the error and terminate script
	return display dialog e buttons {"OK"} default button 1 with icon 0
end try

beep
delay theDelay
beep

– Rob

it worked! thanks

This is how I validate text input to an integer:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Yep, that’s the thorough way to do it. Here’s a modified version that limits the number of attempts given to the user.

property the_delay : 30

set {the_error, the_icon, attempts_} to {"", 1, 5}

repeat attempts_ times
	try
		set the_result to text returned of (display dialog the_error & "Enter the delay to use (in seconds):" default answer the_delay buttons {"Cancel", "OK"} default button 2 with icon the_icon)
		set the_delay to the_result as integer
		exit repeat
	on error e number n
		set attempts_ to (attempts_ - 1)
		if n = -128 then return --user cancelled 
		if attempts_ is not 1 then
			set {the_error, the_icon} to {"The input “" & the_result & "” is not a valid integer. Please try again. You get " & attempts_ & " more tries." & return & return, 2}
		else
			set {the_error, the_icon} to {"The input “" & the_result & "” is not a valid integer. Please try again. You get 1 last try." & return & return, 2}
		end if
		if attempts_ is 0 then return
	end try
end repeat

beep
delay the_delay
beep 2

set the_delay to 30 -- because the entered value is retained by the property on subsequent executions

– Rob