How to retrieve the current screen brightness

With Keyboard Maestro is there a way to retrieve the current screen brightness and store it in a variable and restore the brightness to this value later on?

I do know about the command line tool brightness I could interface with, but of course a native way (not involving command line parsing) would be easier...

I don't think there is a way to do that, which is why the command-line tool was created.

Even a quick look around did not find a way to do it in AppleScript.

I think brightness is your best bet.

FWIW, if you just want the brightness level from the output of brightness you can use this bit of shell scripting:

/usr/local/bin/brightness -l | /usr/bin/awk -F' ' '/brightness/{print $NF}'

Translated, that says: “Use the brightness -l command, and then filter the results through awk. Use the space character to determine "fields" (-F' '), then look for the word 'brightness' and show me that last 'word' ($NF) on the line that has the word brightness

So if this is the usual output of brightness -l:

display 0: main display, ID 0x4280e40
display 0: brightness 0.799805

Then the output of "the last 'word' of the line with the word 'brightness' in it" would be 0.799805

HTH.

Caveat: I don't have multiple monitors, so I can't test how it would work in that scenario.

Thanks for looking into this. With multiple monitors the output of brightness -l looks like this:

display 0: main, active, awake, online, external, ID 0x42c0141
display 0: brightness 0.146914
display 1: active, awake, online, built-in, ID 0x4280a80
display 1: brightness 0.186523

So, not much harder to parse when you stick to the first listed display.