Temporarily suspend energy saver settings via script

May 23, '08 07:30:01AM

Contributed by: RickoKid

When watching videos in QuickTime Player and DVD Player, the screen saver and energy saver are automatically suspended, but not so if you're watching full-screen Flash video -- using the BBC iPlayer, for example. This AppleScript takes care of that problem:

(*
    Suspend Energy Saver © RickoKid 2008
    Version 0.1
  
    TODO: Change so that authentication only has to be given once for non-administrator users, no matter how long suspension is in place.

  
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*)

set userPass to ""
set battState to {powersource:"Battery", sleep:"", displaysleep:""}
set acState to {powersource:"AC Power", sleep:"", displaysleep:""}

if (do shell script "id -Gn") contains " admin" then
  (*  If user is admin, ask for password.  Otherwise, leave it to the system
    to ask username and password of an administrator (which will
    probably time out and ask again when ending suspension.  *)
  
  display dialog "Please type your password" default answer "" default button "OK" with hidden answer
  set userPass to text returned of the result
end if

set pmState to (do shell script "pmset -g disk") -->  Get current settings for both Battery and AC Power, and Screen Saver timeout. 
set saverState to (do shell script "defaults -currentHost read com.apple.screensaver idleTime") as number
repeat with stateSetting in paragraphs of pmState
  if stateSetting contains "Battery" then --> write following values to the battState record
    set powersource to (a reference to battState)
  else if stateSetting contains "AC Power" then --> write following values to the acState record
    set powersource to (a reference to acState)
  else if stateSetting contains " sleep" then
    set sleep of powersource to (word 2 of stateSetting)
  else if stateSetting contains " displaysleep" then
    set displaysleep of powersource to (word 2 of stateSetting)
  end if
end repeat

-->  Disable sleep, displaysleep and screen saver for both Battery and AC Power
if userPass is not "" then (do shell script "pwd" password userPass with administrator privileges) --> If we have a admin password, activate it.
set battSleepResult to (do shell script "pmset -b sleep 0" with administrator privileges and password)
set battdisplayResult to (do shell script "pmset -b displaysleep 0" with administrator privileges and password)
set acSleepResult to (do shell script "pmset -c sleep 0" with administrator privileges and password)
set acdisplayResult to (do shell script "pmset -c displaysleep 0" with administrator privileges and password)
set ssResult to (do shell script "defaults -currentHost write com.apple.screensaver idleTime 0")

display dialog ¬
  "Sleep and Screensaver are now temporarily suspended." with title ¬
  "Energy Saver Suspended" buttons {"Resume"} default button "Resume" with icon 1

-->  Re-enable sleep, displaysleep and screen saver for both Battery and AC Power, by restoring the saved settings.
if userPass is not "" then (do shell script "pwd" password userPass with administrator privileges) --> If we have a admin password, activate it.
set battSleepResult to (do shell script "pmset -b sleep " & sleep of battState with administrator privileges and password)
set battdisplayResult to (do shell script "pmset -b displaysleep " & displaysleep of battState with administrator privileges and password)
set acSleepResult to (do shell script "pmset -c sleep " & sleep of acState with administrator privileges and password)
set acdisplayResult to (do shell script "pmset -c displaysleep " & displaysleep of acState with administrator privileges and password)
set ssResult to (do shell script "defaults -currentHost write com.apple.screensaver idleTime " & saverState)
The above script will temporarily suspend the Energy Saver and screen saver until you click the Resume button. If you're logged in as an administrator, you'll need to provide your password first. (The original version of this script can be found in this post on my site.)

[robg adds: I haven't tested this one...]

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080518162911638