iTunes alarm clock with speech

Jan 10, '02 04:36:23PM

Contributed by: hayne

The following Bourne shell script elaborates on some elements used in other people's tips to provide a wakeup service with music and voice. It uses AppleScript to tell iTunes to play a random song of a specified artist.

Read the rest of the article for the script...

[Editor's note: There was a previous thread about iTunes as an alarm clock, but this script is a little more interesting and we have the source now stored on macosxhints, in case the MacNN thread referenced in the prior article goes away at some point...]

#!/bin/sh

# This script plays music and speaks to you to wake you up
# after a specified number of hours & minutes

# Functions
#--------------------------------

music_app="iTunes"

speak_message()
{
message="$*"
echo "say \"$message\"" | /usr/bin/osascript
}

speak_time()
{
hour=`date "+%H"`
time=`date "+%I %M"`
if [ $hour -le 11 ]; then
message="It is now $time A-M"
else
message="It is now $time P-M"
fi
speak_message $message
}

start_music()
{
artist="$*"
echo "tell application \"$music_app\" to play (some track of playlist \"Library\"
whose artist is \"$artist\")" | /usr/bin/osascript
}
# NOTE: The echo command is shown on two lines; enter it as one!

pause_music()
{
echo "tell application \"$music_app\" to pause" | /usr/bin/osascript
}

resume_music()
{
echo "tell application \"$music_app\" to play" | /usr/bin/osascript
}

# Command-line arguments
#--------------------------------

if [ $# -lt 1 ]; then
echo "Usage: $0 hours [minutes]"
exit
fi

hours=$1
minutes=0
if [ $# -gt 1 ]; then
minutes=$2
fi

ack_msg="I will wake you up in $hours hours and $minutes minutes"
speak_time
speak_message $ack_msg

# Sleep until it's time to wakeup
#--------------------------------

delay_seconds=`expr $hours \* 3600 + $minutes \* 60`
sleep $delay_seconds

name=$USER
message1="Time to wake up! Wake up $name"
artist1="The Rolling Stones"

# Deliver wake-up message
#--------------------------------

start_music $artist1

while :
do
sleep 30
pause_music
speak_time
speak_message $message1
sleep 1
resume_music
done
Open a terminal, paste in the above code (remember to combine the one double-line into one line!), modify the "artist" and text variables to suit your preferences, and then save the file somewhere on your path (a folder named 'bin' in your user's folder works well). Make the file executable ("chmod 755 name_of_file") and then run it with no arguments to see the command line options.

[Editor's note: I tried this, and it worked as described ... cool!]

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020110163623707