Find and Delete Macro Actions?

Is it possible to find and delete macro actions if so how?

I want to find and delete both actions, if possible where they are one after the other in the following sequence

It is indeed possible to find and delete macro actions, though I’m afraid I don’t know if there’s a way to do so only when a particular two are in sequence. That said, this AppleScript works well enough for me in limited testing, and I think you should be able to modify it to your needs fairly easily (just replace “Test” with the name of the macro group containing the macros you want to target):

tell application "Keyboard Maestro"
  repeat with i in every macro of macro group "Test"
    tell i
      delete (every action whose name begins with "Set Variable “Reason for SMS”")
      delete (every action whose name is "Pause for 0.05 Seconds")
    end tell
  end repeat
end tell

This script can of course be run from KM, but if this is a one-off operation, you may just want to copy it into Script Editor (or Script Debugger if you have that) and run/modify it from there.
If the script produces undesirable results, you can revert your macros to the state they were in before you ran it with a simple Command-Z.

If you’re feeling adventurous, or you’re sure you can narrow down the “actions to be deleted” criteria enough, you could also target every macro at once:

tell application "Keyboard Maestro"
	repeat with i in every macro
		tell i
			delete (every action whose name begins with "Set Variable “Reason for SMS”")
			delete (every action whose name is "Pause for 0.05 Seconds")
		end tell
	end repeat
end tell

But for obvious reasons, this is not recommended, Command-Z undo or not :slightly_smiling_face:

1 Like

I suspect that that will only delete actions that are directly within the macro, not actions that are nested into other actions.

2 Likes

Nice script.

You might be able to test for the sequence of Actions by getting the list of Actions for a macro. I was just going to post the additional scripting, but then found it a much more challenging task than I original thought. So, here's a complete script as an example.

One Note: Action Names are NOT necessarily an unique identifier of the Action. It is better to use the id property.

Here's a script that will delete the Actions ONLY if they are in the specified sequence.

updated 2017-11-03 17:47 CT

I have removed the script that was posted here because it contains a flaw and should NOT be used.
Please see the updated script in this Macro, which I just posted:
###MACRO: Delete KM Actions in Selected Macro by Name [Example]

2 Likes

I've just tested this and you're exactly right. Thanks for pointing this out! I've since managed to alter my script to also check actions nested one level deep, which I imagine should work well enough for most use-cases:

tell application "Keyboard Maestro"
	repeat with i in every macro of macro group "Test"
		tell i
			if actions of i contains actions then
				tell every action
					delete (every action whose name begins with "Set Variable “Reason for SMS”")
					delete (every action whose name is "Pause for 0.05 Seconds")
				end tell
			end if
			delete (every action whose name begins with "Set Variable “Reason for SMS”")
			delete (every action whose name is "Pause for 0.05 Seconds")
		end tell
	end repeat
end tell

but this is clearly something that needs its own recursive handler, and that's a bit over my head at this point (I don't know if it's obvious from the incredibly simplistic AppleScripts I post, but I've only been seriously learning it for about the past few months (and I also have zero experience with any other programming or scripting language) so I'm not exaggerating in the slightest when I say I'm still very much a beginner :sweat_smile:)

@JMichaelTX
Wow, thank you for the exhaustive example! I appreciate the kind words on my own meager script, but I have to say it looks positively neolithic compared to yours :sweat_smile:
I've gone over your script step by step in Script Debugger a few times now, and think I've (mostly) managed to wrap my head around how it works (though I feel like it will probably be a long time before I'm able to implement a similar level of complexity in my own scripts, let alone the wisdom to know when to employ it) so thank you again for this example, and the rigorous comments you employ throughout!

That said (and I'm aware this is a first draft, so this isn't a complaint!) I'm afraid it doesn't seem to cover two important aspects. First, as Peter pointed out above, it doesn't affect actions nested within other actions. If you (or any other AS experts reading this) can modify it to include the kind of recursive "if actions include other actions" handler I alluded to above, I would be very interested to see that.

Second (and I could well be wrong about this due to my amateurish understanding) it seems to expect exact action names, rather than allowing for different ones that start with a common prefix, which at least in this case would seem to defeat the purpose somewhat (since as I understand it, the ultimate goal here is to expedite removing an assortment of now-redundant actions, half of which all start with the same string but that have different end strings). Assuming I'm not wrong about this, how would one go about modifying this script to delete all actions that fall under that criteria?

Thanks again for sharing your knowledge. I'm always impressed by the sheer thoroughness of the solutions you post here :bow:

1 Like

I have just posted a complete Macro, with an updated Script, that should provide you with a good example of how to do this. You will need to update the Macro to enter your Action Names to be deleted. Please let us know if this answers your question.

###MACRO: Delete KM Actions in Selected Macro by Name [Example]

##notes

  1. This works ONLY on the currently selected Macro
  2. It searches ONLY the top-level Actions.
  • So, for example, Actions in another Action (like a Group Action) will NOT be searched.
  1. I strongly recommend that you backup your macro BEFORE running this macro.
  2. The function that searches for the match of Actions is complicated.
  • While I have made a number of tests, it is possible that it could fail
  • I think the failure would be most likely not finding a valid sequence of Actions, but I can't be sure
  • If you observe any unusual results, please discontinue use immediately, and notify me immediately by PM.
1 Like

Thank you all for your contributions, many of the actions are inside IF actions or Repeat actions, is there a way to delete inside from these by searching all macros?

Yes there is. I’m working on a script now that will drill down through all actions in a macro. But it is quite complicated, and may take a few days to work out.

Many thanks, I wouldn’t want you to spend a few days on it unless you love solving problems :slight_smile:

For me to continue my personal development with KM how can I find out which KM actions I can trigger in AppleScript? I’m sure many other non-programmer users too would like to know the answer to this question.

Do you mean trigger "macros"? Actions cannot be triggered.
Of course, you can put any action in a macro, and then trigger the macro.

See Controlling Keyboard Maestro Engine via Scripting (KM Wiki).

1 Like