Launch countdown after starting Apple script App

I want to set up a script which checks periodically (half hours) if:
A- a living network exists
B- some applications run

The problem is, I need arbitrary half hours, (independent from our clock time)
because the timer should start only if both conditions A and B, are satisfied. Launchd isn’t able to deal with arbitrary half hours and needs a plist to be loaded at startup.

A stay open idle handler works mostly but isn’t reliable in that case, as I tried this already many times. Therefore my request to replace on idle with a better counter

Seems that you’re looking for a bash script that sleeps for an half an hour and startup an applescript using osascript. Then you can use launchd to make sure the bash script is kept alive and make it an agent.

Yes… Just I’m not able to figure out how to write such a bash script … sleep 1800? And where to place that script? Hmm…

[format]
while sleep $(osascript path/to/script.scpt)
do

You can do some other stuff here

done
[/format]

In applescript you return the value to sleep, just like a return in an idle handler. The return value of the (implicit) run handler is the output of osascript and thus the number of seconds to sleep. If the conditions A and B not met you return a low value so the script is invoked in a short time again (like 10 seconds or so) if conditions A and B are true you return.

Something like this:

on run argv
	if internetIsReachable() and processIsRunning() then
		return "1800" -- wait half an hour
	else
		return "20" -- wait 20 seconds
	end if
end run

Or you can use another while loop that checks the internet connection using ping and check the process using ps and grep command in bash.

You can load the bash script into launchd using launchctl command (or reboot your system). Make sure that you use the key KeepAlive in the launchd.plist file so if for whatever reason the bash script quits, launchd will restart the script again. You have to store this plist file in the LaunchAgents folder of your library. The contents of such a file could be:

That’s great, :cool:
I was thinking if we could fetch the network uptime things would be less generic than with half hours - and the script should start only when one of my Applescript.app launches… not earlier. Launchd runs all the time even when conditions don’t exist at all. Don’t get me wrong I appreciate your suggestions very much :slight_smile:

It’s not the actual uptime but the modification date of /var/run/resolv.conf could tell you how long you’re running this network configuration. For example when you connect to a VPN server, this file will be updated too but you go from one online configuration to another.

This example will set var networkUptime to the network uptime in seconds or missing value if there is no internet connection.

try
	tell application "System Events"
		set networkUptime to modification date of disk item "/var/run/resolv.conf"
	end tell
	set networkUptime to (current date) - networkUptime
on error
	-- no internet connection
	set networkUptime to missing value
end try

Isn’t it easier to start the script by your “Applescript.app” then? You could also share a file, add a WatchPath to it and touch it with your “Applescript.app” app to make launchd start the script.

I know :cool: