Setting Consistent Screen Brightness
For various reasons, I like to adjust my iMac screen brightness occiasionally.
However, screen calibration is affected by the brightness setting, I like to make sure that the screen is reset to a default brightness for photo editting.
Since OSX 10.13 (and possible 10.12) Apple has introduced a new undocumented framework for screen brightness control, and the "brightness" aaplications developed in 2006-2010 no longer work. No replacement so far.
I have created this little Applescript which resets the screen brightness.
If needed, here is a version that allows user setting of the screen brightness (useful for calibration activities)
And finally here is a version that just reads the screen brightness setting
However, screen calibration is affected by the brightness setting, I like to make sure that the screen is reset to a default brightness for photo editting.
Since OSX 10.13 (and possible 10.12) Apple has introduced a new undocumented framework for screen brightness control, and the "brightness" aaplications developed in 2006-2010 no longer work. No replacement so far.
I have created this little Applescript which resets the screen brightness.
set theBrightness to 0.4 as real ## must be between 0 and 1
tell application "System Events" to set SysPrefWasRunning to 0 < (count of (get name of application processes whose bundle identifier contains "systempreferences"))
tell application "System Preferences"
if not SysPrefWasRunning then
activate
delay 0.25
end if
set prevShowAll to show all
if not prevShowAll then set prevSysPrefPane to get id of current pane
set current pane to pane "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
set readySysPref to false
repeat 30 times
try
get value of radio button 1 of tab group 1 of window "Built-in Retina Display"
set readySysPref to true
exit repeat
on error
delay 0.1
end try
end repeat
if not readySysPref then error "System Preferences is not responding"
tell window "Built-in Retina Display" to tell tab group 1
click radio button "Night Shift"
set statusNightShift to (get value of pop up button 1)
if "Off" ≠statusNightShift then
click pop up button 1
click menu item "Off" of menu 1 of pop up button 1
end if
click radio button "Display"
tell group 1
set prevBrightnessValue to (get value of slider 1) as real
set value of slider 1 to theBrightness
set newBrightnessValue to (get value of slider 1) as real
end tell
end tell
end tell
end tell
if prevBrightnessValue ≠newBrightnessValue then
set statusString to "Screen Brightness changed from " & roundToQuantum(prevBrightnessValue, 0.01) & " to " & roundToQuantum(newBrightnessValue, 0.01) & return & "Night Shift: " & statusNightShift
else
set statusString to ("Screen Brightness: " & roundToQuantum(newBrightnessValue, 0.01) & ", Night Shift: " & statusNightShift)
end if
display notification statusString
if not SysPrefWasRunning then
tell application "System Preferences" to quit
else if prevShowAll then
tell application "System Preferences" to set show all to true
else
tell application "System Preferences" to set current pane to pane prevSysPrefPane
end if
return statusString
on roundToQuantum(thisValue, quantum)
## Public domain author unknown
return (round (thisValue / quantum) rounding to nearest) * quantum
end roundToQuantum
If needed, here is a version that allows user setting of the screen brightness (useful for calibration activities)
tell application "System Events" to set SysPrefWasRunning to 0 < (count of (get name of application processes whose bundle identifier contains "systempreferences"))
tell application "System Preferences"
if not SysPrefWasRunning then
activate
delay 0.25
end if
set prevShowAll to show all
if not prevShowAll then set prevSysPrefPane to get id of current pane
set current pane to pane "com.apple.preference.displays"
end tell
set answerGood to false
set errorStr to {}
tell application "System Events" to set frontmost of process (get name of current application) to true
repeat 5 times
try
set dialogResult to display dialog ("Enter Brightness value between 0 and 1" & errorStr) default answer ""
on error
error "User Canceled"
end try
set theBrightnessString to (get text returned of dialogResult)
if 0 = (count of theBrightnessString) then
set errorStr to return & "Input Value was a zero length string; must be a number between 0 and 1"
else
try
set theBrightness to (get theBrightnessString as real)
if (theBrightness ≥ 0) and (theBrightness ≤ 1) then
set answerGood to true
exit repeat
else
set errorStr to return & "Input Value was " & theBrightness & " ; must be between 0 and 1"
end if
on error
set errorStr to return & "Input Value was \"" & theBrightnessString & "\" ; must be a number between 0 and 1"
end try
end if
end repeat
if not answerGood then error "PBKAC: User fails literacy test"
tell application "System Events"
tell application process "System Preferences"
set readySysPref to false
repeat 30 times
try
get value of radio button 1 of tab group 1 of window "Built-in Retina Display"
set readySysPref to true
exit repeat
on error
delay 0.1
end try
end repeat
if not readySysPref then error "System Preferences is not responding"
tell window "Built-in Retina Display" to tell tab group 1
click radio button "Night Shift"
set statusNightShift to (get value of pop up button 1)
if "Off" ≠statusNightShift then
click pop up button 1
click menu item "Off" of menu 1 of pop up button 1
end if
click radio button "Display"
tell group 1
set prevBrightnessValue to (get value of slider 1) as real
set value of slider 1 to theBrightness
set newBrightnessValue to (get value of slider 1) as real
end tell
end tell
end tell
end tell
if prevBrightnessValue ≠newBrightnessValue then
set statusString to "Screen Brightness changed from " & roundToQuantum(prevBrightnessValue, 0.01) & " to " & roundToQuantum(newBrightnessValue, 0.01) & return & "Night Shift: " & statusNightShift
else
set statusString to ("Screen Brightness " & roundToQuantum(newBrightnessValue, 0.01) & " ; Night Shift: " & statusNightShift)
end if
display notification statusString
if not SysPrefWasRunning then
tell application "System Preferences" to quit
else if prevShowAll then
tell application "System Preferences" to set show all to true
else
tell application "System Preferences" to set current pane to pane prevSysPrefPane
end if
return statusString
on roundToQuantum(thisValue, quantum)
## Public domain author unknown
return (round (thisValue / quantum) rounding to nearest) * quantum
end roundToQuantum
And finally here is a version that just reads the screen brightness setting
tell application "System Events" to set SysPrefWasRunning to 0 < (count of (get name of application processes whose bundle identifier contains "systempreferences"))
tell application "System Preferences"
if not SysPrefWasRunning then
activate
delay 0.25
end if
set prevShowAll to show all
if not prevShowAll then set prevSysPrefPane to get id of current pane
set current pane to pane "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
set readySysPref to false
repeat 30 times
try
get value of radio button 1 of tab group 1 of window "Built-in Retina Display"
set readySysPref to true
exit repeat
on error
delay 0.1
end try
end repeat
if not readySysPref then error "System Preferences is not responding"
tell window "Built-in Retina Display" to tell tab group 1
click radio button "Night Shift"
set statusNightShift to (get value of pop up button 1)
click radio button "Display"
tell group 1
set theBrightnessValue to (get value of slider 1) as real
end tell
end tell
end tell
end tell
set statusString to ("Screen Brightness: " & roundToQuantum(theBrightnessValue, 0.01) & ", Night Shift: " & statusNightShift)
display notification statusString
if not SysPrefWasRunning then
tell application "System Preferences" to quit
else if prevShowAll then
tell application "System Preferences" to set show all to true
else
tell application "System Preferences" to set current pane to pane prevSysPrefPane
end if
return statusString
on roundToQuantum(thisValue, quantum)
## Public domain author unknown
return (round (thisValue / quantum) rounding to nearest) * quantum
end roundToQuantum
0
-
Forgot to mention the following.
if you run these scripts from Apple's Script Editor, then you must open System Preferences>Security and Privacy>Privacy>Accessibilty and give Script Editor accessibility privileges (for the purpose of allowing it to move the slider on your screen brightness setting)
If you use Sript Editor to compile these scripts into applications (which is the bst way of using them), then you must do the same for these applications.0
投稿コメントは受け付けていません。
コメント
1件のコメント