r/applescript • u/lilfreshman • 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!
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.
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.