Monday, August 13, 2012

AppleScript: Set Display Brightness

Apple Thunderbolt DisplayI’ve been using AppleScript for years now, but I’ve never had the opportunity to become really good at it. I can do a few simple things, but once I get beyond that, I pretty much have to turn to the web. So it was, this morning: my office has two large windows in it, so the ambient light varies quite a bit from day to day and hour to hour. I finally decided that I’m sick and tired of adjusting the brightness on three different displays—not to mention hoping that they’re all exactly the same—on a regular basis. So as usual, I turned to the web.

After a few less useful hits, I finally came to an old blog post called “Change Monitor Brightness Using AppleScript.” It was exactly what I needed, with three exceptions:
  1. Since it’s four years old, it hasn’t been updated for Mountain Lion.
  2. It isn’t designed to change multiple displays concurrently.
  3. It doesn’t include the ability to specify the brightness level, on the fly.
Having solved all three of these problems, I post my results, in hopes that it may help someone else:

set brightness_level to (text returned of (display dialog "Set Brightness Level" default answer ".875" buttons {"Cancel", "OK"} default button "OK")) as number

tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.displays"
tell application "System Events"
set j to (count windows of process "System Preferences")
repeat with i from 1 to j
set value of slider 1 of group 1 of tab group 1 of window i of process "System Preferences" to brightness_level
end repeat
end tell
quit
end tell

Enjoy! :-)

2 comments:

  1. thank you, just want i needed, first google result :)

    ReplyDelete
  2. You’re very welcome, 3DDR! :-)

    ReplyDelete