0

I am running a JXA script as an agent using launchctl. The main logic of the script is supposed to be run at intervals of 2 seconds, which I have achieved using an infinite loop and delay. However, whenever my macbook goes to sleep the script stops execution and I have to unload and load the agent manually again.

My script:

for(;;) {
   // (Open browser and check whether a tab exisits or not)
   <APPLICATION LOGIC > 
   delay(2);
}

Can I do something to ensure that this script keeps on running even after my macbook wakes up?

Shubham
  • 352
  • 3
  • 14
  • 1
    Throw away that script. You don't want it running even when your MacBook is awake, let alone when it's trying to sleep. **DON'T** run stuff on an infinite loop. Either use an AppleScript _stay open_ application with an **`on idle`** handler or, better yet, use `launchd` to run the script, which will deal with recurring the execution of the script properly. (* *smh* *) – CJK Nov 24 '20 at 16:03
  • 1
    Thanks. Have removed the infinite loop and now I am managing the execution of the script using `launchctl` as recommended. If someone wants to learn how to execute a script every N seconds using `launchd` please refer to the following article: [Run a script every 5 mins on a Mac](https://www.splinter.com.au/using-launchd-to-run-a-script-every-5-mins-on/) – Shubham Nov 25 '20 at 04:27

1 Answers1

0

Based on the suggestion by CJK, I am now executing my JXA script, every 5 seconds, using launchctl and the following plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>google-meet.job</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>/Users/porter/Library/Script Libraries/Brave - Google Meet Running.scpt</string>
    </array>
    <key>StartInterval</key>
    <integer>5</integer>
</dict>
</plist>

You can control the frequency of execution of your script using the StartInterval key.

Shubham
  • 352
  • 3
  • 14