Repeat times minutes

Hello,
This is my applescript

on run {input, parameters}
	delay 1.0
	tell application "System Events"
		repeat 100 times
			keystroke "A"
			delay 0.4
			keystroke "R"
			delay 0.4
			keystroke "B"
			delay 0.4
			key code 53
			delay 0.4
			keystroke "B"
			delay 0.4
			key code 53
			delay 0.4
		end repeat
	end tell
end run

I would like to replace the number of 100 times with 5 minutes

I don’t want the number of clicks to be determined by quantity but by duration. Is it possible ?

How about this? It’s not 100% precise, though.

set stopDate to (current date) + 300
tell application "System Events"
	repeat until ((current date) ≥ stopDate)
		keystroke "A"
		delay 0.4
		keystroke "R"
		delay 0.4
		keystroke "B"
		delay 0.4
		key code 53
		delay 0.4
		keystroke "B"
		delay 0.4
		key code 53
		delay 0.4
	end repeat
end tell

Thank you kmitko but (current date) + 300 What does that correspond to? 300 seconds?

(current date) + 5 * minutes

is the same as

(current date) +300

That’s 300 hundred seconds.

1 Like

If you are able to save the script as stay open or as application you can use the on idle handler

on run {input, parameters}
	delay 1.0
end run

on idle
	tell application "System Events"
		keystroke "A"
		delay 0.4
		keystroke "R"
		delay 0.4
		keystroke "B"
		delay 0.4
		key code 53
		delay 0.4
		keystroke "B"
		delay 0.4
		key code 53
		delay 0.4
	end tell
	
	return 5 * minutes
end idle

That’s to say ? Sorry, I don’t speak English very well, and I didn’t really understand your answer.

wow. so all these years I didn’t have to calculate seconds - but could just use * minutes (or * hours). why why didn’t I know about it.

and…

10 * days
10 * weeks
1 Like

Your repeat loop contains a series of delay commands, which are pretty much doing exactly what you need—that is to say, they are literally monitoring the amount of time that elapses between the start and end of the command call. We can safely assume that each call to the key code command takes approximately zero seconds, which I imagine is the sole reason you needed the delay command calls to separate them.

There are six calls to delay in each iteration of your repeat loop, each taking 0.4 seconds of time. Therefore, one single iteration takes around 6 × 0.4 = 2.4 seconds.

You want this repeat loop to continue for a total of 300 seconds. This equates to 300 ÷ 2.4 = 125 iterations. Therefore:

tell application "System Events" to repeat 125 times
		keystroke "A"
		delay 0.4
		keystroke "R"
		delay 0.4
		keystroke "B"
		delay 0.4
		key code 53
		delay 0.4
		keystroke "B"
		delay 0.4
		key code 53
		delay 0.4
end repeat

will run for 5 minutes without expending additional time performing extraneous operations or making additional calls, e.g. to current date.