Macro Help: If [X] happens between 7am - 11am

What’s the best way to test if something happens between certain hours?

For example, let’s say I want to make a macro that checks to see if I am using Tweetbot between 7am and 11am.

I can do this with a shell script, like this:

#!/bin/zsh -f
# Purpose: check to see if the current time is between 7am and 11am (10:59 am, technically)

zmodload zsh/datetime

HOUR=`strftime "%H" "$EPOCHSECONDS"`

case "$HOUR" in	
	07|08|09|10)
		echo yes
		exit 0
	;;

	*)
		echo no
		exit 1
	;;

esac


exit 0

but is there a way to do it in Keyboard Maestro without needing an external script?

What about using the trigger “Periodically while logged in”?

Trigger: Periodically while logged in
         Repeating every 1 hour
         Between 07:00 and 11:00
1 Like

I think my question might not have been very clear. Here is what I am doing so far

(note: the 711.sh is the script I posted in my initial post:

The result is that if I launch Tweetbot, or if I switch to it (i.e. if it was running before 7am and then I cmd+tab to it), it will run the script and check the time. If the script is run between 07-10 a.m. it will return 'exit 0' and then Keyboard Maestro will quit Tweetbot (and launch OmniFocus, which is probably what I should be using anyway!)

I'm not sure if there's a better way to test the "IF" part of this. Does Keyboard Maestro have a way to tell if it is between 7:00 a.m. and 11:00 a.m.?

Simply you can use the “Calculation” condition. I don’t know how I can merge those conditions together, but anyway, this can work. HOUR() can get current hour. If current hour is 7, 8 , 9 or 10, this will return true. Hope it might help.

1 Like

If you have a look at the wiki for “hour”, it will show you the HOUR() function which you can use in a calculation like this:

7 <= HOUR() AND HOUR() <= 11

which will return true for times between 7:00:00 and 11:59:59

3 Likes