Page 1 of 1

Preventing sleep

Posted: Fri Mar 30, 2007 11:26 am
by Chris
Hello -

does anyone know of a good way to prevent a machine (os x) going to sleep? I have written a simple photo/video display program to show my snapshots, but I'm becoming increasingly bothered about killing the planet. I want to set my mac mini to sleep after a few minutes of inactivity - but *not* if my slideshow is running.

So I need a way to prevent the comp going zzzzzzz.

(Of course in real-life, the simplest way to prevent sleep is to have kids...)


Chris

Posted: Fri Mar 30, 2007 1:24 pm
by Mark
Hi Chris,

Here are three scripts for Mac OS X.

Code: Select all

function sleepTime
  return shell("pmset -g | grep displaysleep | awk '{ print $2; }'")
end sleepTime

on setSleepTime theMins
  ask "Enter your password"
  if it is not empty then
    put it into myPW
    put "#!/bin/sh" & cr into myScript
    put "pw=" & quote & myPW & quote & cr after myScript
    put "echo $pw | sudo pmset dim " & theMins & cr after myScript
    put myscript
    put shell(myScript) into rslt  -- do the command & get the result
    if rslt is not empty then
      beep
      answer error "Sorry, a problem occurred." & cr & cr & rslt with "Okay"
    end if
  end if
end setSleepTime

on setSleepTimeAS theMins
  put "sudo pmset dim " & theMins into myShell
  put "try" & cr & ¬
  "do shell script" && quote & myShell & quote && "with administrator privileges" & cr & ¬
  "on error myErr number myErrNr" & cr & ¬
  "set myErrMsg to myErr & ' (' & myErrNr & ')'" & cr & ¬
  "end try" into myScript
  replace "'" with quote in myScript
  do myScript as AppleScript
  put the result into rslt
  if rslt is not empty and rslt is not (quote & quote) and "(-128)" is not in rslt then
    beep
    answer error "Sorry, a problem occurred." & cr & cr & rslt with "Okay"
  end if
end setSleepTimeAS
(mind line wraps).

The sleepTime function returns the current delay until the monitor is put to sleep. The other two handlers set this delay.

The setSleepTime handler allows you to enter a password, either by script or manually, without intervention of the OS. Replace the ask dialog with your own dialog or include the password in your script.

If you want others to use your application, you probably want to display the native password dialog of Mac OS X rather than a custom dialog. Use the sleepTimeAS handler, which uses AppleScript, to achieve this.

Best,

Mark