2

Is there a way to adjust the Mac's display brightness in Objective-C? I have sensitive eyes, and sometimes the display seems like it could be darker. Combine that with Jeff's blog post about geeks and darkness, and it can get pretty annoying.

I think it would stand to reason that if the display could be set as a number value, it could get set lower than the regular screen brightness. I have Googled all over, but all of the things that came up were about the iPhone. Is there a way to do it for Mac?

CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
  • Pardon my ignorance, Why would your program do this when one can just as easily do that via System Preferences? –  Mar 15 '12 at 14:24
  • http://stackoverflow.com/questions/3239749/programmatically-change-mac-display-brightness –  Mar 15 '12 at 14:25
  • http://stackoverflow.com/questions/818829/how-to-dim-os-x-desktop-using-cocoa-core-animation –  Mar 15 '12 at 14:26
  • I want it to be darker than the lowest setting. – CoffeeRain Mar 15 '12 at 14:27
  • But for the whole system, right? Not just your app? –  Mar 15 '12 at 14:28
  • Yeah, but if I could make it so when I launch it it dims the screen or has options to, that would work also. – CoffeeRain Mar 15 '12 at 14:29
  • Check out https://github.com/pirate/mac-keyboard-brightness which has a utility for the display brightness as well. – Nick Sweeting May 28 '18 at 22:47

1 Answers1

2

just googled around and found this :)

Here is the code how to get brightness.

    // almost completely from: http://mattdanger.net/2008/12/adjust-mac-os-x-display-brightness-from-the-terminal/
- (float) get_brightness {
    CGDirectDisplayID display[kMaxDisplays];
    CGDisplayCount numDisplays;
    CGDisplayErr err;
    err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays);

    if (err != CGDisplayNoErr)
        printf("cannot get list of displays (error %d)\n",err);
    for (CGDisplayCount i = 0; i < numDisplays; ++i) {


        CGDirectDisplayID dspy = display[i];
        CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy);
        if (originalMode == NULL)
            continue;
        io_service_t service = CGDisplayIOServicePort(dspy);

        float brightness;
        err= IODisplayGetFloatParameter(service, kNilOptions, kDisplayBrightness,
                                        &brightness);
        if (err != kIOReturnSuccess) {
            fprintf(stderr,
                    "failed to get brightness of display 0x%x (error %d)",
                    (unsigned int)dspy, err);
            continue;
        }
        return brightness;
    }       
    return -1.0;//couldn't get brightness for any display
}

How to set the brightness.

// almost completely from: http://mattdanger.net/2008/12/adjust-mac-os-x-display-brightness-from-the-terminal/
- (void) set_brightness:(float) new_brightness {
    CGDirectDisplayID display[kMaxDisplays];
    CGDisplayCount numDisplays;
    CGDisplayErr err;
    err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays);

    if (err != CGDisplayNoErr)
        printf("cannot get list of displays (error %d)\n",err);
    for (CGDisplayCount i = 0; i < numDisplays; ++i) {


        CGDirectDisplayID dspy = display[i];
        CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy);
        if (originalMode == NULL)
            continue;
                io_service_t service = CGDisplayIOServicePort(dspy);

        float brightness;
        err= IODisplayGetFloatParameter(service, kNilOptions, kDisplayBrightness,
                                        &brightness);
        if (err != kIOReturnSuccess) {
            fprintf(stderr,
                    "failed to get brightness of display 0x%x (error %d)",
                    (unsigned int)dspy, err);
            continue;
        }

        err = IODisplaySetFloatParameter(service, kNilOptions, kDisplayBrightness,
                                         new_brightness);
        if (err != kIOReturnSuccess) {
            fprintf(stderr,
                    "Failed to set brightness of display 0x%x (error %d)",
                     (unsigned int)dspy, err);
            continue;
        }

        if(brightness > 0.0){
        }else{
        }
    }       

}

I found it here - http://www.alecjacobson.com/weblog/?tag=brightness.

Dominik Hadl
  • 3,609
  • 3
  • 24
  • 58
  • That's the same answer from : http://stackoverflow.com/questions/3239749/programmatically-change-mac-display-brightness –  Mar 15 '12 at 14:29
  • You got it. A cocmmand line example can be found here: [Matt Danger](http://mattdanger.net/2008/12/adjust-mac-os-x-display-brightness-from-the-terminal/) – Living Skull Mar 15 '12 at 14:30
  • It tells me `failed to set brightness of display 0x506ef80 (error -536870201)` – CoffeeRain Mar 15 '12 at 14:34
  • Should be. But I am not sure :/ – Dominik Hadl Apr 04 '12 at 20:45
  • one of our app "lampSOS" using it which was rejected last year, because we are flashing SOS there... another app called "AFTERsongs" have to be changed to black curtain mode before list in app store. dragonBTV – Jiulong Zhao May 17 '12 at 21:52
  • @kba Sadly, I didn't solve it, but found out that it was because I wasn't using the Mac hardware (aka Hackintosh). – CoffeeRain Jul 30 '12 at 03:38
  • IODisplayGetFloatParameter is not in 10.9 – Daij-Djan Dec 01 '13 at 20:42
  • IT is in 10.9, there just doesn't seem to be a clear umbrella header, that i can find... but you can go in and import what you need. – uchuugaka Dec 24 '13 at 08:42
  • CGDisplayIOServicePort IS deprecated in 10.9 and has no replacement.. :( – uchuugaka Dec 24 '13 at 14:34
  • Turns out this is still doable with a lot of poking around in the poorly documented IOKit and IORegistry (the developer app from the developer site, IORegistryExplorer.app helps a lot, but not without also being fairly cryptic. If anybody is interested, I will post some code once I verify a reliable way to identify the display without IORegistryExplorer. – uchuugaka Dec 25 '13 at 01:14