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

enable/disable screen saver/computer sleep?

In a cocoa-application I am making, I would like to create buttons to enable and disable the computer from going to sleep. The following code is not working:


on disableSleep_(applescript)

do shell script"caffeinate &> /dev/null & echo $!"

end disableSleep_

on enableSleep_(applescript)

do shell script"kill -INT " & thePID

end enableSleep_


any suggestions?

MacBook Pro, OS X Mountain Lion (10.8.2), 2.3 GHz i5, 4GB RAM, OCZ 120GB SSD

Posted on Feb 3, 2013 2:45 PM

Reply
9 replies

Feb 3, 2013 2:57 PM in response to MacMan240

You need to describe these things in excruciating detail - what is "not working"? Since this seemed to be working in earlier topics, I am going to make a guess that it has to do with your handler declaration - since you declared that your handler takes a parameter (applescript), if you are not passing something to put into that parameter you will get an error. If these handlers are button actions, make sure they are connected to the UI objects (the sender will be passed to the action).

Feb 3, 2013 3:32 PM in response to MacMan240

OK, now I remember - when starting the caffeinate utility, it returns the process identifier, which you don't appear to be keeping anywhere. You should have a property with the rest of your property declarations for the process ID, for example:


propertythePID : missing value

...then, in your handlers, update the property as needed, for example:


ondisableSleep_(sender)


setthePIDto (do shell script"caffeinate &> /dev/null & echo $!")

enddisableSleep_


onenableSleep_(sender)


ifthePIDismissing valuethenreturn-- no process


do shell script"kill -INT " & thePID


setthePIDtomissing value-- reset for next time

endenableSleep_

Feb 3, 2013 3:36 PM in response to MacMan240

Your text indicates a Cocoa application, but your code looks like AppleScript.


If you are using Objective C and Cocoa here, then see this discussion for some details of using IOKit. More direct access to the (temporary) settings.


The shell script would usually be a direct call to caffeinate, with whatever command you want to "protect" specified as an argument. That'll avoid having to pop up a dialog box, or whatever else. It'll just work. Or yes, aim the kill at the specified PID.


If you want to disable or enable this stuff somewhat more "permanently", then see the pmset command. That'll do directly what you seem to be doing here, and without messing around with caffienate.

Feb 3, 2013 7:07 PM in response to MacMan240

It looks like System Events doesn't see the process name, probably because it is running in another shell.


Try removing the options from the kill command - I don't see anything that is needed (not sure if the command is getting mixed up with the built-in), and it works without any options in my test project. Also, you should add a test to see if the process has been started, otherwise you will get multiple instances:


on disableSleep_(sender)

if thePID is missing value then -- not started yet

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

end if

end disableSleep_

on enableSleep_(sender)

if thePID is missing value then return -- no process

do shell script "kill " & thePID

set thePID to missing value -- reset for next time

end enableSleep_

enable/disable screen saver/computer sleep?

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