Mac AppleScript Repeat For x Seconds

jcs801

macrumors newbie
Original poster
Feb 19, 2013
9
0
I need AppleScript to repeat for a certain number of seconds. I was thinking "repeat for x seconds" but that doesn't work. Notice: This is not delay, I want the repeat to stop after x seconds.
 

jcs801

macrumors newbie
Original poster
Feb 19, 2013
9
0
Oh, and also, a command that repeats the script until "esc" is pressed
 

ytk

macrumors regular
Jul 8, 2010
225
7
You just want it to run a loop as fast as possible for a specific duration?

Code:
set start_value to do shell script "ruby -e 'print Time.now.to_f'"
repeat while true
	-- Your code goes here
	set end_value to do shell script "ruby -e 'print Time.now.to_f'"
	if end_value - start_value ≥ 1 then
		exit repeat
	end if
end repeat
The above code will run through a loop for one second. Change the value in the if statement to adjust the amount of time. Note that you can also specify partial seconds, such as 2.5.

Can't help you with your other problem.
 

jcs801

macrumors newbie
Original poster
Feb 19, 2013
9
0
You just want it to run a loop as fast as possible for a specific duration?

Code:
set start_value to do shell script "ruby -e 'print Time.now.to_f'"
repeat while true
	-- Your code goes here
	set end_value to do shell script "ruby -e 'print Time.now.to_f'"
	if end_value - start_value ≥ 1 then
		exit repeat
	end if
end repeat
The above code will run through a loop for one second. Change the value in the if statement to adjust the amount of time. Note that you can also specify partial seconds, such as 2.5.

Can't help you with your other problem.


Where you put where my code goes, I can put anything, even delays?

Thanks for helping!
jcs801
 

jcs801

macrumors newbie
Original poster
Feb 19, 2013
9
0
You just want it to run a loop as fast as possible for a specific duration?

Code:
set start_value to do shell script "ruby -e 'print Time.now.to_f'"
repeat while true
	-- Your code goes here
	set end_value to do shell script "ruby -e 'print Time.now.to_f'"
	if end_value - start_value ≥ 1 then
		exit repeat
	end if
end repeat
The above code will run through a loop for one second. Change the value in the if statement to adjust the amount of time. Note that you can also specify partial seconds, such as 2.5.

Can't help you with your other problem.
Woah dude, this is amazing. Thanks! I will vote up on your post.

Btw, if you would guess my age between 10 and 30, how old would you think I am?
 

mperlman

macrumors newbie
Jan 15, 2020
1
0
You just want it to run a loop as fast as possible for a specific duration?

Code:
set start_value to do shell script "ruby -e 'print Time.now.to_f'"
repeat while true
    -- Your code goes here
    set end_value to do shell script "ruby -e 'print Time.now.to_f'"
    if end_value - start_value ≥ 1 then
        exit repeat
    end if
end repeat
The above code will run through a loop for one second. Change the value in the if statement to adjust the amount of time. Note that you can also specify partial seconds, such as 2.5.

Can't help you with your other problem.

ytk When I run your code without adding anything it runs as expected, runs for 1 second then stops. But when I add my code it stops after running my code once (below). It returns 7 then it ends, instead of continously running for 10 seconds. Is there a way to run the code continously for the time listed in the code?

set start_value to do shell script "ruby -e 'print Time.now.to_f'"


repeat while true


set end_value to do shell script "ruby -e 'print Time.now.to_f'"


return "7"


if end_value - start_value ≥ 10 then


exit repeat


end if


end
repeat
 

ytk

macrumors regular
Jul 8, 2010
225
7
ytk When I run your code without adding anything it runs as expected, runs for 1 second then stops. But when I add my code it stops after running my code once (below). It returns 7 then it ends, instead of continously running for 10 seconds. Is there a way to run the code continously for the time listed in the code?

set start_value to do shell script "ruby -e 'print Time.now.to_f'"


repeat while true


set end_value to do shell script "ruby -e 'print Time.now.to_f'"


return "7"


if end_value - start_value ≥ 10 then


exit repeat


end if


end
repeat
I'm honestly not sure what you are trying to accomplish here. A return statement will immediately end any loop it is in, and return the value specified to the calling function. I don't even know what it would mean to repeatedly return a value; returning a value is not something that you can do more than once within a given context.

Maybe if you explain what you're trying to actually do, I can help you figure out how to go about it.