Use iTunes as an Alarm Clock 15

Apple Script

Share
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?

  • Subscribe To Mac Tricks And Tips