sunnuntai 27. heinäkuuta 2014

Sleep enforcer

I use a script to monitor my computer's sleep state. This derives from a previous situation when my Mac used to randomly wake up by itself to "check the network status" and apparently it was a feature which was connected to a checkbox "Wake for network access".
Since I do use my computer remotely on a regular basis I needed to keep this checkbox checked and suffer the consequences of having OSX "checking the network" around 20-40 times a day. But that wasn't the whole case. Sometimes after "checking the network" OSX failed to go back to sleep and stayed up the whole night for me to realize that in the morning. That's when I decided to write a program which repeats forever and forces OSX to go back to sleep. And it kept a log on the wakes in a simple text file in cloud, which I could access also with my phone. Of course you could see wake times and causes with terminal command: syslog | grep Wake , but I only wanted to see the wake time and having it available in the cloud.

But then I found this post (http://sysadminblogger.wordpress.com/2013/12/02/os-x-mavericks-wake-for-network-access-with-no-random-wake-ups/) which helped me to remove the "insomnia" feature, so I'm not sure what's the purpose of my sleep program anymore. I still use it, basically cause I still don't trust OSX not to wake up by itself. But after I did as the post guided, there has been no random wake ups.

Here's a cleaned up version of my "sleep enforcer" application:

set wakelog to (path to desktop as text) & "wakelog.txt"
set fileCreated to false
repeat
tell application "Finder" to sleep
display dialog "Sleep activated. This dialog box dissappears in 20 seconds" with title "1/3" buttons "Cancel" giving up after 20
tell current application to activate
display dialog "Sleep activated. I will write log after 30 seconds." with title "2/3" buttons "Cancel" giving up after 30
repeat
try
set readFile to read (POSIX path of wakelog)
exit repeat
on error
writeLog(false, "", wakelog)
set fileCreated to true
end try
end repeat
writeLog(true, readFile, wakelog)
set readFile to ""
tell current application to activate
display dialog "Sleep activated. I will start sleep app after 2 minutes." with title "3/3" buttons "Cancel" giving up after 120
end repeat

on writeLog(x, getReadFile, getWakelog)
if x is true then
set thisDay to do shell script "date '+%Y%m%d'"
set thisDay to thisDay - 20000000 as string
set thisTime to do shell script "/bin/date +%H%M"
set thisDate to thisDay & space & thisTime & return
else
set thisDate to ""
end if
set myLogFile to open for access POSIX file (POSIX path of getWakelog) with write permission
write thisDay & space & thisTime & return & getReadFile to myLogFile
close access myLogFile
end writeLog

Ei kommentteja:

Lähetä kommentti