Use iTunes as an Alarm Clock 12

Apple Script

Use iTunes as an Alarm Clock

Hey

Earlier today I mentioned about . Awaken is a small application that you can use to set iTunes to go off at a particular time. This means that you can use it as an alarm clock. Although Awaken is a good application the main function, or setting an iTunes alarm, can be done free with a little bit of scripting and know how. This post will show you the basics. You can then take it further as you see fit.

There are two ways of doing this in my opinion. Using Applescript and iCal or Applescript and cron. The first is very easy to set up, but you need to use iCal. If, like me, you have a full calendar, adding in another event will just fill up a bucket that is overflowing. The second option is a simpler option once you have set up. You don’t need to use iCal, just a link to a script. For the purpose of this post I am going to show you both.

The first step is to make your AppleScript. You can take this as far as you want. I have included a couple of features to make it more fun. As always I will explain what each line means and why it is included. First open up the Apple Script editor under Applications > AppleScript >Script Editor. Then paste into the window the following:

tell application "iTunes"
launch
stop
set the_playlist to user playlist 1
set sound volume to 100
repeat 2 times
set shuffle of the_playlist to false
set shuffle of the_playlist to true
end repeat
play the_playlist
tell application "System Events"
tell application "iTunes" to activate
key code 17 using command down
key code 3 using command down
end tell
end tell

It looks very boring. I haven’t got syntax highlighting installed. But it does a couple of unique things.

tell application "iTunes"
launch
stop

This bit of code opens iTunes. The stop part is included because I was getting a couple of problems with running a playlist straight away.

set the_playlist to user playlist 1
set sound volume to 100

This sets the playlist to be played as the Library. You can insert any name you want here if you want to play a different playlist. Make sure you include the name of the playlist in double quotes (” “). The second half sets the volume to maximum. As a side not if you do want to use this script make sure you have your computer volume on. It saves a lot of debugging problems.

repeat 2 times
set shuffle of the_playlist to false
set shuffle of the_playlist to true
end repeat

This section just shuffles up the playlist twice. You can change the shuffle number to anything you want. From what I have read it is the same as pressing option click on the shuffle button.

play the_playlist

This is a major point in the code. It plays the playlist.

tell application "System Events"
tell application "iTunes" to activate
key code 17 using command down
key code 3 using command down
end tell
end tell

Awaken had a funky screen overlay that enabled you to view the current track that is playing. I tried to do this with Front Row. Since I don’t have my iTunes library on my computer it wouldn’t find the tracks so I couldn’t run any AppleScript. As a compromise the script runs the iTunes visualizer. The keycode numbers were found using Full Key Codes. If you haven’t upgraded to iTunes 8 do it just for the funky new visualizer.

Once as you have pasted the script and ran it check to make sure it works, we can now add it to iCal. iCal need the script to be saved as a .scpt. In the Apple Script Editor save the file out, you don’t need to change anything settings wise. Then open iCal insert a new event at the time you want your computer to start playing music .  Set the Alarm to “Run Script” and find the script using the pop up.

I said at the beginning of this post that you can do this with cron. It just as simple. Instead of saving the Apple Script as a script, save it out as an application. Then open Terminal and type the following.

crontab -a

Press the “a” key. Then add this on a new line if necesary.

30 7 * * * /Path/To/Script/scripname.app

Then press escape and type without quote “:wq!” it should then work at  you stated time. From the example above the Apple Script will run at 7:30am every day. You can customize this as much as you want. For more information refer to a quick Google Search on crontab.

Conclusion

Hopefully you can follow what I have written and you have successfully made your own iTunes alarm. Granted it is not as cool as Awaken but it is free. In my opinion it does the job just as well.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

12 Responses to “Use iTunes as an Alarm Clock”

  1. 1

    Hi James,

    I’m going to try this script when I get back to my mac later. Can it be used if the mac is in sleep mode? I was going to use the iCal method.

    I actually have Aurora installed but find there are days when it randomly doesn’t work. Very frustrating.

    Comment By Rich on September 17th, at 1:38 am

  2. 2

    I’m not sure about sleep mode. You will have to test it out.

    Comment By admin on September 17th, at 9:47 am

  3. 3

    Hi James,
    tried to do this thou I’m a noob in coding. But somehow this did not work properly. After reopening the code and testing it, it told me this:
    [„«class pShf» of "Hot Tracks"“ can not be set to „false“] with “Hot Tracks” being the name of the playlist I want to use as an alarm clock. After some playing, it told me that this playlist couldn´t be set to “true” either.
    Can you tell me what the problem with this is? Did I use the wrong syntax to adress the playlist? Because this was basically the only thing I changed about your script, but I used double quotes on that one.
    Thanks for any advice and for running this great blog. I check your widget on my dashboard every day :D
    Cheers,
    Alex

    Comment By Alex on September 17th, at 10:56 am

  4. 4

    To specify a playlist you should use this code:

    set the_playlist to user playlist "Hot Tracks"

    I hope that helps.

    Comment By admin on September 17th, at 10:58 am

  5. 5

    It sure does. Thanks for the quick help. I’m just a noob in code ;)
    Thanks a lot,
    Alex

    Comment By Alex on September 17th, at 11:54 am

  6. 6

    Don’t worry Alex we all have to start somewhere. If you don’t make mistakes you don’t learn.

    Comment By admin on September 17th, at 2:21 pm

  7. 7

    I was going to try your method when i got home but decided to look into doing it with automator first.

    My computer now wakes from sleep at 7.30am and runs an automator created application. This checks for new mail, then sets system volume, then iTunes volume, grabs my ‘morning’ playlist, plays it and turns on the visualiser.

    Very handy, and much less scary than writing a script in Terminal for those of us who like a visual approach.

    Thanks for giving me a kick in the right direction though.

    Comment By Rich on September 18th, at 5:17 am

  8. 8

    Automator is just another way of doing this sort of thing. Automator is probably better for people who don’t know how to to use AppleScript or want a more GUI approach.

    Comment By admin on September 18th, at 10:12 am

  9. 9

    Alright – so im willing to try this the automator way, or the AppleScript way, but I have one problem. I have the bose companion 5 series speakers, and whenever my computer is inactive or asleep, the speakers themselves go to sleep. So basically because the speaker’s module controls the sound, my computer will wake up and go through the normal process, but the speakers won’t. Please help

    P.S. LOVE THE WEBSITE!!!!!

    Comment By Jake Speyer on September 23rd, at 5:17 am

  10. 10

    @Jake. It be perfectly honest I can’t really help you. I don’t have any Bose speakers so I can’t comment. I think the internet might be a better help.

    Comment By admin on September 23rd, at 9:57 am

  11. 11

    I’m trying to get this script to target a playlist on my ipod, but I’m having no luck finding the right coding. Any ideas?

    Comment By Chris on September 24th, at 2:47 pm

  12. 12

    Wouldn’t it be easier to just play the playlist on your computer instead of on your iPod.

    Comment By admin on September 25th, at 8:48 am

Leave a Reply

You can use: <a href> <h1 - h6> <acronym> <code> <em> <trike> <strong> <i> <b>