I want to change the brightness level with AppleScript, and I don't know how.
It has been very hard trying to find it in the official documentation.
Any guidance is appreciated.
While what's presented in Peter's answer works for me; nonetheless, it doesn't for some as noted in the comment to that answer. Therefore, here are some other ways of adjusting the brightness.
There is a third-party utility named brightness which can be used in Terminal or in AppleScript using a do shell script
command, e.g.:
In Terminal:
/path/to/brightness 0; sleep 2; /path/to/brightness 0.75
In AppleScript:
do shell script "/path/to/brightness 0; sleep 2; /path/to/brightness 0.75
brightness
is in a directory that is in the PATH
you can omit the /Path/to/
portion of the commands. Here is another AppleScript example, not using any third-party utility:
The following example AppleScript code, tested under macOS High Sierra, will set the brightness to 0, wait 2 seconds, and set it to 0.75 (or 75%). The scale is 0 to 1 and any decimal value in between. Note that on other versions of macOS the code may need to be adjusted for the path of value indicator 1 of slider 1.
-- # Start with System Preferences closed.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
-- # Open System Preferences to the target pane.
tell application "System Preferences" to ¬
reveal anchor "displaysDisplayTab" of ¬
pane id "com.apple.preference.displays"
-- # Change the Brightness: slider.
tell application "System Events" to ¬
tell value indicator 1 of ¬
slider 1 of ¬
group 1 of ¬
tab group 1 of ¬
window 1 of ¬
application process "System Preferences" to ¬
set its value to 0
delay 2
tell application "System Events" to ¬
tell value indicator 1 of ¬
slider 1 of ¬
group 1 of ¬
tab group 1 of ¬
window 1 of ¬
application process "System Preferences" to ¬
set its value to 0.75
-- # Close System Preferences.
quit application "System Preferences"
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.