[HOWTO] - Use iTunes as an alarm clock

Status
Not open for further replies.

tieng

Registered
A feature I've been waiting for, but it took procrastinating for a big test and a project to finally get me to sit down and figure it out.

This makes use of AppleScript and Cron. It also assumes you have a playlist that opens by default in iTunes.

First, open Script Editor and type this stuff in. Note that when I say << and >> in my code, I mean the character generated by option-\ and option-shift-\

Code:
tell application "iTunes"
   -- I have it skip to the next song, in case 
   -- we stopped in the middle of playing something.
   << event hookNext>>
   -- this next part sets a nice volume for waking up to.
   -- omit if you'd like
   set counter to 20
   repeat until counter  = 0
        << event aevtvol->>
        set counter to counter - 1
   end repeat
   set counter to 10
   repeat until counter = 0
         << event aevtvol+>>
         set counter to counter - 1
    end repeat
    -- this next line actually starts playing iTunes
    << event hookPlay>>
end tell

Compile the script as an Application and save it somewhere under a name you can remember. I have mine saved in /Applications under Play, but you can put it anywhere you'd like. Test it, and make sure it starts iTunes playing when you double click on it.

Apple provides a command line utility called "open" that makes the argument you pass it behave as if you've double clicked on it. Fire up a terminal and try

open /Applications/Play

substituting where you put it and what you named. If it works, then you're a little closer.

The next part is good stuff...we use the Unix utility cron to automatically execute it when ever you want to wake up.

A cronjob is specified thusly by saying crontab -e from the command line:

Minute Hour DayOfMonth MonthOfYear DayOfWeek [Command]

You can specify valid numeric values for each of the options, or stick in a * for it to run [command] every time a valid option comes up.

So if you wanted to execute something every Monday and Thursday at 1:25 PM, you'd say:

25 13 * * Mon,Thu [command]

Or the first and fifteenth of January and June at 2:38 pm, you'd say:

38 15 1,15 1,6 * [command]

Or in our case, at 8:15 am every weekday morning, you'd say:

15 08 * * Mon,Tue,Wed,Thu,Fri /usr/bin/open /Applications/Play

Cron opens in the vi editor by default, so after you've made the entry you want (and you can make as many as you want) you should hit the escape button, then :wq to exit vi.

You may want to put a few testing entries in there to make sure you're working right, but other than that, you should be set! Good luck, and post here to let me know how it goes!

NOTE: Cron wants the last line in the file to be a new line, so make sure you hit return a couple of times after the last crontab entry in your file.

Thanks go out to the good folks at www.applescriptcentral.com, especially <a href="http://www.barzeski.com">erik</a> for helping me get this working.
 
hey tieng!

that looks like a great script ...i am gonna give it a try.....

one thing, does it work if the computer goes to sleep? i guessed it would due to using cron, but not sure....

thanks:D
 
Just a quick update to the script...

iTunes 2 is much more scriptable; although they will still work, you don't need to do the event hookNext and event hookPlay commands. You can just use "play" and "next"

I don't believe cron will wake up a sleeping computer. And from what I can tell about Tempus (the utility mentioned above to perform cron jobs while sleeping) it won't actually wake up a computer to run the job. The first time the computer wakes, tempus will check to see if any cron jobs were supposed to have been run with the computer was sleeping.

So, if you used Tempus to run all this stuff, it would start playing music after you manually woke the machien up. You'd probably have missed your classes or work by then, though. Safest bet is still just to leave your computer running and not sleeping. (You should use all those extra cycles at night for something useful, like , encryption research, curing diseases or finding life on other planets.)

 
Here's an alteration to that trick that might be a little easier, in that you can skip all the AppleScript stuff.
If there is a particular song you like to wake up to, for this example I'll call it "Start Me Up.mp3", then you can add a line to your /etc/crontab file with the command
open "/MusicFolder/Start Me Up.mp3"
Regardless of what iTunes is doing, it will start playing that song when the Cron event is tripped. Ain't it sweet?
:D
 
Originally posted by symphonix
Here's an alteration to that trick that might be a little easier, in that you can skip all the AppleScript stuff.
If there is a particular song you like to wake up to, for this example I'll call it "Start Me Up.mp3", then you can add a line to your /etc/crontab file with the command
open "/MusicFolder/Start Me Up.mp3"
Regardless of what iTunes is doing, it will start playing that song when the Cron event is tripped. Ain't it sweet?
:D

hey symphonix

that sounds pretty simple except i don't know how to get cron running to insert my info.....
could you give me a some brief directions?

thanks a lot!:D
 
Though editing your crontabs by opening them directly is possible, you should really use the crontab command line program to edit them.

from a terminal window type:

crontab -e

It will pop open your crontab file with the vi editor. Add your entry in the format described above, and press :wq to quit.

I still prefer my script to opening a specified file; this way, I wake up to a different song every time I run it. But that's just me...
 
also you could get iTunes Clock off of versiontracker too. it's got a GUI and it's free! has a fature to set volume to max. the only problems are that it works off of 24 hr time, it'll only unpause a selected song, and it won't wake from sleep.
 
It won't work from Classic but if you boot OS 9 you can set the wake up time in the energy saver control panel. Once it is set it works even if you've changed to OS X. I suspect sleep and wake are hardware based (open firmware?) making them independent of the OS.
 
at the moment auto wakeup and startup are not implemented in 10.1 This is a system issue that will hopefully be fixed in 10.2

Untill then there is not util that has the ability to wake up your mac (or start it up) like in OS 9.

Hopefully not long to go before apple releases 10.2!
 
I realise how old this thread is, but I'd someone can probably still help me out. I used the script tieng provided, and every time I run the script classic tries to open to open up OS 9 iTunes. How can I get it to open OS X iTunes, it seems that no one else had any problems, so hopefully it is easily overcome. Any help would be appreciated.
 
Script Editor should ask you to locate iTunes when you write the script. Try it with something else, like TextEdit, then change it back to iTunes.

Aside from that and Chevy's suggestion, I'm not sure what you should do.
 
Please read the forum description.
If you have a question, post in the proper forum.
 
Status
Not open for further replies.
Back
Top