I have written a small applescript that I would like to run automatically every 2 hours. I have thus written the following launchd daemon (probably it is an agent):
<?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>Disabled</key> <false/> <key>Label</key> <string>com.zerowidth.launched.aspect</string> <key>Program</key> <string>/usr/bin/osascript</string> <key>ProgramArguments</key> <array> <string>osascript</string <string>/Users/***/my_script.scpt</string> </array> <key>StandardOutPath</key> <string>/Users/***/file_log.log</string> <key>RunAtLoad</key> <true/> <key>ThrottleInterval</key> <integer>7200</integer> <key>KeepAlive</key> <true/> </dict> </plist>
Then I have opened terminal and done
launchctl load ~/Library/LaunchAgents/com.zerowidth.launched.aspect.plist
It worked fine, but only once and not every two hours. Checking on Console I got the following messages (and many more actually):
com.apple.xpc.launchd (com.zerowidth.launched.aspect): This service is defined to be constantly running and is inherently inefficient.
Dec 16 17:02:25 Air-of-myself-2 com.apple.xpc.launchd (com.zerowidth.launched.aspect): Service only ran for 7 seconds. Pushing respawn out by 7193 seconds.
Can you help me in understanding what is going on please? How can I run the apple script every two hours?
EDIT. I add, following Robert's advice, the Python script and the Applescript.
Here is Python script. It asks the console to run the LocateMe command and from this it obtains the latitude and longitude of the place where I am. Then calls an API to obtain sunset and sunrise time of this place. Finally a small function decides whether it is day (sunrise < now
output = os.popen('/Users/***/Desktop/LocateMe').read() ###I extract my coordinates coord = output[1:25] lat = float(coord[1:12]) ###latitude long = float(coord[13:25]) ### longitude my_sunrise, my_sunset = get_ris_and_set(lat, long) ##this function calls an API that gives sunset and sunrise time in the position given by (lat,long) my_sunset1 = my_sunset.time() my_sunrise1 = my_sunrise.time() result = str(is_day(now.time(), my_sunrise1, my_sunset1)) ### compare the time now and returns true if it is day false otherwise with open('/Users/***/Desktop/log_file.txt', 'a') as f: print(result, file=f) sys.exit()
Here is the Applescript: it runs the Python script above and reads the boolean value (if it is day or not). Then it checks if the dark mode on Mojave is on and it switches if needed.
tell application id "com.apple.systemevents" tell application "Terminal" do shell script "/usr/local/bin/python3 /Users/***/Desktop/python.py $@" end tell tell appearance preferences set value to do shell script "tail -n 1 /Users/***/Desktop/log_file.txt" if dark mode is true and value = "True" then set dark mode to false else if dark mode is false and value = "False" then set dark mode to true else return end if end tell end tell