Starting/ending a service upon open/close of an app

I have a service that wants to run all the time and hogs memory. I only need this service to run when using a specific app. Actually I have a few apps like this. Any way, I want to start the service, then activate the app, and have the applescript idle waiting for the app to be closed. Once the app is closed the script should then proceed to unload the service freeing up memory, then quit the script.

Here is my applescript so far:

set isRunning to do shell script "ps -ef | grep Chrome | grep -v grep | wc -l | sed 's/ //g'"
if isRunning is "0" then
	do shell script "launchctl load -S Aqua <path and name of .plist>"
end if
tell application "app name" to activate

delay 10

on idle
	if running of application "app name" is false then
		do shell script "launchctl unload -S Aqua <path and name of .plist>"
		tell me to quit
	end if
end idle

Problem is it loads the service and opens the app. However, when I close the app, it should unload the service. I have tested the CLI command and it works perfect in Terminal.

I am running OS X 10.7.2. and AppleScript 2.2.1

Any help would be great. Also I have one app I want to use this script for (google chrome) and have it start the service, open the browser, and go to a specific URL.

Thanks

With Idle commands, I think you have to make the Applescript a ‘stay open’ script…

in the save as… screen, set your File Format to Application, and check the ‘stay open’ tick.

That should continue to keep the script looping the idle until it sees the ‘false’ result and executes the last command.

:lol: I totally missed that. I knew it had to be something simple. Thanks for pointing out the simple things to me. I had thought it was my code, but I couldn’t find anything wrong with it.



display dialog "billiehawkins Rocks!"
buttons {"give billie hugs!", "Buy billie a beer!"} default button 2


:smiley: Glad it helped.