Search code examples
applescriptjavascript-automation

JXA script stops running when my macbook goes to sleep


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?


Solution

  • 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.