Way to increase screen brightness in AppleScript?

Joined
Nov 12, 2015
Messages
36
Reaction score
0
Points
6
I'm trying to write an AppleScript to increase the brightness on my MacBook. Is there a way to do this from AppleScript and if so how? I did some searching and I found and tried the following code:

Code:
tell application "System Events"
    key code 107
end tell

But when I entered this into Script Editor and pressed the "Run the Script" button, it just beeped and didn't do anything.
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Try the script in the 2nd post in this Stackoverflow question. It's a lot more generic and doesn't rely on varying/changing key codes Additionally, on the latest MBP with touch bar, the brightness is a similar slider as the one in system preferences, so in theory that solution should work on every Mac.
 
Joined
Oct 16, 2010
Messages
17,526
Reaction score
1,560
Points
113
Location
Brentwood Bay, BC, Canada
Your Mac's Specs
2011 27" iMac, 1TB(partitioned) SSD, 20GB, OS X 10.11.6 El Capitan
Last edited by a moderator:
OP
C
Joined
Nov 12, 2015
Messages
36
Reaction score
0
Points
6
Try the script in the 2nd post in this Stackoverflow question. It's a lot more generic and doesn't rely on varying/changing key codes Additionally, on the latest MBP with touch bar, the brightness is a similar slider as the one in system preferences, so in theory that solution should work on every Mac.

You mean this one?

Code:
tell application "System Preferences"
	if it is running then
		quit
	end if
end tell
delay 0.2
activate application "System Preferences"
tell application "System Events"
	tell process "System Preferences"
		click button "Displays" of scroll area 1 of window "System Preferences"
		delay 1
		set value of value indicator 1 of slider 1 of group 2 of tab group 1 of window "Built-in Retina Display" to 0.75
	end tell
	delay 1
	quit application "System Preferences"
end tell

That didn't work. I got an error message: error "System Events got an error: Can’t get window \"System Preferences\" of process \"System Preferences\"." number -1728 from window "System Preferences" of process "System Preferences"


No. I tried all of the scripts in those threads, nothing worked. The closest I got was this script:

Code:
tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
    set value of value indicator 1 of slider 1 of group 2 of tab group 1 to 0.75
end tell
quit application "System Preferences"

That opened the Built-in Retina Display in System Preferences, but it didn't move the slider. I got the following error message: error "System Events got an error: Can’t get group 2 of tab group 1 of window \"Built-in Retina Display\" of process \"System Preferences\". Invalid index." number -1719 from group 2 of tab group 1 of window "Built-in Retina Display" of process "System Preferences"
 
Joined
Dec 8, 2018
Messages
1
Reaction score
0
Points
1
When using an external keyboard, the brightness key codes change from 107 and 113 to 145 and 144 respectively, to lower/raise the brightness level.

You may also want to check out this command line tool: https://github.com/nriley/brightness
It allows you to specify what percent brightness you want, which is probably more useful.

RazorEdge's solution also has the benefit of allowing you to set a brightness percentage but unfortunately that solution will only work on a MBP with retina display.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top