PDA

View Full Version : wake osX from sleep using applescript?


MasterSwitch
05-23-2003, 03:16 PM
hi all

i have my own alarm clock ive made using applescript that used to work fine in os9. in osX i have needed to tweek it a bit but one of the major problems i have is the script is not refreshing the radio or anouncing the current time every 2mins as it should. however when i wake the computer up by moving the mouse, my script works fine.
it seems like the operating system only pages applications every 20mins so a 2min timmer is useless. however if there is a simple apple script command to wake the screen up and keep it awake, my script will work again.
anyone got any ideas..?

cheers clive

gatorparrots
05-23-2003, 05:01 PM
iBeeZz
http://ibeezz.reboute.net/en/

hayne
05-23-2003, 05:26 PM
the script is not refreshing the radio or anouncing the current time every 2mins as it should. however when i wake the computer up by moving the mouse, my script works fine. Please clarify what the problem is. You say "when I wake the computer up" - well if the computer is asleep (not just the screen, the whole computer), then none of your programs (including your script) is being executed, so what you describe is quite normal. But if you mean merely that the screen has gone black but the Mac is not asleep, then your script ought to work fine.
How are you doing the timing (to get the 2 minute intervals)?

mervTormel
05-23-2003, 05:40 PM
there's display sleep, then deep sleep.

if the processor is deep sleeped, nothing soft is executing. only keyboard/mouse/NIC-womp input can wake the rig, me thinks.

MasterSwitch
05-23-2003, 05:53 PM
i use the energy saver to put the computer to sleep, so it puts the screen to sleep, then about 5mins after the computer, and of course ive ticked the tab to put the hard disk to sleep when possible.
im well awear that using a sleep command is fatal. either using action script or the power key will kick me off the net.
so i dont know what level of sleep its in but it should be fairly deep..:D
and yes the actionscript should work since it works fine in os9.
my 2min counter does this

repeat 2 times
delay 50 -- ok so its not 60 but close
end repeat

hayne
05-23-2003, 06:00 PM
So you seem to be saying that you have an AppleScript which, in OS 9, continues to run even though the computer is asleep. This is indeed strange. Please confirm.

gatorparrots
05-23-2003, 06:01 PM
To prove merv right, both of these approaches do not work in OS X:
AppleScript:
tell application "Finder" to sleep
do shell script "/bin/sleep 120"
tell application "Finder" to activate
Terminal equivalent:
sudo /usr/bin/osascript -e 'tell application "Finder" to sleep' && /bin/sleep 30 && sudo /usr/bin/osascript -e 'tell application "Finder" to activate'
(with or without sudo/administrator privileges)

Buy BeeZz. It will do what you want until Apple *hopefully* incorporates that functionality in 10.3.

MasterSwitch
05-23-2003, 06:01 PM
gatorparrots

thx for the link BUT it wont be as flexible as the script ive written, i can enter the number of hours and mins in my script, my script can also run other scripts, my script will slowly turn the radio up and tell me the current day and time...
so i cant replace that item with mine, and if i use it to run my script, i then wont have a flexible wake up time.... i acutally choose the number of hours im going to sleep so could be 5 could be 7 or whatever.... so i dont have to get up at 7am every morning, i just set my timmer for a regular 7 hours worth of sleep and wake up when my alarm clock wakes me.


mervTormel

um, yes and no. there are a number of alarm clocks out there on the net similar to the one "gatorparrots" linked me to. so if others know how to wake the computer up, it must be possible! even if its a unix script and not an apple script.

MasterSwitch
05-23-2003, 06:10 PM
hayne
So you seem to be saying that you have an AppleScript which, in OS 9, continues to run even though the computer is asleep. This is indeed strange. Please confirm. [/QUOTE]

its been 2months since i ran my script in os9 and its been probabaly a year since i had to do anything to that script in that os. but yes, it worked fine. i dont remember if i put a sleep command in the apple script to put my mac to sleep or if i let the energy saver put it to sleep but i think it used the sleep command. however i did in os9 use macamp not itunes since itunes stops when the comp goes to sleep, macamp doesnt.
i know this sounds like a stange concept to everyone but i dont put my computer to sleep as such, i have scripts, energy savers and a radio to do it for me. so i get music for 20mins at night, slowly turning itself down to zero so i can drift off to sleep with some music playing, then in the morning the music is started and slowly turned up to wake me again.

its called a brave new world..;)

djn1
05-23-2003, 06:12 PM
Originally posted by MasterSwitch
um, yes and no. there are a number of alarm clocks out there on the net similar to the one "gatorparrots" linked me to. so if others know how to wake the computer up, it must be possible! even if its a unix script and not an apple script.
As far as I'm aware iBeeZz writes something to the kernel which (I suppose) is akin to hard-wiring it into the system. And again, as far as I'm aware the comments above are correct; i.e. there is no possibility of developing a script (unix or otherwise) that will send a machine to sleep, pause, then rewake the machine as all scripts cease to run (or stop counting in this case) as soon as your machine is asleep.

mervTormel
05-23-2003, 06:15 PM
there are no electrons flowing thru the processor in deep sleep, ergo, nothing is processed. proof is in the complete absense of the regularly scheduled cron jobs (or even a simple applescript that logs something at regular intervals). nothing runs in deep sleep. here's the system log that shows this rig slept thru the night, 23:02 to 5:19, and none of the hourly cron jobs ran. couldn't. no processor. the throbbing power light, USB keybd/mouse and NIC card are the elements which have power and can signal the rig to wake.

deep sleep log:

May 22 23:02:30 gunther mach_kernel: AppleNMI mask NMI
# no cycles. processor == so much inert silicon junk
May 23 05:19:34 gunther mach_kernel: System Sleep
May 23 05:19:34 gunther mach_kernel: System Wake
May 23 05:19:34 gunther mach_kernel: Wake event 0020
May 23 05:19:34 gunther mach_kernel: AppleNMI unmask NMI
May 23 05:19:34 gunther mach_kernel: UniNEnet::monitorLinkStatus - Link is up at 100 Mbps - Full Duplex
May 23 05:19:34 gunther mach_kernel:
May 22 23:02:39 gunther configd[106]: executing /System/Library/SystemConfiguration/Kicker.bundle/Resources/restart-Appl
eTalk
May 23 05:19:40 gunther configd[106]: executing /System/Library/SystemConfiguration/Kicker.bundle/Resources/restart-Appl
eTalk
May 23 06:00:00 gunther CRON[1964]: (root) CMD (/usr/local/bin/CMFileScanTool)
May 23 06:00:01 gunther CheckMate[1964]: Starting CheckMate Integrity Scan Fri May 23 06:00:01 US/Pacific 2003
May 23 06:00:01 gunther CheckMate[1964]: Host: gunther (192.168.1.10)
May 23 06:00:01 gunther CheckMate[1964]: ==================================
May 23 06:00:01 gunther CheckMate[1964]: /etc/inetd.conf: Ok
...

and ethernet disconnect is a normal part of deep sleep. ethernet is a heartbeat protocol. if the heartbeat stops, the NIC card (a good one) will disconnect.

now then, it may be possible that your sitch used to work because your rig didn't achieve deep sleep. if you set the processor to never sleep, you may achieve your success.

[edit: emote]

MasterSwitch
05-23-2003, 06:21 PM
mervTormel

now then, it may be possible that your sitch used to work because your rig didn't achieve deep sleep. if you set the processor to never sleep, you may achieve your success. [/QUOTE]

ok i guess we all have different ideas of what sleep means to a mac.... or that if i tell my mac to go to sleep from an energy saver control panel, its not acutally doing that..... because that is the only explanation i have for the fact that i can leave my messengers on over night. have applescripts running in the background and can wake up with the radio.

MasterSwitch
05-23-2003, 06:32 PM
mervTormel

now then, it may be possible that your sitch used to work because your rig didn't achieve deep sleep. if you set the processor to never sleep, you may achieve your success. [/QUOTE]

ok i am actually going to try that one, was way ahead of ya..:)
however i never ever had to do that in o/s9 which is why im a bit peeved.
the point is no one seems to believe i can run a script over nite, but i can, and it does acutally try to run my morning wakup cycle. unfortunately the two min counter is only being paged by the system every 20mins, so the fact the screen and maybe other parts of the comp are asleep, the 2min counter is not working right until i move my mouse and wake my comp up properly.
so its not so much i cant run a script, its i need a way to tell my comp to run my script properly. so either i need the system to page my applescript properly so the 2min counter works, or i need to wake the screen up so it behaves as the computer awake..... and i need to do it by applescript so i dont have to get out of bed..;)

hayne
05-23-2003, 07:14 PM
There is no problem in running a script overnight. The only issue here is what the state of your machine is when you are running this script. The state usually referred to as "sleep" is a state in which nothing is happening on the machine. So you clearly can't have been in "sleep" state if your script was working in OS 9.

And in the Energy Saver Prefs when it says "Put the computer to sleep when it is inactive for:", it is talking about the same "sleep" state that you get to immediately via the "Sleep" menu item.

I'm very interested in your experience that the script seems to activate at 20 minute intervals instead of 2 minutes. Please give more details about what you are seeing. (I wouldn't expect the script to activate at all if your machine is asleep.)

By the way, you seem to be deleting the beginning QUOTE tag in your replies. You need to have a QUOTE and a /QUOTE, both inside square brackets, to get proper display of quoted sections.

MasterSwitch
05-23-2003, 07:44 PM
Originally posted by hayne
There is no problem in running a script overnight. The only issue here is what the state of your machine is when you are running this script. The state usually referred to as "sleep" is a state in which nothing is happening on the machine. So you clearly can't have been in "sleep" state if your script was working in OS 9.

And in the Energy Saver Prefs when it says "Put the computer to sleep when it is inactive for:", it is talking about the same "sleep" state that you get to immediately via the "Sleep" menu item.

I'm very interested in your experience that the script seems to activate at 20 minute intervals instead of 2 minutes. Please give more details about what you are seeing. (I wouldn't expect the script to activate at all if your machine is asleep.)


yep, get about the quote thing, just trying to cut down the repeated text and im new here :)

well i have no explanation for why my sleep works and everyone elses doesnt, or the other way around..;) i was begining to assume the energy saver sleep command was different. maybe its because my script runs before my computer goes to sleep, and runs all the time its asleep. as far as i am awear the computer and the hard disk sleep, when they can, between my messengers staying in touch with the messenger servers and between my script doing its thing. considering when ever ive gotten out of bed in the middle of the nite the computer is silent, no fan or hard disk spinning.

as for timings. i havent actually logged the actual wake up time against the time the script activates but considering this 20min lag, i would assume that is a little late too. basically because i realised there was a problem and the computer time announcements werent comming reguarly i decided to log every wake up announcement. thats how i know it only makes these announcements every 20mins instead of every 2mins. move the mouse and wake the screen and my script works normally.

i cant really give any more details than that but if you want the scripts i can send them to you, they should work for you.

mervTormel
05-23-2003, 07:55 PM
Originally posted by MasterSwitch
...well i have no explanation for why my sleep works and everyone elses doesnt...
the other way around, here. deep sleep here does what i expect it to do.

examine your logs at /var/log/system.log* for sleep and wake events and you can maybe determine if your rig deep sleeps or not.

as for the time disparity, i suspect your rig deep sleeps, then wakes up, then the script gets scheduled to run.

MasterSwitch
05-23-2003, 09:07 PM
Originally posted by mervTormel

examine your logs at /var/log/system.log* for sleep and wake events and you can maybe determine if your rig deep sleeps or not.


that looks like a unix path, i havent messed with mac unix yet and the only other unix ive messed with was 6 years ago. i did search for a mac file called system.log but found none!
if it is unix, i need to know what program and what i do in the shell to look at this log..?

thx clive

hayne
05-23-2003, 09:41 PM
This posting is a bit technical and (except possibly for point #7) not of too much immediate interest for the problem at hand but this is the most convenient place to put this info for others' benefit.

I downloaded the OS X developer kit for Power Management from http://developer.apple.com/sdk/index.html#PowerManager
and thereby gained some more info about sleep.

Here's some interesting facts:
1) An inactive machine will not sleep if there is disk activity at least once a minute.

2) "An application may explicitly prevent system sleep by calling IOCancelPowerChange() when it receives a power management kIOMessageCanSystemSleep notification. An application may accidentally prevent system sleep by NOT calling IOAllowPowerChange when it receives a kIOMessageCanSystemSleep notification."

3) An "application can keep the machine awake by generating fake UI activity using IOHIDPostEvent() or UpdateSystemActivity()." This is done by the QuickTime APIs for playing movies.

4) "In Jaguar, the AppleFileServer process explicitly disables idle sleep when you have personal file sharing enabled."

5) "In the state of sleep almost all hardware except the minimum required things like the PMU (Power Management Unit) are suspended/turned off, including the CPU - this means that processes are not scheduled or run."
However, there is an intermediate sleep mode called "doze". "When in doze, PCI devices, the PCI bus, and the CPU do not have their powered removed. Doze only occurs on systems that have PCI cards whose drivers aren't power managed. Doze also occurs on any machine that doesn't support sleep, like Apple's original PowerMac G3s. If you insert a non-power managed PCI card into a recent PowerMac G4 you might notice that it no longer goes into full sleep - just doze. The reason for Doze is simple - a PCI driver that doesn't understand sleep can easily panic a machine by trying to access PCI registers after the PCI bridge is powered down."

6) A desktop "machine that is asleep will not wake if the mouse is moved ; you must click on the mouse button for it to wake up." This is apparently a way to distinguish sleep from doze.

7) There is an "AutoWake" property in the Power Manager that an application can change to make the machine wake up after a specified delay. As part of the PM developer's kit, Apple supplies a simple command-line application that demonstrates how to do this. This app must be run as root. They also supply a GUI app "Sleep Cycler" that sleeps & wakes at configurable intervals. It is quite possible that the use of the "Sleep Cycler" app would provide a solution to the original poster's problem. However this app is not validated for general use - it is intended for use by developers testing their apps in a controlled environment.

hayne
05-23-2003, 09:48 PM
Originally posted by djn1
As far as I'm aware iBeeZz writes something to the kernel which (I suppose) is akin to hard-wiring it into the system. I've downloaded iBeeZz and looked at what it installs. It does not seem to involve a kernel extension. Instead, it merely runs a daemon (process that runs in the background) which is governed by its Prefs panel. I presume that this daemon uses the Power Manager API (mentioned in my previous post) to tell the machine when to wake up.

mervTormel
05-23-2003, 10:00 PM
holy crizzapy! hayne, that is some excellent information. thanks for the duty.

AKcrab
05-23-2003, 10:02 PM
Originally posted by hayne
6) A desktop "machine that is asleep will not wake if the mouse is moved ; you must click on the mouse button for it to wake up." This is apparently a way to distinguish sleep from doze.
If this is true, then my machine never ever sleeps, just dozes. Moving my mouse will *always* wake it up. (logitech wireless using logitech drivers)

djn1
05-24-2003, 01:36 AM
Originally posted by AKcrab
If this is true, then my machine never ever sleeps, just dozes. Moving my mouse will *always* wake it up. (logitech wireless using logitech drivers)
This is interesting. I have several mice-type items installed, one of which is also a logitech wireless trackball (tho I use USB Overdrive) and, like you, moving the trackball will wake the machine. However, any activity on my Cirque touchpad (other than a button click) doesn't wake my machine. So, perhaps this is something to do with the logitech device rather than your machine is dozing rather than sleeping.

hayne
05-24-2003, 02:02 AM
The document that I was quoting about not waking when the mouse is moved went on to say: "This is true for machines that have Apple-supplied ADB & USB mice and keyboards or other, conforming mice and keyboards".

So, another interpretation is that your devices are not "conforming", which I guess means that they don't respond properly to the Power Manager protocol mentioned in my posting above. From my reading of the PM documents, this might mean that the whole system would be prevented from going into sleep. Not sure how you can tell though. If it was a laptop, you might be able to tell by looking at the battery consumption with and without the 3rd-party mouse.