Announcement

Collapse
No announcement yet.

Script/Action to Dim Screen based on time of day

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script/Action to Dim Screen based on time of day

    A few months back, I had the idea to use an Apple Script or Automator Workflow to check the system time, and automatically dim the screen at a certain time each day. This way the screen wouldn't be blindingly bright at night, but would be at full brightness during the day.

    My idea was to have the code check the system time, and at say, 7pm, take the brightness down a few notches. On the flip side, anytime between say 6am and 6:59pm, it would adjust back to full brightness.

    I don't really know any Applescript. I tried recording an action (like a macro in MS office) but that didn't work. I was also unsuccessful at creating an Automator Workflow to accomplish this (I can't get Automator to allow access to things in System Prefs). I've tried searching to see if someone already did this, but I haven't had any luck. Is this even possible? Is the Mac Mini capable of adjusting the dimness of an external display (meaning, can this type of thing only be done on a notebook)? Thanks for any help anyone can provide!
    iDub Status:

    99.99% -- It's alive!!!...the extra .01% is because it'll never be completely "done."

  • #2
    You know, I think I looked into this too. I think from what I found the only real way was to "stuff" the keyboard buffer (or whatever) with the right key-combo to dim the screen (ie, Fn-F1 or Fn-F2). It sures seems like there should be an easier way.

    I have no idea if external displays can be dimmed either. I only have a notebook. I gave my dad a MacMini for Christmas to replace his fried Dell, so maybe I can give him a ring and see if he can dim the monitor, etc.
    -

    Comment


    • #3
      I know, you'd think someone else would need this functionality. What exactly do you mean by stuffing the key combination? Into an applescript? My coding skills aren't the greatest in general, and they're virtually non-existent on a Mac.

      The Mac Mini that's going in my car is on my desk at home next to my PowerBook, so I can check as well. I think I did this a few months back, and I *think* it's not even an option on the Mini. My guess is that OS X detects what hardware it's being installed on and enables this feature accordingly. If that is in fact the case, maybe there's some way to trick it into thinking the Mini is notebook (but then there's the whole issue of whether or not the touch screen will respond if we tell it to dim).
      iDub Status:

      99.99% -- It's alive!!!...the extra .01% is because it'll never be completely "done."

      Comment


      • #4
        Not sure about how to do this in AppleScript and Automator or whether it is possible at all, but this thing is relatively easy otherwise. All you need to do is to adjust the gamma of the screen. An example from my code:

        Code:
        - (void) setNiteVision:(BOOL)niteVisionState {
        	if (niteVisionState){
        		// engage the nite vision by changing the screen gamma value
        		// the values (which determine the color an brightness) are
        		// taken from the module's setup where they are stored as NSNumbers
        		CGGammaValue gamma, min;
        		CGDisplayErr err;
        
        		min = 0.1;
        		float redMax = [[moduleSetup objectForKey:@"RED_GAMMA"] floatValue];
        		float greenMax = [[moduleSetup objectForKey:@"GREEN_GAMMA"] floatValue];
        		float blueMax = [[moduleSetup objectForKey:@"BLUE_GAMMA"] floatValue];
        		gamma = 1.0; // we do not want to change the curve, just adjust end points
        	
        		err = CGSetDisplayTransferByFormula(
        			CGMainDisplayID(),
        			min, redMax, 1, // red
        			min, greenMax, 1, // green
        			min, blueMax, 1); // blue
        	}
        	else
        	// restore the default gamma curve as defined in the system
        	// preferences for all displays
        	CGDisplayRestoreColorSyncSettings();
        }

        Comment


        • #5
          i want the app pease.
          Cant code cause I dont know how, but give me the paint bucket and my eraser and have at you!

          Comment


          • #6
            Following is an Applescript handler that use GUI scripting to control the Display preference pane. I presume if your screen does not have a "Brightness" slider that this script will error. It works fine on my Apple studio display but I haven't tested it on the Xenarc.

            Cut and paste the code below the dashes and run it in applescript studio to try it out. If it works, save one Applescript with the dim value and save a different one with the bright value. You should be able to dim/bright by running the appropriate script.

            ------------------------------------------------------------

            ChangeBrightness(.5) --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 2 of tab group 1 of window 1 to set value to BrightnessValue
            end tell
            --tell application "System Preferences" to quit
            end ChangeBrightness
            Originally posted by ghettocruzer
            I was gung ho on building a PC [until] just recently. However, between my new phone having internet and GPS and all...and this kit...Im starting to have trouble justfiying it haha.
            Want to:
            -Find out about the new iBug iPad install?
            -Find out about carPC's in just 5 minutes? View the Car PC 101 video

            Comment


            • #7
              Thanks Bugbyte, I tried it on my Powerbook and it works great! I'll try it on the Mini tomorrow as well. One question though, is there any code that can be added to make everything hidden as it works? Currently you see the Displays pref pane opening and adjusting, and then it remains open when the script has finished running. Can all that be done in the background? Thanks!
              iDub Status:

              99.99% -- It's alive!!!...the extra .01% is because it'll never be completely "done."

              Comment


              • #8
                I can't quite get every bit of it to be invisible, but here's a script that does it and only shows the display window briefly, then closes the prefs window.

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

                on ChangeBrightness(BrightnessValue)

                tell application "System Preferences"
                launch without visibles
                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 2 of tab group 1 of window 1 to set value to BrightnessValue
                end tell
                tell application "System Preferences" to quit
                end ChangeBrightness
                Originally posted by ghettocruzer
                I was gung ho on building a PC [until] just recently. However, between my new phone having internet and GPS and all...and this kit...Im starting to have trouble justfiying it haha.
                Want to:
                -Find out about the new iBug iPad install?
                -Find out about carPC's in just 5 minutes? View the Car PC 101 video

                Comment


                • #9
                  Don't know if this is helpful or not, I have a perl script here that's meant to run from cron every X minutes and do something depending on if it's night or day.

                  The fleshed out version on my system uses xbrightness to change the gamma in X. (Though it checks to see if a net cable is plugged in first, then it acts like it's always day because I'm in the house).

                  I know OSX is based on BSD, but I know nothing about the windowing system.
                  GE Cache Builder | [email protected] |Coolstuff :autospeed.com | bit-tech.net | Nitemax Ultra Pinouts

                  Comment


                  • #10
                    Thanks Bugbyte! The revised code works very well. All that shows is the system prefs window for a second or two, but for my needs that's acceptable. Now I just have to test it on the Mini connected to the Xenarc.

                    Shotgun, I saved Bug's script as an applicatoin and I'm going to work with automator to run it at specific times of the day, but thanks for your post.
                    iDub Status:

                    99.99% -- It's alive!!!...the extra .01% is because it'll never be completely "done."

                    Comment


                    • #11
                      Ok so nevermind Automator, I resaved them as scripts, then created an event in iCal that repeats everyday at a specified time, and I set the alarm to Run Script. Still ahve to test on the mini though.
                      iDub Status:

                      99.99% -- It's alive!!!...the extra .01% is because it'll never be completely "done."

                      Comment


                      • #12
                        I'm about to resurrect an old thread, in hopes of putting it to bed for good! I took my install off of the indefinite hold it's been on for almost a year, and I just had some new thoughts on this issue...

                        Bugbyte's AppleScripts mentioned above work great for an Apple notebook or cinema display, but unfortunatly have no effect on the touchscreens most of us are using. However, using Shades, you can adjust the screen brightness of any display connected to any Mac, as the adjustment is not dependant on any specific type of hardware. It also has the ability to assign hot keys to increase and decrease brightness functions.

                        As I mentioned earlier, I have no coding skills at all, SO, would someone be willing to write up an AppleScript similar to Bugbyte's that opens Shades (if its not already running) and sends the hot key combinations to it? Shades can run hidden in the background (in its preferences, keep it out of the dock and the menu bar), and we can set the resulting Apple Script as a reccuring iCal alarm, to seamlessly automate the process of dimming and brightening our touchscreens at predetermined times of the day.

                        I would be totally thrilled if someone would be able to do this! Thanks in advance :-)
                        iDub Status:

                        99.99% -- It's alive!!!...the extra .01% is because it'll never be completely "done."

                        Comment


                        • #13
                          I suggested this idea in the Amp wishlist thread but I don't think people took it seriously. I like this implementation. It should be a snap to write a script.

                          For awhile, I looked into trying to calculate sunrise/sunset based on a user's lat/long but Applescript doesn't have the math functions to do it. You end up having to call them out of Coccoa! The iCal alarm idea is a dead simple solution for that problem.

                          I'll look at it but no promises on time frames.
                          Originally posted by ghettocruzer
                          I was gung ho on building a PC [until] just recently. However, between my new phone having internet and GPS and all...and this kit...Im starting to have trouble justfiying it haha.
                          Want to:
                          -Find out about the new iBug iPad install?
                          -Find out about carPC's in just 5 minutes? View the Car PC 101 video

                          Comment


                          • #14
                            http://www.srrb.noaa.gov/highlights/sunrise/azel.html

                            Check the source of the page for JavaScript implementation of the algorithm that calculates the position of sun. And I am sure there are plenty of other sites and libraries. AFAIK it does not require any math, that is not doable in AppleScript.

                            Comment


                            • #15
                              Try this

                              This worked on my Mini with Shades installed.

                              There's no error handling in it. It assumes Shades is running. It then checks to see if it is either before or after 12:00pm. If it is after 12:00pm, it dims. If it is before 12:00pm, it brightens.

                              It sends 10 repeats of Cmd and '-' or 10 repeats of Cmd and '+'. Make sure your hotkeys are set to this setting.

                              You can easily modify how many times it repeats by opening the script in Script Editor and changing the number in the "repeat" lines. There are two.
                              Attached Files
                              Originally posted by ghettocruzer
                              I was gung ho on building a PC [until] just recently. However, between my new phone having internet and GPS and all...and this kit...Im starting to have trouble justfiying it haha.
                              Want to:
                              -Find out about the new iBug iPad install?
                              -Find out about carPC's in just 5 minutes? View the Car PC 101 video

                              Comment

                              Working...
                              X