2 Replies Latest reply: Feb 4, 2013 10:45 AM by twtwtw
MacMan240 Level 1 Level 1 (80 points)

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)

        return true

    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
  • 1. Re: Checking for process and closing if it exists?
    red_menace Level 6 Level 6 (14,615 points)

    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. Re: Checking for process and closing if it exists?
    twtwtw Level 5 Level 5 (4,690 points)

    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.