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