My little AppleScript Program;

Nighty Night.

 

After a few months of working at Community LIFE, I considered purchasing a radio alarm clock for my Apple iPod. I wanted to be able to wake up to my iTunes library, in addition to going to sleep to the same music. From what I read, a few of these radios had the same functions as the famed Bose WaveRadio. What enticed me to them was that you could set a sleep timer and a wake timer. With the sleep timer, you set a time period in increments of 15 minutes for the radio to shut off. Every 5 minutes or so, the radio volume would decrease by a few decibels until the time was up; whereas the volume was hardly audible and the radio would shut off. With the wake timer was set in the same fashion, the volume would increase little by little until it was at the highest volume set by the user. It could also be set the same as a normal alarm clock where the volume is already at the set level and either the alarm or music would come on abruptly.

I thought to myself that this is the time I should be learning AppleScript programming, so that's what I set out to do. I bought a few books on Mac OS X programming and AppleScript programming in addition to the free information supplied by Apple. It took a numerous tries, multiple examples, a few pulled out clumps of hair and lots of caffeine; but I finally got the majority of it working. I took a break from development in the 2nd week of June but will try to find time to work on it again, hopefully between job hunting.

Here's what the program does: When the program loads it presents you with a list of all of your playlists. You choose a playlist of songs that you would like to fall asleep to, then you choose a sleep timer ranging from 15 minutes to 3 hours. If the total length of songs in your chosen playlist is longer than the sleep timer (i.e. playlist length is 102 minutes but sleep timer is 60 minutes), the program will run for 60 minutes and decrease the decibel level by 5 every so many minutes until it reaches zero decibles, then put the Mac to sleep. If the sleep timer is longer than the chosen playlist, you have a choice to sync the timer with the playlist or choose a different playlist and sleep timer, then the program will run the same, decreasing the decibel level by 5 every so many seconds until it reaches zero. After that the Mac will put itself to sleep.

 
It works using the basic AppleScript language; commands given to each program with multiple attributes and controls. Let's take a look at it. First I double-click the Nighty Night program.
 
A dialog box comes up asking if we want to run the script or quit. When the user presses "Run", the program will open iTunes automatically if it's not open already, then the screen below appears:
 

The program loads the users iTunes library including all playlists. I'm brutally honest as I've put down that I haven't figured out a way to start over if the user presses the cancel button. I may just find a way to have the button removed from the window. Also, I have an extra period after the first sentence.

An older version of iTunes listed the songs starting with the number first if you chose to sort by artist. So 10,000 Maniacs and .38 Special would be listed before ABBA. The new version lists artists starting with the letter "A" first, while the bands whose name starts with a number get listed last, but before songs listed without an artist.

For this demonstration I chose the playlist Acquisition. Acquisition is a file-sharing program for the Mac that automatically puts downloaded songs in the Acquisition playlist whenever they're finished being downloaded.

 

The program requests the user for a sleep timer. The dialog at the top shows the name of the playlist that the user selected and the length of that playlist in minutes. This is so that the user can decide on the length of the sleep timer based on the duration of the playlist. If the user chooses a sleep timer that is less than the duration of the playlist, the program runs normally but will sleep before the end of the playlist is reached.

If the user chooses a sleep timer that is greater than the playlist duration (as in the window to the right), the following warning appears:

 

The warning box displays the playlist and its duration again as well as the sleep timer chosen.

The user can start over and choose a new playlist and/or a new sleep timer. If the user chooses "Sync sleep timer", the sleep timer will be set to the duration of the playlist.

While a few calculations are performed in the background, the songs in the playlist start. Every so many minutes the volume level will decrease by 5 decibels until it reaches zero. When the volume does reach zero, the Mac will then go to sleep (assuming no other programs are running and are requiring the use of the CPU).

As I mentioned before, the new versions of iTunes handle the sorting of artists differently. If the playlist you choose is sorted by artist rather than by song, album or otherwise; iTunes will still play the song with the artist whose name begins with a number rather than a letter.


 

Eventually I want to learn UNIX code necessary to wake it from sleep as well. The Mac operating system has Energy Saver settings that allow you to wake it from sleep and boot the computer when the power is off. Rather than rely on the user having to make the changes manually, I'd like to enable to the changes to be made within the Nighty Night program. Once I get that coded, the user can wake up to soothing sounds slowly, such as that of an orchestra or nature sounds; or very abruptly in the fashion of an alarm clock.

FOR YOUR FREE VERSION, EMAIL ME AT [email protected] WITH THE SUBJECT "NIGHTY NIGHT"

I will email you my version directly as I only have access to photo hosting sites and not file hosting sites. This is the current version last worked on the date 6/07. It's very basic, but will perform the necessary functions for now. Because this is an AppleScript file, this will only work on Apple Macintosh systems and not Windows machines. Let me know of any bugs you find and any improvements you can think of.

I'm still learning XCode, which is AppleScript designed to use more of the Mac OS X windows and graphics, but it will be a while before I master that. Enjoy the program!

 

Back to Home