Copy, Paste, and Batch-Delete Triggers

This is a set of three KM8-only macros that makes working with triggers easier. The first two enable users to easily copy triggers from one macro and paste those triggers into all currently selected macros, and the last one enables mass deletion of triggers from all currently selected macros.

Copy, Paste, and Batch-Delete Triggers.kmmacros (8.5 KB)

These macros make it easy to make a group of macros create a conflict palette, or mass-convert a series of text-based macros to support the new clipboard trigger (my own motivation for creating these. By way of example, these macros enabled me to turn duplicates of these text-processing macros:

with multiple triggers like these:

into these:

I'm sure there are other ways these can be useful, so I hope others here are able to benefit from them. And thanks, @peternlewis, for making this kind of thing possible with the enhanced scripting capabilities in KM8!

More Screenshots:

Copy

Paste

Delete

7 Likes

Wow! Excellent work! Thank you!

1 Like

Trying and failing to modify to select and copy only one trigger. Any idea how I’d accomplish this?

This was my rudimentary attempt passing the selectedTrigger variable from a Prompt User action:

set TriggersList to {}

tell application "Keyboard Maestro Engine"
	set selectedTrigger to getvariable "selectedTrigger"
end tell


tell application "Keyboard Maestro"
	set MacroTriggers to every trigger of item selectedTrigger of (get selection)
	repeat with TriggerXML in MacroTriggers
		set end of TriggersList to xml of TriggerXML
	end repeat
end tell

TriggersList as text

In my tests, the current copy macro works fine with macros that have only one trigger. Do you mean copying a particular trigger from a macro with multiple triggers? Because that is admittedly a use case I did not account for :sweat_smile:

At any rate, the AppleScript for grabbing the XML of a macro with only one trigger is considerably simpler, so hopefully this will help you modify the macro copy to incorporate the user prompt:

tell application "Keyboard Maestro"
	xml of trigger 1 of item 1 of (get selection)
end tell

If you are talking about grabbing a particular trigger from a macro with multiple triggers, one easy way to do it would be to just use the trigger’s number in the above AppleScript (i.e. trigger 2 instead of trigger 1. You can arrive at that number fairly easily just by counting the triggers from the top down. Of course, the real trick would be entering the number of the trigger you want in a user prompt and then having KM enter that automatically so you don’t have to modify the script each time, but unfortunately I don’t have time to figure that out at the moment. If this is what you meant and you haven’t figured it out in a day or so, I should be able to take a crack at it.

And thank you so much for the kind words, both here and in the other threads! I’m glad these macros are already proving useful :smile:

That is what I mean, and what I'm attempting to do with that snippet I posted. Doesn't work so far though, not sure if I have to make further modifications, or maybe modifications to the Paste macro.

I've tried moving the selectedTrigger AS variable around (every selectedTrigger of item 1) etc., no luck so far.


Edit 1:

set TriggersList to {}

tell application "Keyboard Maestro Engine"
	set selectedTrigger to getvariable "selectedTrigger"
end tell

tell application "Keyboard Maestro"
	set MacroTriggers to trigger selectedTrigger of item 1 of (get selection)
	repeat with TriggerXML in MacroTriggers
		set end of TriggersList to xml of TriggerXML
	end repeat
end tell

TriggersList as text

Is what I'm trying now, but getting an invalid key form for "trigger selectedTrigger"

Also how do y'all do that fancy color formatting?

Ok, @gglick I've got it now. It's not optimized, as I'm not certain what I could remove without breaking it, but it works. There's no checks at all for leaving Copy Trigger empty, or if you've got a macro selected vs. action or macro group, etc.

Triggers - Copy SPECIFIED Trigger from Currently Selected Macro.kmmacros (27.8 KB)

Glad you got it working, @cfriend! Unfortunately, for whatever reason, your macro didn't work on my system, but fortunately, I was able to modify it so that it did. In my experience, the AppleScript was reading the value of the Copy Trigger variable as text instead of an integer, so it failed when telling KM which numbered trigger to copy (because it didn't get that the number from the KM variable actually was a number) but after I made sure that selectedTrigger was being read as an integer, that fixed it. Here's my modified script:

tell application "Keyboard Maestro Engine"
	set selectedTrigger to getvariable "Copy Trigger"
	set selectedTrigger to selectedTrigger as integer
end tell

tell application "Keyboard Maestro"
	xml of trigger selectedTrigger of item 1 of (get selection)
end tell

and the macro:

Triggers - Copy SPECIFIED Trigger from Currently Selected Macro - 1.1.kmmacros (3.5 KB)

As for how to get the color formatting here, just write your scripts like this:
`````AppleScript
[SCRIPT]
```

1 Like

All KM Variables are TEXT only, regardless of contents.
So, when AppleScript reads a KM Variable into a AppleScript Variable, it is TEXT.

THANK YOU

Unless something has recently changed in the Forum Software, "AppleScript" needs to be in lowercase.

###How to Put Script in a Forum Code Block
Just insert your script between the lines with the triple backquotes:

 ```applescript
 -- Your Script Here

If your script is another language, like JavaScript, then use the keyword in lower case for that language, like:
`javascript`

Here is a macro that will paste the script on the clipboard into the forum in the proper format:
####[MACRO: KM Forum -- Paste Script Block](https://forum.keyboardmaestro.com/t/paste-script-block-in-km-forum/4047)

:+1:t2:

That explains it! Thanks for the clarification :slightly_smiling_face:
That probably has some relation to why I wasn't able to use

set selectedTrigger to getvariable "Copy Trigger" as integer

and instead had to use

set selectedTrigger to getvariable "Copy Trigger"
set selectedTrigger to selectedTrigger as integer

And yes, you're of course quite right about ```applescript
[SCRIPT
```
requiring lowercase. I dashed that post off in a hurry this morning and didn't notice Typinator auto-casing "AppleScript" (something that is usually helpful, but occasionally backfires) so thanks for sharing your script block macro! Looks like I'd be much better off using it if I'm going to keep posting more scripts here :sweat_smile:

2 Likes

thank you for a very useful macro.

  • how to I delete all triggers in a macro group ?
  • which macros are affected: solely the ones in the macro group which contains the highlighted macro ? It would be a disaster if triggers were changed in all macros
    thanks again

Thanks for the kind words. I'm afraid your questions aren't quite clear to me, but for the first one, if you mean all the triggers of every macro in a macro group, the easiest way to do that with these macros as they exist now is to just select every macro in the group and run the Delete Triggers macro. As for the second question, again, it isn't quite clear what you mean, but regardless, you can Command-Z to undo any changes these macros make, so I would encourage you to just give them a try first and see what they do for yourself. Maybe copy the macro group in question and experiment on the duplicates in there first if you're really concerned about the possible consequences.

1 Like

Great. Thank you very much. You are delete trigger suggestion answers my question. Thanks again. Thank you

1 Like

Absolutely super cool! Thanks Gabe.

1 Like

Hello Gabe, Great macro thank you. I use it a lot. I am in the midst of a major reorganization of my macro groups and triggers, reason being that KBM crashes 90% of the time when I call up a conflict palette (KBM 9.06, Catalina 10.15.6 supp - I tried everything to no avail; Mac works perfectly otherwise)
So I have to do away with all conflict palettes.
My question: is there any way to modify/create a script that would set the trigger to the first letter of the macro name ? It would save me a lot of time. I can do adjustments after that.
thank you !

Glad the macro is proving useful for you. So if I understand your question, you basically want conflict palette style behavior without the conflicting triggers? In that case, is there any reason the Show Palette of Macros action wouldn't work, since the palettes it generates work the same as conflict ones?

Show Palette → mouse click is too tedious
One trigger per palette is fine.
I want to start off by assigning triggers to all macros in the palette, and after that I will change those triggers that are conflicting (same trigger in multiple macros).
So I go from no trigger → assign the first letter of the macro name (which is why I wrote to you) → after that, manually, I will modify duplicate triggers.
thank you Gabe !

You don't need to mouse click with the Show Palette of Macros action; it behaves just the same as a conflict palette does (i.e. it lets you type to select macros by the first letter in their names) only without needing to manually assign triggers first. Try this and you should see what I mean:

Show Palette of Macros.kmmacros (1.1 KB)

1 Like

I'm working on it. Not sure why you use a show palette of macros action instead of the usual macro group palette. I will figure it out.
thanks very much