Detect When macOS Wakes

I have a macro that dims the screen (I use a monitor so don’t need the laptop screen visible) but it only works about 50% of the time. It is triggered by 2 methods ‘At system wake.’ & ‘The system appearance changes.’. Because I sometime don’t connect a monitor, obviously I don’t want it to dim the screen in that situation, so that is ignored.


Put a "Pause" in as the first action -- it can take a moment after wake for all screens to be detected, so you might be testing too soon.

And don't forget that KM has a SCREENCOUNT() function, which will be more efficient than instantiating a shell. For example:


1 Like

Hi Nige_S

Thank you for suggesting to insert a pause, which I've done and I will let you know how the testing goes.

Your other suggestion, I never knew existed to check on the monitors and will try that later if my version still doesn't work.

I am running latest macOS / Safari and as far as I can tell I have enabled all notifications on this forum, but I never get one. Who should I contact on the forum to try and sort this out?


So I added a pause but it made the situation worse. I then tried your version of the AppleScript and that fails to trigger as well. So the problem is detecting the laptop waking. My triggers are these but of you have any suggestions to improve the detection of waking up?

As always -- things will be easier if you post your actual macro rather than images of pieces of it.


Here is my macro as requested.

Dim Screen check - monitor & wake : sleep.kmmacros (8.0 KB)

So the first question would be "When do you want this macro to trigger?" Your choice of "At system wake" looks useful -- you open your laptop, it wakes up, detects the number of screens and reacts accordingly. But "appearance changes" is when you switch from Light to Dark Mode (or vice versa) -- do you actually want it to trigger then?

What other events should trigger it? I'm thinking you might sometimes plug/unplug your external while the laptop is still awake, in which case the Display Layout Changed trigger could be useful.

There's also a chance you may be moving too quickly -- you can close the laptop lid, plug in a display, open the laptop again and it won't have had a chance to sleep, so won't wake, so no trigger. It should respond to Display Layout Changed though, and you may find that adding that trigger is enough to solve your problem.

So the problem trigger is when I leave my laptop and after 3 minutes it 'goes to sleep'. To wake it I touch any key, this normally has to be repeated for the laptop and monitor to wake up.

Your other suggestions of plugging in USB et cetera would be useful to detect and wake up the screen.

Try the "Display Layout Changed" trigger -- in a quick test at home yesterday, an already-connected but asleep laptop will wake up, only see one screen (internal), but then a second or so later will detect the second. That second detection will fire your macro again and set your internal display's brightness appropriately.

Sadly "Display Layout Changed" trigger didn't make any difference. Do you want to me to try macro with only that trigger and delete the other two?

And yes the sequence is "then a second or so later will detect the second" monitor, but unfortunately it doesn't trigger the macro.

It's always good to start simple when troubleshooting. Try this, which works in every situation I've tried:

Display Test.kmmacros (2.3 KB)



Thank you for creating the macro. I have added it to my 'everywhere' group and will let you know how I get on. I have also added a sound so I can hear if it triggers.

I forgot to say I disabled my macro.

Unfortunately, your macro doesn't work.

Doesn't work how?

With it selected in the Editor, runs KM's "Interactive Help" from the Help menu. What does that tell you?

With your laptop open and awake, you logged in and the screen unlocked, and no external display connected -- plug in the external display. Count to 5 then in KM's Help menu select "Open Logs Folder". From the Finder window that appears, open the Engine.log in either Console or a text editor and scroll to the last entries -- is there one indicating that the macro was triggered?

I meant that your macro didn't appear to run.

"Interactive Help" from the Help menu just has empty page titled :- What can I help you with?

is there one indicating that the macro was triggered? Yes and it triggered twice:- 2023-03-31 16:31:15 Execute macro “Display Test” from trigger The Display Layout Changed
2023-03-31 16:31:46 Execute macro “Display Test” from trigger The Display Layout Changed

The you're likely just missing the Notifications (or haven't allowed them). Since it was triggered you should have heard your added sound, though. Change the "Display text briefly" to something more attention-grabbing, like "Display text in a window".

If it is triggering then there really is nothing much in the macro that can go wrong -- it'll show one dialog or the other, even if you have an error in the "If" condition.

For once I am one step ahead and changed to display text large and this time did see the message 'Only 1 display'. I will add the dimming code tomorrow and let you know the results.

1 Like


I have tried various combinations using your macro as you uploaded it and versions including the slight tweak large display text but none are triggered. I found I had 2 desktops in "Show Desktop" and deleted one but still cannot trigger when I 'wake' the laptop.