Preventing sleep

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Chris
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10
Joined: Tue Mar 27, 2007 1:24 pm

Preventing sleep

Post by Chris » Fri Mar 30, 2007 11:26 am

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

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Mar 30, 2007 1:24 pm

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
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply

Return to “Talking LiveCode”