Programatically changing the brightness of your Macbook

Hi all,

I was wondering if anyone knew a way to change the brightness of your MacBook screen in Swift without using an overlay that changes the colours?

I want the effect of just pressing the F1 and F2 brightness controls but done without using system events/ Applescript popping up windows on the screen.

I think the UIScreen.brightness is something similar to what I want but it is not available for NSscreen. I can't figure out a way to do it with IOKit either.

Things like ddccl doesn't work as the screen is not an external monitor.

If there is a solution using Swift or terminal commands any help is much appreciated.

Thanks, James

Answered by JamieS56 in 802248022

Hi all,

I have found a solution that works well.

If you create a shortcut on your Mac like this.

then you can run it as a terminal command from swift like this.

func setLaptopBrightness(brightness: Float){
    let command = "shortcuts run 'Set Laptop Brightness' <<< \(brightness)"
    let _ = shell(command)
}

or directly from terminal like this

shortcuts run 'Set Laptop Brightness' <<< 0.5

It works well for controlling your MacBook brightness programmatically until Apple lets us do it directly via Swift.

Thanks, James

Accepted Answer

Hi all,

I have found a solution that works well.

If you create a shortcut on your Mac like this.

then you can run it as a terminal command from swift like this.

func setLaptopBrightness(brightness: Float){
    let command = "shortcuts run 'Set Laptop Brightness' <<< \(brightness)"
    let _ = shell(command)
}

or directly from terminal like this

shortcuts run 'Set Laptop Brightness' <<< 0.5

It works well for controlling your MacBook brightness programmatically until Apple lets us do it directly via Swift.

Thanks, James

Programatically changing the brightness of your Macbook
 
 
Q