Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

iTunes alarm clock with speech Apps
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!]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[16,620 views]  

iTunes alarm clock with speech | 7 comments | Create New Account
Click here to return to the 'iTunes alarm clock with speech' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
great idea, but hey, use cron!
Authored by: wealthychef on Jan 10, '02 08:57:05PM

I think this script is great, but instead of having to execute the script each night, if you know you are going to wake up each morning at 6am, you could change it a little and add it to your crontab. Ie, do the following. First, remove the following lines (which are for parsing the command line of your script, can't be used with cron)


# 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


Then, change the following two lines

while :
do

to the following four lines:

i=0
while i < 10:
do
i = i+1

That keeps the script from never dying... but it means it will only yell at you for 10 times. Note that I'm guessing at the applescript syntax, I might have not dotted an i or crossed a t right.
Now, name the script, say, /Users/myloginname/Wakeup.sh
Be sure to make it executable as described (using chmod)

Finally, add the script to your "crontab file" as follows:

crontab -e
this fires up pico or vi or your favorite editor, and you add the following line to your crontab:
0 6 * * * /Users/myloginname/Wakeup.sh

(if you find yourself in some window with a bunch of ~ symbols down the left side, you are in vi, so you need to type 'i' followed by the '0 6.." etc from above, then type Esc then ":wq" to exit. )
Whew! That's easy! :-)
What this does is launch the script every morning at 6am. It speaks the message 10 times then quits.



[ Reply to This | # ]
great idea, but hey, use cron!
Authored by: vibrato on May 13, '02 11:47:37AM

Hi, I tried your cron-based solution but for some reason the shell script only works from the command line but not when run from cron. The time and wake up prompt work fine but iTunes launches without playing a track. The only thing I can think of offhand is that I do have OS X installed on an external drive and my MP3s on the drive in my PowerBook but I don't see that affecting the script in any way (since it works from the command line). THX



[ Reply to This | # ]
great idea, but hey, use cron!
Authored by: wealthychef on May 13, '02 05:25:14PM

You are right. I pared my example to this, and it still didn't work. Anybody know why?
#!/bin/sh

echo "tell application \"iTunes\" to play (some track of playlist \"Library\" whose artist is \"Alanis Morissette\")" | /usr/bin/osascript

If I run this from the prompt, no problems, it works. If I run it from a cron job, I get the following error:

syntax error: Can't get the application's event dictionary. (-2709)



[ Reply to This | # ]
great idea, but hey, use cron!
Authored by: wealthychef on May 13, '02 08:33:40PM

From everything I can tell, you cannot run applescripts from cron. So the only thing to do, would be to compile the arguments to osascript into little applications with AppleScript Editor, and call them from your script using \'open.\' For example, instead of the line:
echo \"tell application \\\"$music_app\\\" to play (some track of playlist \\\"Library\\\" whose artist is \\\"$artist\\\")\" | /usr/bin/osascript

You would put the line:

tell application \"iTunes\" to play (some track of playlist \"Library\" whose artist is \"The Rolling Stones\")

into a text file and use Applescript Editor to compile it into an application, say /Users/mylogin/StartPlayingStones

Then the line above becomes:
open /Users/mylogin/StartPlayingStones

and life is good again.
I didn\'t fully test this in the alarm clock context, but was able to confirm that cron can handle a shell script with that \'open\' command in it just fine.



[ Reply to This | # ]
great idea, but hey, use cron!
Authored by: wealthychef on May 13, '02 08:54:34PM
For more info on the cron thing, see: This thread at bbs.applescript.net

[ Reply to This | # ]
Nice
Authored by: Anonymous on Jan 11, '02 05:51:27AM

This is nice. And it's really easy to modify to make a sleep-timer as well.

#!/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
}
# 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 stop this in $hours hours and $minutes minutes"
speak_time
speak_message $ack_msg
artist1="Philip Glass"
start_music $artist1
delay_seconds=`expr $hours * 3600 + $minutes * 60`
sleep $delay_seconds
echo 'tell application "iTunes" to quit' | osascript
exit 0



[ Reply to This | # ]
iTunes alarm clock with speech
Authored by: gert on Oct 02, '03 01:15:23PM
hi the script doesn't seem to work. I must be doing something wrong. I copied and pasted the script and got rid of the backslashes and did the chmod.

If I run it it says: ./wakeup hours [minutes] ("wakeup" is the name I've given the script). And that's it? What to do?



[ Reply to This | # ]