Checking for process and closing if it exists?
A current part of my code is:
property thePID : missing value
on disableSleep_(sender)
set thePID to (do shell script"caffeinate &> /dev/null & echo $!")
end disableSleep_
on enableSleep_(sender)
if thePID is missing value then return -- no process
do shell script "kill $(pgrep caffeinate) " & thePID
set thePID to missing value -- reset for next time
end enableSleep_
At the end of the program I have:
on applicationShouldTerminateAfterLastWindowClosed_(sender)
returntrue
end applicationShouldTerminateAfterLastWindowClosed_
on applicationShouldTerminate_(sender)
return current application's NSTerminateNow
end applicationShouldTerminate_
And I would like it to check to see if the process "caffeinate" exists, and if it does, I would like it to kill all "caffeinate" processes. Any ideas?
MacBook Pro, OS X Mountain Lion (10.8.2), 2.3 GHz i5, 4GB RAM, OCZ 120GB SSD