Simple sleep timer with AppleScript

As you may already be aware, I have a hard time falling asleep at night. My parents suggested that I get a TV for my bedroom and use that to put me to sleep. That would probably work, but I don’t feel like buying another TV right now. I already have a laptop stand and a laptop, so I’m going to be trying that tonight. Maybe I’ll find something on Hulu or YouTube or a cable channel’s site, or perhaps I’ll just watch whatever Leo is rerunning on TWiT Live. The only problem is that my computer is lacking a simple feature that is on most TVs: a sleep timer. Yes, I could go into my settings and set the sleep time to any period of time, but I don’t want to have to keep remembering to reset it back to its normal value the next morning. I’m sure there are freeware utilities which can accomplish this, but I was feeling a little geeky on WWDC Eve and decided to do some development myself.

It has been years since I’ve done anything with AppleScript, but a few minutes of Google searches and a couple of guesses had me create this:

tell application "Finder"
display dialog "Sleep time:" default answer "1"
delay (text returned of the result) * 3600
sleep
end tell

The script (or compiled application as I eventually saved it) asks the user how many hours to wait until putting the computer to sleep, and then sleeps the computer after that amount of time. The user input is multiplied by 3600 since it takes input in seconds. The script is very simple and doesn’t do any error checking, but it does what I want and it took longer to write this blog post than to write the script.