04/03/2006

wake me up!

i'm notoriously bad at getting up, and since i pawned my last ipod (don't ask...) i don't have any way of waking up to a selection of music in the morning. i decided this wouldn't do at all, and i was getting tired of the awful ring-tone my phone used as its alarm noise. now, my macintosh has itunes, and a set of nice loud speakers. howevber, i'm running OS X 10.3.9, so no automater for me. i do have a working knowledge of applescript though, and itunes is chock-full of applescript-awareness, so i decided to write a little script to work as an alarm clock.

the script is really very simple. to use it, open up the script editor, which lives in the /Applications/AppleScript/ folder. enter the following text, exactly as shown:

-- iWake
--
-- slowly raise itunes volume to wake up.
-- call from batch processing every morning
--
-- author: andrew kennedy
-- created: 02 march 2006 09:54
--
-- copyright (c) 2006 nevada systems

property wake : 30 -- time in minutes to wake up in
property vol : 100 -- volume setting
property step : 1 -- delay in seconds between volume changes

on run
-- get current volume
tell application "iTunes"
set vol to sound volume
set sound volume to 0
end tell

-- set wake time in minutes
set step to (wake * 60) / vol

-- start itunes
tell application "iTunes"
play
end tell

-- slowly raise the volume
repeat with counter from 0 to vol by 1
delay step
tell application "iTunes"
set sound volume to counter
end tell
end repeat
end run


you can test this script out by choosing compile and then, making sure iTunes is running but paused, press run at the top of the script editor window. what should happen is that the itunes volume will be reset to zero, and then start playing, while slowly raising the volume back to the original level over the next ten minutes. assuming you see the volume drop and iTunes start, you can (rather than wait ten minutes) just quit the iWake application, but make sure it's not the script editor.

now, save the whole thing as an application somewhere useful. i chose to put mine in ~/bin/iWake.app which is in my path. you will need a way to run your alarm clock, at whatever time in the morning you want woken up. i use the Unix cron daemon, which is part of the BSD package installation on OS X. go to the terminal, and run the command crontab -e and you will be presented with a blank editor window, probably vi. now, add the following text (to the end of the file if there is anything there already) and save it.

##
# adk cron entries
# modified 2006/03/04 -5h00
##
# wake up with itunes in the morning at 09h00
00 09 * * 1-5 osascript /Path/to/your/saved/iWake
# and at 10h30 weekends
30 10 * * 0,6 osascript /Path/to/your/saved/iWake


make sure that you replace the path after osascript with wherever you saved the script. if you're not sure how to use vi, paste the text into another editor and modify it there, then copy the whole thing to the clipboard and just press the following keys in order G o [command]-V [escape] :wq [enter] when vi appears, and you should be told crontab: installing new crontab when finished. for help on changing the times and days look at the crontab(5) man page.

you now get woken up gently by your favourite music. which is good. as an exercise for the reader, i would suggest modifying the script to choose a particular playlist, since this version just resumes whatever was playing when itunes was paused. next time, a sleep timer that drops the volume...

No comments: