July, 2011

When was the last time you went to the library?

I recently moved to Daly City and have been going to the Mission in San Francisco to work – they have some wonderful cafes over there; I mostly go because I’d rather work from outside the house than drink coffee.

On the way back (I took the subway back) I noticed the a public library – it is only a ten minute walk from the house. So here I am, at the library today – it is fairly quiet but not eerily silent, there are others here working and studying – usually older folks, college students and a few kids off in the distance being kids.

The desks are excellent, just perfect for a laptop! There are lots of wall sockets, good Wifi, tons of books if you need a break. Since not many people use the library these days, there is plenty of empty space here – many empty desks – great place for a team. They also have meeting rooms. Oh, and did I mention that all of this is available for free?

Use OSX as an alarm clock with iTunes, AppleScript, iCal or crontab and Energy Saver

Update: Follow this tutorial for Spotify, instead of iTunes

What if none of the alarm clock apps for OSX work just the way we want them? My favorite app until a couple of hours ago was Robbie Hanson’s excellent Alarm Clock 2 app to wake me up. It is simple, intuitive and did exactly what I needed, which was to let me pick a song from my iTunes and set it as my repeating alarm. But then it couldn’t play Soma.fm streams, which iTunes gladly plays.

Luckily OSX comes with excellent built-in tools to make it play the awesome internet radio at 10am.
Here are the steps:

  1. First, create a playlist using iTunes and add music to it.
  2. Now, lets tell our Mac what we want it to do: Open AppleScript Editor. It should open a new script editor window. Paste the following code – be sure to replace MY_PLAYLIST with the name of our iTunes playlist.
    set volume 10
    tell application "iTunes"
    	set the sound volume to 0
    	play user playlist "MY_PLAYLIST"
    	repeat 10 times
    		if sound volume is less than 100 then
    			set sound volume to (sound volume + 10)
    			delay 3
    		end if
    	end repeat
    end tell

    The AppleScript is very easy to read. We first set the computer’s volume to full, then start iTunes and tell it to set the volume to 0. Then we tell it to play a playlist. Finally, we gradually increase the volume.
    Go ahead and press the “Compile” button, and then the “Run” button to test out the script. iTunes should fire up and start playing the desired playlist.
    . Now save the script to an appropriate location – I put mine in my home directory (/Users/Vishal/) and called it alarm_clock. AppleScript will save it with a .scpt extension, so it might look like alarm_clock.scpt, depending on our display settings.

  3. We now need to do two things: First, we must schedule a time for our alarm_clock script to run everyday at our chosen time, and then we need to make sure that our computer starts up on time for the script to actually run on time! Lets tackle the scheduling first.
  4. We have two choices for this step: the easy way using iCal that Joachim suggested, or the crontab way, which involves some Unix magic!
  5. The iCal way
    Simply, open iCal and create an event. Set the event to start at our preferred wake-up time (10am), and set the alarm to Run Script and select our AppleScript (in my case, alarm_clock). Thats it! Lets skip the crontab, and move onto the final step.
  6. Alternatively, the crontab way:
    We are going to use crontab to schedule our alarm clock. Open up Terminal. What we are going to do now is open up the scheduler file in a text editor, and enter a line for our alarm clock to run at the appropriate time. Here is what the crontab line’s format is:

    MM HH dd mm ww osascript /Path/to/alarm clock script.scpt

    Here MM stands for Minte, HH, stands for hour, dd for day of month, mm for month and ww for day of week. osascript is a program that lets us run AppleScript files from the command line. And finally we will want to enter the path to our alarm clock script. Start crontab’s editor:

    crontab -e

    If we have never done this before, then we are most likely in the vi editor. By default, the vi editor does not let us enter text. Press i to enter insert-mode. Now lets start typing in our crontab entry. Here’s what mine looks like:

    00 10 * * * osascript /Users/eyce/alarm_clock.scpt

    Thats right. I plan to wake up at 10:00am every day! The *’s represent wild-card entries meaning every day / month / day of the week.
    Now, when we’re happy with the line, press Escape to get out of insert-mode, and then type in :wq and press enter. This will save the crontab settings and quit the editor. Lets type in crontab -l and press Enter and crontab should show us our new settings.
    We are done in Terminal and can type in exit and press Enter to end the Terminal session and finally quit the Terminal.

  7. For the last step, we are ready to tell our system to wake up at the right time for our crontab or iCal event to run our AppleScript to run iTunes at the right time! Lets go into System Preferences, then Energy Saver. Lets press the Schedule button and set the wake-up time to about 5 minutes before our actual alarm is supposed to go off. In my case, since my alarm is set for 10am, I set the scheduler to turn my macbook on at 9:55am.

And that is all! =). The computer should start up, along with the cron/iCal event, and then the AppleScript, which will start iTunes with our favorite playlist at 10am! Or, whatever time you set yours for ;-)