Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.5: A python script to work around a sleep issue System 10.5
After installing Leopard, my iMac would no longer auto sleep. It worked flawlessly in Tiger. I searched google and found many others having similar problems. I found many different fixes, such as removing the PowerManagement plist file, unplugging all devices, and not running certain widgets or programs. However, no matter what I did, it would not auto sleep. It would stay asleep if I manually put it to sleep.

I called Apple and was on the phone with them for about two hours before they said it is probably a hardware problem, thanks for calling. After all that hassle, I decided to see if AppleScript could put it to sleep, and it did. Once I found that out, I just wrote a python program that keeps track of how long the screen saver is running. Using that, it determines if the system has been unused for the period the user set in system preferences for machine sleep.
import sys, os, shutil, filecmp, string, datetime, time

# initalize some variables
cronPeriod = 1
sleepFile = "/Library/Preferences/SystemConfiguration/com.apple.PowerManagement.plist"
saverFile = os.path.join(os.getenv("HOME")) + '/Library/Preferences/ByHost/com.apple.screensaver.0016cb98e9e2'
trackingFile = os.path.join(os.getenv("HOME")) + '/var/sleepTracker'

f = open(sleepFile, "r")
useNextLine = 0
for line in f.readlines():
  if useNextLine == 1:
    useNextLine = 0
    timeLine = line.strip()
  if line.strip() == "<key>System Sleep Timer</key>":
    useNextLine = 1
f.close()

sleepTime = timeLine.strip('<integer>')
sleepTime = sleepTime.strip('</')
sleeepTime = int(sleepTime)

screensaverTime = os.popen("defaults read " + saverFile + " idleTime")
screensaverTime =  screensaverTime.read()

sleepTime = int(sleepTime) * 60 - int(screensaverTime)
if sleepTime < 0:
  sleepTime = 0


########################################
# Begin function definations
######################################## 
# checkFiles is the function that checks the times
# of the tracking file

def clearFile():
  if os.path.exists(trackingFile):
    os.remove(trackingFile)
  cmd = "touch " + trackingFile
  os.system(cmd)

def compareTimes():
  endLine = ""
  currentTime = time.mktime(datetime.datetime.now().timetuple())
  currentTime = str(currentTime).split('.')[0]

  f = open(trackingFile, "r")
  data = f.read()
  f.close()
  lines = data.split("n")
  lastRunning = lines[len(lines)-2]
  lastRunning = lastRunning.split('.')[0]

  if lastRunning == "":
    print "last time is: " + lastRunning
    return

  difference = int(currentTime) - int(lastRunning)
  print time.localtime()
  print "difference between current time and last time saver was found running is: ", difference
 
  cronPeriodSec = int(cronPeriod * 1.5 * 60)
  print time.localtime()
  print "cron period is: ", cronPeriodSec

  if difference > cronPeriodSec:
    print time.localtime()
    print "difference is greater - need to clear file"
    clearFile()

def wantSleep(currentTime):
  f = open(trackingFile, "r")
  data = f.read()
  f.close()
  lines = data.split("n")
  firstRunning = lines[0]
  firstRunning = firstRunning.split('.')[0]

  if firstRunning == "":
    #print "firstRunning was blank"
    return

  print "current time is: ", currentTime, " first time is: ", firstRunning
  difference = int(currentTime) - int(firstRunning)
  print time.localtime()
  print "difference between now and first time screensaver was on is: ", difference
  print "comparing against: ", int(sleepTime) 

  if difference >= sleepTime:
    print time.localtime()
    print "sleep is desired" 
    aplcmd = 'tell app "Finder" to sleep'
    cmd = "/usr/bin/osascript -e " + "'" + aplcmd + "'"
    print time.localtime()
    print cmd
    os.system(cmd)
 
##############################################
# End function definations
#############################################

#check if the screensaver is running:
cmd = "ps -e | grep ScreenSaverEngine | grep -v grep"
result = os.system(cmd)

#print "remove the following line to run for real"
#result = 234

if result != 256:
  print time.localtime()
  print "Screen Saver is running"
  compareTimes()

  currentTime = time.mktime(datetime.datetime.now().timetuple())
  currentTime = str(currentTime).split('.')[0]
  f = open(trackingFile, 'a')
  f.write(currentTime + "n")
  f.close

  wantSleep(currentTime)

else:
  print time.localtime()
  print "Screen Saver Not running"
  clearFile()

sys.exit(1)
I put this in cron to run once a minute and it has been working very reliably. Note: I didn't make it robust enough to find the screensaver preferences file. I hard-coded it to mine, so if anyone wants to try it, you would need to update the numbers at the end of the saverFile value.

[robg adds: I haven't tested this one.]
    •    
  • Currently 2.83 / 5
  You rated: 4 / 5 (6 votes cast)
 
[8,354 views]  

10.5: A python script to work around a sleep issue | 3 comments | Create New Account
Click here to return to the '10.5: A python script to work around a sleep issue' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: A python script to work around a sleep issue
Authored by: johnsawyercjs on Feb 09, '08 12:55:13PM

As the hint says, this script is hard-coded to work with the hinter's Mac, at the line containing

'/Library/Preferences/ByHost/com.apple.screensaver.0016cb98e9e2'

...where 0016cb98e9e2 is his Mac's MAC (Media Access Control) address, usually the particular Mac's Ethernet (en0) address, which is a number unique to each Mac. To make a version of this script that would work on any Mac, without having to change the MAC address in this script manually, you'd need to use the "-currentHost" command (variable?), which finds your Mac's MAC address for you. But as to how to work that into this script, I don't know, though the man page for "defaults" may help.



[ Reply to This | # ]
10.5: A python script to work around a sleep issue
Authored by: jatilios on Mar 10, '08 12:28:49PM

Is there any way that this script be used, who can I say, by non programmer user? As myself... I´m having the same issue, my MBP can´t auto sleep...unless I close the lid or if I manually sleep it by using the Apple Menu Sleep comand....
I´m intertest in trying your tip...but really don´t understand how to... I´m a photographer, and really don´t know how scripts or codes work
thanx

Jatilios

Macbook Pro C2D 2.16 2GB MacOSX 10.4.11



[ Reply to This | # ]
10.5: A python script to work around a sleep issue
Authored by: johnsawyercjs on Jan 29, '09 05:51:51AM

There appears to be a spelling error in the program, at the line:

sleeepTime = int(sleepTime)

I think the variable needs to retain the name "sleepTime" that it had immediately above that line, since there are no other references to "sleeepTime" in the rest of the program.



[ Reply to This | # ]