May 12

I’m looking for a way to identify if an application on my Mac stopped. I couldn’t find anything, so it was time to write my first AppleScript:

set network_connect_is_running to is_running("Network Connect")

if network_connect_is_running then
	repeat until network_connect_is_running is false
		delay 10 -- wait some seconds
		set network_connect_is_running to is_running("Network Connect")
	end repeat
	-- application stopped, so run a dummy app so that marcopolo can switch context
	tell application "NetworkConnectStopped" to activate
end if

on is_running(appName)
	tell application "System Events" to (name of processes) contains appName
end is_running

The code is quiet simple. Line 12-14 defines a function which checks if application appName is running. Line 01 calls this function and checks if the application “Network Connect” is running. The result is stored in the variable network_connect_is_running. Only if “Network Connect” is running (line 03) I want to start my listener. The listener (loop line 04-07) checks every 10 seconds if the application is still running. If not, the loop ends and I can start my trigger application “NetworkConnectStopped”.

So what is this all for?

I use MarcoPolo to switch my network connections. I use three contexts:
- home
- homeOffice (VPN)
- work
The contexts differ in things like proxy, printer, mounts.

The problem: MarcoPolo has no rule “Not Running Applications”

When I’m at home and start my VPN (Network Connect) I use this application to switch automatically to context “homeOffice”. When I finished my home office session and quit “Network Connect” I want to switch automatically to context “home” again. This can not be done with MarcoPolo so far. Now the above script comes into play. I stored this script as an application and start this listener when switching to context “homeOffice”. When I quit “Network Connect”, my listener recognizes this and starts the application “NetworkConnectStopped”. This application is created with the Automator and contains nothing but a “Pause” of 6 seconds. One more than the default MarcoPolo rule update interval. The last step is to add a rule “Running Application” to MarcoPolo which switches to context “home” when application “NetworkConnectStopped” is running.

Here are my MarcoPolo rules:

And here are my actions:

If you have suggestions or need more details feel free to comment.

Leave a Reply

preload preload preload