How to Trigger a Macro Once – IF Mac is On During Certain Period?

I have a macro that triggers at specific time of the day. But if the Mac is not ON during that specific point in time, it won't trigger.

I was wondering if we can expand the time at which it can trigger.

For example:

The macro currently triggers at 10:30 AM daily – is there a way to get it to trigger once any time between 10 AM to 11 AM whenever the Mac is ON?

Correct. Keyboard Maestro cannot run while the Mac is off or sleeping (usually when its sleeping, but “sleep” in macOS is a very complex topic).

Yes.

Have the macro triggered using the Periodic trigger in the range of time you want it to possibly run. When it runs, note the date (perhaps use the ICUDateTime token or the DAY function, there are various ways.

If the date is different to the last time it was run, then remember that new date in a variable and run the macro.

Convention is to use a variable name that starts with DND_ (eg DND_Whatever Macro Run Date). DND stands for Do Not Delete.

Keyboard Maestro global variables are permanently stored.

1 Like

Hey Peter – thanks for chipping in.

At first, I thought I would not be able to make such a macro by myself. But I tried and ended up making this. Is this something that you were suggesting? Would this macro work flawlessly?

Test macro.kmmacros (24.3 KB)

1 Like

Hey @fluid_eye,

You can do something like that, but I would generally prefer to use numbers that are available to calculations rather than text – i.e. %LongDate% – which is not an actual date but a piece of text.

This is probably the least complicated approach I'd take:

WhatsApp Periodic Macro v1.00.kmmacros (10 KB)

Macro Image

Keyboard Maestro Export

Here's another example of a macro that uses date math in a somewhat different context:

How to Run a Macro Weekly and when the Mac is Unlocked - #3 by ccstone

-Chris

Here’s another example. I use this method for some of my backup macros, to prevent them from running more than once a day.

Run macro every 5 minutes only if not run in the last 24 hours.kmmacros (4.4 KB)

Macro screenshot (click to expand/collapse)

2 Likes

Yes, that looks fine. The Break From Loop and Cancel This Macro actions are not necessary unless you plan to put other actions after the If action.

@ccstone’s concern about using %LongDate% are probably unfounded, the only issue would occur if say the date format changed for some reason (for example, you change the date format in your System Preferences), then the macro would run a second time. If running the macro a second time would be catastrophic, then that would be an issue to take seriously. Otherwise, it's such an unlikely occurrence that it could probably be ignored.

But for safety, instead of %LongDate% you can just use %ICUDateTime%yyyy-MM-dd% in both locations.

Hi there Christopher,

I was just going through the macro you shared.

I wonder what would happen on 1st date of any month?

Can you shade some light on, in what sense it is advantageous to use numbers instead of text?

Can you think of any limitations of the text format if I we have to modify this macro for any other slightly different use case?

For your specific purpose here I don't think there's a downside to using text.

But – to do anything more complex using date-math you will want to familiarize yourself with the date functions.

1 Like

Thank for sharing the macro here. I am going to use it for a different use case now, and I'm curious,

  1. Is it okay to have 0 actions in 'otherwise execute the following actions'?

  2. What is the difference between having end user actions inside 'otherwise execute the following actions' vs the way you have kept i.e. outside?

Yes, this is perfectly fine.

No real difference. I prefer placing the rest of the macros actions outside to avoid having the entire macro nested in an if/then/else action.

1 Like