Trigger for Attaching Second Monitor

I have a macro which moves a variety of apps to my second monitor. How can I trigger that macro automatically when the second monitor gets attached to my mac?

I don't think there is a Trigger for that.

I would imagine that this does not happen that often, so I would use either Trigger Macro by Name action or hotkey trigger.

I have a keyboard shortcut assigned to the macro, but an automatic trigger would be nice...

BTW, I have played with the Periodic trigger and worked out a "trigger macrco" which sort of works:

Keyboard Maestro 8.2.4 “Second monitor attached/detached” Macro

Second monitor attached-detached.kmmacros (5.6 KB)

That's probably the best to come up with, right?

Yes and no. It is generally not a good idea to have high frequency periodic triggers. For one thing, it will be a drain on your CPU and battery. For another, any triggering of a macro will cancel “for one action” palettes, such as the Conflict Palette or Prompt with List and such.

Both are probably issue enough to want to avoid this sort of high frequency macro.

Thanks Peter. Yes, I have noticed that the periodic trigger interferes with my macro palettes. :frowning:

Anyway to avoid this? Could I call the Second monitor attached-detached macro from a shell script with a never-ending loop?

Calling the macro from the loop would be the same as triggering it.

You could have a shell script that repeatedly asked the Keyboard Maestro Engine to evaluation the calculation of the SCREENCOUNT function, and then triggered the macro when that changed. That would not trigger a macro until the change, so that would work better.

Thanks. I found out that I can check the number of attached monitors in an AppleScript via

tell application "Image Events"
    set displayNum to count of displays
end tell

So this is probably the preferred option. Still needs a never-ending script which does the check in a loop, but with a 1- or 2-second wait between checks it shouldn't be too taxing on the system, I hope.

Nope. That is no different from counting the screens in KM or AppleScript.

This is a bad idea. How many times a day do you connect/disconnect monitors? Are you really willing to pay a significant performance penalty on everything on your Mac just to automate this one, very simple event, that can be very quickly triggered by a hot key???

The key to automation is to spend your time automating the things that you do many times a day, OR, is very difficult to do otherwise.

But, you're the master, and KM is the slave. It will do whatever you tell it, even if it is not in your best interest. :wink:

I just want other readers of this thread to be aware of the price.

Good point. So my question is: How high is the price? How much system load gets used for a never-ending loop with a 1second wait? And is it better to use a bash or python script than an AppleScript? Or is it the same?

Sorry, but this approach has such little ROI, that it is no longer worth me spending time on. I've given you my best advice. It is up to you to do with it what you will. The last question you asked takes time and research to answer. If you really want to know, then you will need to do the research/testing yourself.

Just ask yourself this question: Is this how you want to be spending your time? Do you have any other Mac workflows that need automating?

Good luck.

A quick Google search found this:

But it seems you can only schedule it to run every minute.

1 Like

If it is a desktop, probably not much.

It is it a laptop, then it may have a significantly negative impact on your battery.

It may also have a sufficiently negative impact on your performance that you notice it - this is more likely on a laptop generally too since they tend to have less performance to spare.

Just a follow-up after half a year of usage:

I have an AppleScript running in the background which every 2 seconds checks whether the number of monitors has changed (and if so, then calls a KM macro).

I cannot find any impact on the performance of my iMac and on my MacBook it *has some impact on battery life I guess, but not really much.

So I am quite happy with it. It is quite magic to see when I attach my iPad as 2nd display all the apps I have dedicated to the 2nd screen wander over to that screen by themselves - every time again.