Feature request: Trigger when displays awaken

I need to trigger a specific macro each time my displays awaken from sleep (not system sleep).

According to ChatGPT:

"Keyboard Maestro has a built-in Wake trigger, but it responds to the Mac waking from system sleep, not necessarily when a sleeping display turns back on. If your Mac Studio remains running while the displays sleep, this trigger won't reliably accomplish what you're asking. Keyboard Maestro doesn't have a dedicated Display Wake trigger."

If the above is inaccurate, please let me know! But if it's accurate, I'd love for this functionality to be added.

AFAIK there's no "display has woken up" event at the OS level that KM can use as a trigger. So you'll have to look for something else.

Give the "Display Layout Changed" Trigger a try -- depending on your monitor and setup that may work. If the monitor is also an USB hub the "USB Device" Trigger could be worth a look too.

Otherwise -- how quickly do you want this trigger to fire after the display wakes and what do you want the macro to do? You may be able to approach the problem from another direction...

Thanks for your reply.

I realize that AI can make mistakes, but according to ChatGPT, there is a "display has woken up" event, and it recommends an app called EventScripts to access it. ChatGPT wrote:

"EventScripts would trigger your existing Keyboard Maestro macro. You would need a tiny AppleScript, but you wouldn't have to duplicate any of your macro's actions.

Here's how it would work:

  1. EventScripts detects that your display has awakened.
  2. It executes a short AppleScript.
  3. That AppleScript tells Keyboard Maestro Engine to run your existing macro.

The entire AppleScript would be:


tell application "Keyboard Maestro Engine"
    do script "Your Macro Name"
end tell

You'd replace Your Macro Name with the actual name of your macro.

You would save this as a separate AppleScript file and configure EventScripts to execute it when the display wakes. Your Keyboard Maestro macro would remain completely unchanged.

EventScripts supports screen wake events specifically, independently of system wake events."

Again, I acknowledge that AI can make mistakes, but ChatGPT seems confident that EventScripts can do this. The $8 cost isn't prohibitive; I'd just prefer not to have a separate app doing something that KM might be able to do (in theory, if implemented).

I'm going to write the developer of EventScripts to confirm this functionality.

Lastly, to answer your question, there's a long-standing Finder bug (affecting some users) that causes desktop icons to move to different positions when the Mac restarts or reawakens. (I'm in Tahoe on this particular Mac, but the same bug occurred in Sequoia.) I have a KM macro that returns all icons to their correct position, but I'm tired of having to trigger it multiple times a day. So, I'd like to automate it so that the macro runs whenever the screens awaken.

As you said there was a system event for it, I just asked Claude to write a JS that would watch for that event. I then wrapped that in a polling macro that runs every second (update the timing as needed for your usage).

Download Macro(s): Activate on display wake.kmmacros (5.5 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 26.6.1
  • Keyboard Maestro v11.1.1

If the display wakes up, you see a text box with the wake time. That's where you'd put your actual macro actions (or call to launch another macro, etc.). If there's no wake event, the macro does nothing at all and ends in a handful of microseconds.

Just remember to leave the deletion step in place so the variable gets reset. In theory, I think this should work with local variables, but I didn't want to spend a ton of time looking at it.

I know polling's not ideal, but in lieu of a trigger, maybe this is good enough (and saves you $8)?

Disclaimer definitely applies: AI can make mistakes, so use at your own risk. But the risk level here seems low, as it just reads a value and sets a Keyboard Maestro variable.

-rob.

Thanks so much, Rob, for taking the time to put this together! That was really kind of you, and I really appreciate the effort.

While I'm sure this would work, I'd prefer not to have a script running continuously, polling the system every second. I'm hoping for a more native, event-driven solution, particularly since EventScripts apparently manages to detect display wake events without continuous polling.

So, while I appreciate the workaround, I'd still love for Peter to consider adding a dedicated "Display Awakened" trigger to KM. I think it would be a useful addition beyond my particular use case.

Again, many thanks for your generosity!

Understood, though polling like this is pretty trivial. 99% of the time, the script does nothing but fire, run two lines of code, then quit. I couldn't even detect it running on my 2023 Mac Studio, but I understand the preference for a trigger.

-rob.

Also, is there a specific cause, or set of causes, of making the display wake that is of particular significance?

I wonder how EventScripts itself might even work without polling?

It would seem the only alternative would be to hack MacOS' event chain to include an additional external call, which also seems..unlikely.

This is on the wish list already. Not sure when or if it will happen, but it is there.

Thanks!

There's a difference between an Event that is propagated by the OS, and an event that you can poll for.

There's also a difference between what is "Public"ly available and KM will use and what Apple terms "Private" but other software uses anyway.

But the above are general comments, since this particular request is...

...implying it's a Public, propagated, Event after all.

In the meantime -- I've never seen the Finder bug myself but remember some discussions that it may be the OS incorrectly registering a change in displays on every wake/restart. So do try a macro with a "Display Layout Changed" Trigger. It'll only take a couple of minutes to write and test:

If you have your system set to lock on screen sleep you could use the "Unlock" Trigger.

If those doesn't work I'd have no hesitation in using EventScripts -- it's lightweight and I never found it to cause any problems. Try the demo from their Downloads page. It has a bunch of other triggers that you might find useful but are absent from KM, and it is trivial to run a KM macro via AppleScript from EventScripts.