This is my first post in MacScript, I know some basics of AppleScript but not much about objective-c.
Back to the topic, as I digging the solution in google about controlling night shift using AppleScript, I found there’s a project called “nshift” in GitHub that can alter the strength of night shift, it’s achieved by calling some methods of class “CBBlueLightClient” in a private framework “CoreBrightness”, I tried, it works.
Luckily, there’s a complete header file available in GitHub, I tried converting the objective-c code in nshift to Applescript, but the script failed to run:
use framework "CoreBrightness"
use framework "Foundation"
on NightShift(strength)
set clientOBJ to a reference to current application's CBBlueLightClient's alloc's init
if not strength = 0 then clientOBJ's setStrength_(strength, true)
clientOBJ's setEnabled(strength = 0)
end NightShift
NightShift(0.5)
Result: error “-[CBBlueLightClient setStrength:]: unrecognized selector sent to instance 0x600003150180” number -10000
My questions are:
why this error occurs while “setStrength” is a method in the header?
There’s a method call “getStrength” in the header file, why does it require a float argument? And Why does it always return 1 no matter what float number I pass to it in applescript,
There’s no such method. There are setStrength:commit: and setStrength:withPeriod:commit: methods, but no setStrength or setStrength: methods (that I can see).
Who knows? It’s in a private framework.
See above.
Playing with private frameworks is a matter of trial and error, and also carries an element of risk. But if you’re going to try it, you should follow the conventions of AppleScriptObjC. So your code would look more like this:
on NightShift(strength)
set clientOBJ to current application's CBBlueLightClient's alloc()'s init()
if not strength = 0 then clientOBJ's setStrength:strength commit:true -- or false
clientOBJ's setEnabled:(strength = 0)
end NightShift