An AppleScript to turn the brightness of your laptop display on/off

Yesterday at work, Eric was asking me if I knew of a way to immediately make the screen of his MacBook go straight to off (0 brightness). The only way I could think of doing such a thing was just holding down the button to dim the brightness or perhaps via AppleScript. He, of course, didn’t want to just hold down the brightness key, and asked me to write an AppleScript to be triggered via QuickSilver to turn the brightness off and then back on.

And of course, there was a way to do it via AppleScript.

So, after much searching and reading on various sites (I forget them all or I would definitely credit them) here is what I got:

Link to brightnesskiller code

Now, what the code here does is simply this:

First, it checks to see if “TextEdit” is open, sets a boolean accordingly, then checks to see if the logfile “brightness–.txt” exists in your Documents folder and sets another boolean accordingly. Next, it opens System Preferences and opens the Displays pane. Using very dirty code, it grabs the current brightness level and stores it into ‘brightness’. It checks to see if ‘brightness’ is over 0 then it sets your display brightness to 0 and follows that by storing ‘brightness’ into the logfile “brightness–.txt”. But if your brightness is at 0, then it opens up “brightness–.txt” and reads the value it previously had stored and sets your display to that value.

This all of course assumes that you’ve used this script when your display was on or had a brightness greater than 0. But other than that works pretty well.

In case you didn’t want to create the script/application, I’ve bundled a .scpt and .app of this script into a zip file which can be downloaded by following this link:

brightnesskiller.app & brightnesskiller.scpt

Feel free to modify it and such so long as you give me some kind of credit in the code (I leave a claiming comment at the top). I hope someone finds some use out of it.

–Edit: 06/04/2008 3:45PM–

Found an error with my original code. Made a small update. Check the source ^^ :)

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon] Tags: , , , , ,