How to set a timer, then exit repeat of a script.

In another thread, Adayzdone showed me how to do this:

set myMinutes to 1

set timeA to current date
repeat
   say "Small Guy says Hi!"
   delay 5
   if (((current date) - timeA) / minutes) ≥ myMinutes then exit repeat
end repeat

It sets a timer, then runs a voice saying “Small Guy says Hi!” for one minute. With the end of the minute comes the end of the script.

It works fine! But for the user, me, setting it up in a hurry, deciding how long she (my sister) will be annoyed for, it’s not that good. It works, but you need to go into the script to change the time (myMinutes).

So instead of setting myMinutes to 1, I want to

set myMinutes to display dialog "How long do you wish to be annoyed for?" default answer "10" default button 2

Thus, I get to choose how long the timer is set for within the actual AppleScript (which I save as an application). All I need to do is get the result of myMinutes to tie into the script. Instead, it says “Small Guy says Hi!” once, then quits. How do I repeat the script for however long I want to? Thanks!

Hi iMacintosh,

Try this:

set myMinutes to text returned of (display dialog "How long do you wish to be annoyed for?" default answer "10" default button 2) as number

I’m curious, how old are you?

Hello!
Try inserting this where the original myMinutes is in the original script.

try
	set myMinutes to (text returned of (display dialog "How long do you wish to be annoyed for?" default answer "10" default button 2)) as number
on error
	error number -128
end try

Thanks adayzdone! It’s perfect (although when you added the delay 5 in the other thread since I Cmd+V’ed it basically without looking I thought it was broken :wink: ) It’s perfect! It goes like this:

set myMinutes to text returned of (display dialog "How long do you wish to be annoyed for?" default answer "10" default button 2) as number

set timeA to current date
repeat
	say "Small Guy says Hi!"
	if (((current date) - timeA) / minutes) ≥ myMinutes then exit repeat
end repeat

…And it goes like anything!

I have no clue why you would want to know .-. but I am 12 and proudly scripting!

EDIT: While I composed this reply, McUsr posted a reply as well, so thanks to you too!

Awesome, keep it up!

McUsr’s suggestion was better than mine because he added an error handler which will cancel the script if the user types in something that can’t be coerced into a number, such as text. error number -128 is the error produced when a user clicks the cancel button in a dialog and will stop a script from executing any further.

Another trick of ensuring the input returned from a dialog is in the right format is to enclose it in a repeat statement. The script below will convert the user’s answer into a number and then break out of the repeat loop only if no error is produced.


repeat
	try
		set myMinutes to text returned of (display dialog "How long do you wish to be annoyed for?" default answer "10" default button 2) as number
		exit repeat
	end try
end repeat