Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Posted on Feb 4, 2013 8:36 AM

Reply
Question marked as Best reply

Posted on Feb 4, 2013 9:01 AM

Using killall with the process name will kill all of those processes - the shell script will just return an error message if the process name doesn't exist:


do shell script"killall caffeinate"


for example (tested):


on disableSleep_(sender)

if thePID is missing value then -- not running yet

set thePID to (do shell script"caffeinate &> /dev/null & echo $!")

end if

end disableSleep_


on enableSleep_(sender)

try-- skip error if process name doesn't exist

do shell script "killall caffeinate"

end try

set thePID to missing value -- reset for next time

end enableSleep_

2 replies
Question marked as Best reply

Feb 4, 2013 9:01 AM in response to MacMan240

Using killall with the process name will kill all of those processes - the shell script will just return an error message if the process name doesn't exist:


do shell script"killall caffeinate"


for example (tested):


on disableSleep_(sender)

if thePID is missing value then -- not running yet

set thePID to (do shell script"caffeinate &> /dev/null & echo $!")

end if

end disableSleep_


on enableSleep_(sender)

try-- skip error if process name doesn't exist

do shell script "killall caffeinate"

end try

set thePID to missing value -- reset for next time

end enableSleep_

Feb 4, 2013 10:45 AM in response to MacMan240

a slightly different (and somewhat easier) approach to the problem. Use a launchd plist that you runs caffeinate, then load and unload that plist as needed. save the following property list as a plain-text file at ~/Library/LaunchAgents/user.nodoze.plist":


<?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>Disabled</key>

<true/>

<key>RunAtLoad</key>

<true/>

<key>Label</key>

<string>user.nodoze</string>

<key>Program</key>

<string>/usr/bin/caffeinate</string>

</dict>

</plist>


Then run the following applescript to start and stop it:


if (do shell script "launchctl list") contains "user.nodoze" then

set r to "enabled."

else

set r to "disabled."

end if


set b to button returned of (display alert "Caffeinate is currently " & r buttons {"Never Mind", "Enable", "Disable"})


if b is "Enable" then

do shell script "launchctl load -F ~/Library/LaunchAgents/user.nodoze.plist"

else if b is "Disable" then

do shell script "launchctl unload ~/Library/LaunchAgents/user.nodoze.plist"

end if


the Disabled key is set to true to keep the plist from loading when you log in; the -F option overrides the Disabled key to load the job on demand.

Checking for process and closing if it exists?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.