New Alarm, Part II

I just came back from my last “mock orals” meeting (for my PhD general exams) today and it will all be over with the main event next Thursday afternoon. After the oral exam, all I have to do in order to get my PhD is write a book about something, right?

Instead of plunging back into my books and notes when I got home today, I really needed something to relieve my stress. I wanted to program something, anything really. I decided to bring my new alarm to the next level. It has been working wonderfully. However, to “set my alarm” each day I have to create an iCal event that triggers the elaborate process described in my previous posting. That is still not streamlined enough for me. If I woke up at the same time everyday like normal people I could just create a repeating event but my unhealthy sleeping schedule varies wildly.

Today I wanted to create something such that when I click it, it 1) asks me what time I want to wake up 2) Unless it is after midnight, it creates an alarm event in iCal for the next day (otherwise it creates one for today) which triggers my elaborate alarm at the time I told it. 3) It adjusts the computer’s wake up time to be 5 minutes before the alarm (The way I had it before, the computer’s scheduled wake up time did not fluctuate with the vast changes in my sleeping schedule). I’m not very experienced with applescript, which I used to accomplish my aim (I especially miss the great date and time functions of a language like PHP for example) but I got it to work. The resulting script is here, with the full text of the script below.

set theReply to display dialog “What time do you want to set the alarm for?” default answer “8:00″
set theString to the text returned of theReply
if theString is “” then
activate
display dialog “No time entered. Please enter a time and try again.” buttons {“OK”} default button 1
else
(* SETTING THE WAKE-UP TIME FOR THE MACHINE *)
set theDate to date theString
(* Subtract a few minutes from it and set the wakeup time, giving the alarm enough time to do its work. *)
set wakeDate to theDate – 5 * minutes
set wakeDate to (time string of wakeDate as string) & “0” (* For some reason it leaves a 0 off the end *)
do shell script “pmset repeat wakeorpoweron ‘MTWRFSU’ ‘” & wakeDate & “‘” with administrator privileges
(* This command sets the computer’s wakeup time, in this case to 5 minutes before the alarm *)

tell application “iCal”
activate

(* CREATE THE ONE-TIME ICAL EVENT WITH THE ALARM WORKFLOW ATTACHED *)

set myCalendar to first calendar (* My first calendar “Home” will be the calendar for this event *)
set alarmpath to “/Users/fool/Documents/Docs/alarm.app/” (* Change this path to the location of your alarm workflow *)
(* Set the Event for tomorrow if it is after 7am. Otherwise assume I’m setting my alarm passed midnight. *)
set currentdate to the current date
if the time of currentdate as integer > 25200 then (* 7 * 60 * 60 = 7AM *)
set theDate to theDate + 1 * days
end if
set newevent to (make new event at end of event of myCalendar)
tell newevent
make new open file alarm at beginning of open file alarms with properties {trigger date:theDate, filepath:alarmpath, trigger interval:0}
set summary to “Wake Up”
set start date to theDate
end tell
end tell
end if

I saved this in my “Scripts” folder (~Library/Scripts/) so it is accessible in the script menu in the menu bar. You could just as well dump it into your Dock or whatever. Running this script assumes you have set up my “alarm.app” described in the last posting about the alarm.

5 thoughts on “New Alarm, Part II”

  1. Konrad, good luck on your PhD general exam! I would really like to read one book on Han Jian written by Dr. Lawson someday:)

  2. Konrad: all the very best! It will all seem very tame (and lame) in a few days time. We should catch up when you’re Generals free. Just remember to treat it like an elongated press conference! :)

  3. This is even better than your last script! I love it. I have searched the internet for a long time looking for a way to do exactly what ouy have done. It works perfectly for me. Thank you so much.

Comments are closed.