Sunday, November 19, 2017

#1 2017-08-24 06:44:31 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 521
Website

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

Offline

 

#2 2017-08-24 07:37:05 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Launch countdown after starting Apple script App

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.

Offline

 

#3 2017-08-24 08:05:06 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 521
Website

Re: Launch countdown after starting Apple script App

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

Offline

 

#4 2017-08-24 09:19:09 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Launch countdown after starting Apple script App

while sleep $(osascript path/to/script.scpt)
do
   # You can do some other stuff here
done

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:

Applescript:

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:

<?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>com.company.ascontroller</string>
        <key>Program</key>
        <string>/Users/Joy/bin/aslauncher.sh</string>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
    </dict>
</plist>

Last edited by DJ Bazzie Wazzie (2017-08-24 10:04:53 am)

Offline

 

#5 2017-08-24 11:55:51 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 521
Website

Re: Launch countdown after starting Apple script App

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 smile

Last edited by Joy (2017-08-24 12:40:02 pm)

Offline

 

#6 2017-08-25 06:48:32 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Launch countdown after starting Apple script App

Joy wrote:

I was thinking if we could fetch the network uptime things would be less generic than with half hours



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.

Applescript:

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

Joy wrote:

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.



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.

Joy wrote:

Don't get me wrong I appreciate your suggestions very much



I know cool

Last edited by DJ Bazzie Wazzie (2017-08-25 06:48:51 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)