Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help with Brightness Applescript!

I recently found/edited a applescript which allows you to change your brightness to a set value. Here it is:



ChangeBrightness(0.25) --Set this value between 0 and 1 to adjust brightness


on ChangeBrightness(BrightnessValue)


tell application "System Preferences"


--activate

set current pane to pane "com.apple.preference.displays"

end tell

tell application "System Events" to tell process "System Preferences"


tell radio button "Display" of tab group 1 of window 1 to if value is 0 then

repeat until value is 1


click

end repeat

end if



tell slider 1 of group 1 of tab group 1 of window 1 to set value to BrightnessValue

end tell

tell application "System Preferences" to quit

end ChangeBrightness



Now this is awesome, but I want the applescript to set my brightness to ex. (0.5) if I run the applescript and my brightness is (0.25) or lower. D

MacBook Pro (13-inch Early 2011), OS X Mountain Lion (10.8.3)

Posted on Mar 28, 2013 5:01 PM

Reply
9 replies

Mar 29, 2013 6:02 AM in response to JEE24

If I understand what you are looking to do it is this:


If the current brightness setting is less then .25 you want to raise it to .5, if it is above .25 you want to leave it alone.


Is that correct?


If so I don;t believe there is anyway to get feedback from a control when GUI scripting it. That is you can't find out what the current setting of the slider is.



If this is not what you are looking to do post back with a more detailed description.


regards

Mar 29, 2013 8:48 AM in response to Frank Caggiano

That is pretty much what I want to do.


Another option would be if you run the script the first time it sets it to .25 and if you run it again it would set it to .5


Maybe you could create some random file in the documents folder and if the file exists run ".5 brightness" which would delete the file. And if it doesn't exist run ".25 brightness" which would also create the file. Is that possible? Let me know if I didn't explain it well.


Best,


Jake.

Mar 29, 2013 9:02 AM in response to JEE24

Another option is to have a property variable in your script. Property variables keep there value between script runs.


So something like


property brightnessLevel : 0.25

ChangeBrightness(brightnessLevel) --Set this value between 0 and 1 to adjust brightness

on ChangeBrightness(BrightnessValue)

          tell application "System Preferences"
  --activate
                    set current pane to pane "com.apple.preference.displays"
          end tell
          tell application "System Events" to tell process "System Preferences"

                    tell radio button "Display" of tab group 1 of window 1 to if value is 0 then
                              repeat until value is 1
  click
                              end repeat
                    end if


                    tell slider 1 of group 1 of tab group 1 of window 1 to set value to BrightnessValue
          end tell

          if brightnessLevel is 0.25 then
                    set brightnessLevel to 0.5
          else
                    set brightnessLevel to 0.25
          end if

          tell application "System Preferences" to quit
end ChangeBrightness


will toggle between setting the level to .25 and .5

Mar 29, 2013 9:23 AM in response to Frank Caggiano

While my last script works this is a bit more 'correct'


property brightnessLevel : 0.25

ChangeBrightness(brightnessLevel) --Set this value between 0 and 1 to adjust brightness

if brightnessLevel is 0.25 then
          set brightnessLevel to 0.5
else
          set brightnessLevel to 0.25
end if

on ChangeBrightness(BrightnessValue)

          tell application "System Preferences"
  --activate
                    set current pane to pane "com.apple.preference.displays"
          end tell
          tell application "System Events" to tell process "System Preferences"

                    tell radio button "Display" of tab group 1 of window 1 to if value is 0 then
                              repeat until value is 1
  click
                              end repeat
                    end if


                    tell slider 1 of group 1 of tab group 1 of window 1 to set value to BrightnessValue
          end tell


          tell application "System Preferences" to quit
end ChangeBrightness

Mar 29, 2013 3:34 PM in response to Frank Caggiano

After a whole lot of googling I compiled most of the script. Then my keyboard turned off and I got really worried then I found an on-screen keyboard: (Mountain Lion: System Prefs> Keyboard> Show in Menu Bar> Click the icon on the menu bar> select "Show Keyboard Viewer") Then I restored my keyboard. Wasn't thinking clearly. 🙂


Here is the script: EDIT! I got it to ignore the error! Updated script:


try
          do shell script "sudo kextunload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext" with administrator privileges
end try

delay 20.0

do shell script "sudo kextload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext" with administrator privilege




Am I writting the delay correct to delay to run the next shell script after 20 seconds? Also, if there is another way to do this without the admin pass, that would be awesome!


Best,


Jake

Mar 30, 2013 11:17 AM in response to JEE24

You can just write the delay as 20. You don't need 20.0.


You can hack your way past sudo's demand for a password. One way would be by doing this.


However, it's not advisable. That password is there to protect you and your computer from harm, whether accidental by yourself or malicious by others.


Leave the password protection there. It's a small inconvenience for a significant benefit.

Help with Brightness Applescript!

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.