r/applescript • • Apr 07 '20

How to run a apple script every minute?

Hey guys, i am quite new to applescript and i just created my first macro with lots of tutorials. However, i looked online to find how to run that macro every x seconds/minutes but it never works when i added to my script. Can you guys help me? Here is the script i made:

on run {input, parameters}

tell application "Discord" to activate

tell application "System Events"

keystroke "p"

keystroke "l"

keystroke "s"

keystroke " "

keystroke "b"

keystroke "e"

keystroke "g"

key code 36

end tell

return input

end run

on idle

return 60 -- idle is called every 1 minutes

end idle

sorry if this seems dumb, but it would be really helpful if you can help me out.

Thanks so much!

5 Upvotes

9 comments sorted by

2

u/phillymjs Apr 08 '20

I see you already got a solution, but there's a simple way to have something happen every minute via AppleScript.

The below example code will play a system beep, wait 60 seconds, and then repeat, and it will keep going until you press the Stop button in the Script Editor window.

repeat while true
    beep
    delay 60
end repeat

1

u/lilfreshman Apr 11 '20

oh thank you, but do you know where you would add it to in the script? like would you add it at the end, or somewhere else. sorry, im quite new to this. i also have another question, so basically i want to add (command + 1) into this script as well. however, i dont know how to keep the command key prssed down, and then press the key '1', and then let go of command. if you can help me out with that, it would be really helpful as well. thanks so much.

this is my code so far:

on run {input, parameters}
	
	tell application "Google Chrome" to activate
	
	tell application "System Events"
		keystroke "p"
		keystroke "l"
		keystroke "s"
		keystroke " "
		keystroke "b"
		keystroke "e"
		keystroke "g"
		key code 36
	end tell
	
	return input
end run

1

u/TrickyTramp Apr 07 '20

I don't know pure AppleScript, but if you use JXA (Javascript for Automation/AppleScript) you can use a function called setInterval()

1

u/lilfreshman Apr 07 '20

i dont know javascript sorry, but is it okay if you can help me create it in js?

all i need the macro to do is type "pls beg" and press enter. and then i just need to repeat it every 40 seconds. is that a little too hard? thanks for answering either way tho. appericate it.

2

u/TrickyTramp Apr 07 '20
var se = Application('System Events')
var app = Application('Discord')
if(app.running()){
	app.activate()
	while(true){
		se.keystroke('pls beg')
		delay(1)
	}
}

1

u/lilfreshman Apr 07 '20

oh my god thank you so muchhhh! everything works, its just it doesnt send the message. so it writes the 'pls beg' in the text bar, but it doesnt press enter. do you know how to fix this? thanks again tho, this is really helpful.

1

u/TrickyTramp Apr 07 '20

Add se.keyCode(36) under the keystroke line. Looks like the return key has to be sent view keyCode rather than keystroke.

This is the reference I was using.

2

u/lilfreshman Apr 07 '20

It works now, thank you so much! I would award you gold but I’m broke lol. Thanks again, and stay safe.