Setting a Trigger to a Macro via Another Macro

Hi all,

I was wondering whether there was any way to set a time trigger for a macro via another macro. Essentially, my use case is that I set my schedule for the following day the night before using a macro that breaks the day into blocks of time. I also use the Focus app to shut off my internet for certain intervals throughout the day, which can also be automated via KBM, but I need to do this separately from the daily schedule. It occurs to me that I can probably integrate this directly into my time blocking and automate the Focus app turning on (and its duration). The only difficulty is that I have a different start time on most days, so the triggering of the Focus macro needs to depend on the initial time I enter the day before. I could, of course, just set a new trigger each night for the next morning, but if there's any way to set this via the macro itself, it would be incredibly convenient. Just wanted to check!

Than you all again for your help. Such a wonderful community!

Yes, it is possible, but you've have to use a script (AppleScript or JXA) and the learn how to read/write XML. It's a fairly advanced use case. Let us know if you really want to proceed.

I not sure that it would be any easier to input in to a prompt dialog the time for the trigger than it would be to directly edit the macro itself.

Trgger XML

EDIT: Out of curisoity, I took a look via AppleScript at the trigger XML. It was easy to get (it's in a separate macro property). It looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>ExecuteType</key>
	<string>Time</string>
	<key>MacroTriggerType</key>
	<string>Time</string>
	<key>Repeat</key>
	<false/>
	<key>RepeatTime</key>
	<integer>60</integer>
	<key>TimeFinishHour</key>
	<integer>17</integer>
	<key>TimeFinishMinutes</key>
	<integer>30</integer>
	<key>TimeHour</key>
	<integer>8</integer>
	<key>TimeMinutes</key>
	<integer>30</integer>
	<key>WhichDays</key>
	<integer>16</integer>
</dict>
</plist>

Easy to change if you know RegEx or ASObjC dictionaries. Otherwise, it might be a chore.

But I still don't see the time saved vs just changing the macro directly in the KM UI.

Thanks so much for the response!

I wouldn't say I'm expert, however I'm at least proficient in both AppleScript and RegEx and have some familiarity in JXA. Very little knowledge of XML, however, but I think I can at least begin to mentally parse the snippet you've posted.

Here's the macro I have set up currently. As you can see, it uses AppleScript to create time blocks throughout the day based on an initial time. The scripts add the time to the initial time, including breaks when entered, in order to produce a daily schedule. So, all I really want to do is have the variable "Start Time" that I set through user input be entered automatically as the trigger for a second macro that will then automate Focus throughout the day according to these preset time intervals. Let me know what you think and thanks again for your help.

BigImage

Please upload the macro file so I can review.

I took a look at the code you provided and KBM’s scripting capabilities (I had no idea they had become so extensive) and think I’ve figured it out–and now also know that I can get the XML code from a macro and edit directly via AppleScript. So, so useful. Thank you again for your directions.

tell application "Keyboard Maestro"
	set m to macro "Testing Trigger"
	tell m
		delete every trigger
		make new trigger with properties {xml:"<dict>
	<key>ExecuteType</key>
	<string>Time</string>
	<key>MacroTriggerType</key>
	<string>Time</string>
	<key>Repeat</key>
	<false/>
	<key>RepeatTime</key>
	<integer>60</integer>
	<key>TimeFinishHour</key>
	<integer>7</integer>
	<key>TimeFinishMinutes</key>
	<integer>17</integer>
	<key>TimeHour</key>
	<integer>" & x & "</integer>
	<key>TimeMinutes</key>
	<integer>" & y & "</integer>
	<key>WhichDays</key>
	<integer>" & dd & "</integer>
	</dict>"}
	end tell
end tell

Depending on the desired granularity, you can probably just have a macro that is triggered every ten minutes, checks what the status should be and adjusts it as necessary.

So rather than changing your trigger time, or having multiple macros, or whatever, just have a single macro that fires every ten minutes and deals with whatever state changes are required at the appropriate time.

If you really need finder granularity, you can make it every five minutes or every 1 minute, but generally you don’t want macros firing more often than necessary, even if they do nothing, since a triggered macro will interrupt some “once” processes (eg Activate Macro Group for One Action will be deactivated by any macro triggered).

I use my computer and a macro using a time of day trigger as my alarm clock. There are occasions where I wish to reset the time of day and wonder if the XML referenced in this post is the only way to adjust the time of day alarm time using AppleScript and if additional assistance can be provided?

If you are constantly setting different times, then it is definitely possible to use the XML AppleScript Approach to change the Trigger Time on a Macro. You could enter the new Trigger Time in a KM User Prompt and feed the new Value through a Variable in the AppleScript. But this would probably be no faster to use than just opening your current Macro and adjusting the time trigger.

One way to speed that process up might be to have a hot key that opens up your Alarm macro in the Keyboard Maestro editor so that you can adjust its time quickly. That can be done with an AppleScript that quotes the name of the Macro:

EXAMPLE__Open Alarm Clock Macro for Editing.kmmacros (2.0 KB)

I was trying to think why this Alarm Clock question hasn't really come up before and realised it is because we all use our phones as our alarm clocks now :rofl:

Zabobon,

Thanks for responding. I was looking for a bit more assistance on the XML AppleScript approach, but I'll dig into it to see if I can figure it out. Yeah, I do have the same alarm time set on my iPhone as well. :laughing: However, my "Wake Alert" macro on the MacBook announces historical events associated with the date (i.e. birthdays and anniversaries of death), updates/presents the current day's Dispatcher schedules, and uses "type to Siri" to announce the day's weather forecast.

I'd "love" to be able to create an iOS shortcut that would adjust the iPhone's Alarm Wake Up time and then send the MacBook a remote web trigger to reset my "Wake Alert" macro's time of day trigger for the same desired time.

Here is how to change the Time of Day Trigger of a Macro to a specified time, with XML and AppleScript. The AppleScript is adapted from what @zvh posted above.

The difference in the version of the AppleScript below is that it is for the Time of Day Trigger and it uses Keyboard Maestro Local Variables for the the new Trigger Settings and the Name of the target Macro. This should give you the building blocks you need for:

Notes

  1. I've uploaded a Group which includes a Macro to set the time and an example Macro to have its time setting changed. You would adapt so the Macro's name matches whatever your Macro name is called (and it needs to be the only Macro named this in your Library).
  2. The Example Macro I uploaded has a second AppleScript Action which opens the target Macro in the Editor so that you can see the Trigger time is changed. This was just to check the script is working. Disable this Action if you don't want to open the target Macro.
  3. The AppleScript first deletes whatever Triggers the target Macro had before so use with caution and first try out on the EXAMPLE Macro I included so, you get relaxed with the way it works.

EXAMPLE Alarm Clock Setting using XML and AppleScript Macros.kmmacros (17.9 KB)

Click to Show Macro

Click to Show AppleScript
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
  set varMacroName to getvariable "LOCAL__Macro Name" instance kmInst
  set varHour to getvariable "LOCAL__Hour" instance kmInst
  set varMinutes to getvariable "LOCAL__Minutes" instance kmInst
end tell

tell application "Keyboard Maestro"
  
  set m to macro varMacroName
  tell m
    delete every trigger
    make new trigger with properties {xml:"<dict>
            <key>ExecuteType</key>
            <string>Time</string>
            <key>MacroTriggerType</key>
            <string>Time</string>
            <key>Repeat</key>
            <false/>
            <key>RepeatTime</key>
            <integer>60</integer>
            <key>TimeFinishHour</key>
            <integer>17</integer>
            <key>TimeFinishMinutes</key>
            <integer>30</integer>
            <key>TimeHour</key>
            <integer>" & varHour & "</integer>
            <key>TimeMinutes</key>
            <integer>" & varMinutes & "</integer>
            <key>WhichDays</key>
            <integer>127</integer>
          </dict>"}
  end tell
end tell
2 Likes

Zabobon,

I've only now gotten around to looking further into your proposed solution and its exactly what I was looking for. :laughing:

Many thanks!

1 Like