Prevent Sleep Till Script Finishs

Hi,

is there a way to prevent system sleep while an applescript is running?
I have a long operation that takes about 3 hours but only has some work
to do every 60 minutes.

Thanks for answers…

Regards

Michael

Hi,

A Google search found this on - http://hints.macworld.com/

(*
    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)

You could add your code between the ‘disable’ section and the ‘Re-enable’ section. Or just take the relevant shell script commands to disable power saving, add them to the beginning of your script then add the re-enable shell commands after your script has finished.

Hopefully yo will be able to use something from this!

This is great thanks a lot! :slight_smile:
I will try it soon.

I like Caffeine for Mac, a small, free app that puts a coffee cup in your menu bar. Clicking on that will prevent sleep. The good news, however, is that it’s scriptable with just two commands in it’s dictionary: TURN ON and TURN OFF. By holding down Command while clicking the icon you get access to the very simple prefs where you can set whether you want it to start at login and the duration after which it will automatically revert to off.

Is there any difference between using the script and using Caffeine?

Certainly not in the result, but I like the Caffeine solution because I use a Keyboard Maestro triggered script to turn Caffeine on and then click on the icon when I’m done and want it back off. I haven’t changed any system settings because Caffeine keeps the screen alive and the system awake by wiggling the cursor one pixel.