Improved: the previous version failed when System Preferences wasn't already open. Added a 2 second delay in the background.
`--Check if System Preferences already open?
--Don't want to quit it on exit if it's already running
set appName to "System Preferences"
if application appName is running then
set appRunning to true
else
set appRunning to false
end if
--Toggle Automatic Display Brightness
tell application "System Preferences"
set current pane to pane id "com.apple.preference.displays"
--Wait a bit for the pane to cold load
if appRunning = false then
delay 2
end if
tell application "System Events"
tell process "System Preferences"
tell checkbox "Automatically adjust brightness" of group of window 1
click
--Display a matching notification
if value = {0} then
display notification "Automatic Brightness Disabled: Manual Override" with title "Manual Display Brightness"
end if
if value = {1} then
display notification "Automatic Brightness Enabled: Sensor Engaged" with title "Automatic Display Brightness"
end if
end tell
end tell
end tell
end tell
--Quit System Preferences if this script opened it up
if appRunning = false then
delay 1
tell application "System Preferences" to quit
end if`
Post
Replies
Boosts
Views
Activity
Sorry about the formatting. No amount of tabs, spaces, backticks etc. are doing any good!
After much fiddling with Accessibility Inspector, this script finally works:
--Don't want to quit it on exit if it's already running
set appName to "System Preferences"
if application appName is running then
set appRunning to true
else
set appRunning to false
end if
--Toggle Automatic Display Brightness
tell application "System Preferences"
set current pane to pane id "com.apple.preference.displays"
--Wait a bit for the pane to cold load
if appRunning = false then
delay 2
end if
tell application "System Events"
tell process "System Preferences"
tell checkbox "Automatically adjust brightness" of group of window 1
click
--Display a matching notification
if value = {0} then
display notification "Automatic Brightness Disabled: Manual Override" with title "Manual Display Brightness"
end if
if value = {1} then
display notification "Automatic Brightness Enabled: Sensor Engaged" with title "Automatic Display Brightness"
end if
end tell
end tell
end tell
end tell
--Quit System Preferences if this script opened it up
if appRunning = false then
delay 1
tell application "System Preferences" to quit
end if```