KMET: Edit KM Objects as Text (Search & Replace) Version 1.2

KMET: Edit KM Objects as Text (Search & Replace)

Requires KM 10 or higher

image

Version History

Click to expand

1.2a - You can use [KMET] Paste Objects into KM to paste in XML that was retrieved with the KM Editors "Copy as XML".
1.2 - Fix for not being able to paste Macros into the editor.
1.1 - Big Sur patch
1.0 - Initial Release

Download and Installation

Click to expand

KMET Edit as Text.v1.2a.kmmacros.zip (39.5 KB)

Installation Instructions:

  1. Delete the existing macros (or the group), if any.
  2. Unzip and double-click the .kmmacros file to install.
  3. Enable the Group and the Macros (if needed).

PURPOSE:

Click to expand

This is a set of macros that allows you to copy Keyboard Maestro "objects" (actions, macros, or groups) to the clipboard in text format (JSON or XML), edit them in a text editor, and paste the results back into KM.

This allows you to do things like search & replace variable names:

image

 
or re-order fields in a Prompt:

TEXT EDITORS:

Click to expand

The default text editor is BBEdit, but you can change it to use other text editors. See the macro [KMET Sub-Macro] Open Editor to Edit Object Text for more information.

USAGE:

Click to expand
  1. In the KM editor, select one or more actions, macros, or groups.

  2. Run the macro [KMET] Edit Selected Objects as JSON Text (or [KMET] Edit Selected Objects as XML Text).

  3. Make your changes in the text editor.

  4. Copy the modified text to the clipboard.

  5. Back in the KM editor, use [KMET] Paste Objects into KM to paste it back into KM.

  6. Most of the time you'll delete the original objects first, so you don't end up with duplicates, but that's up to you.

But what if I ruin my macros?

First of all, you're not modifying the actual source code of your macros. You're working on a copy of a small part of the source, which you paste back into KM when you're done, so if you mess something up, chances are it won't even paste back into the KM editor.

You can always "undo" your changes in the KM editor. And you can work on a copy of your macro, which makes it even less likely you'll mess things up.

As I mentioned earlier, I've been using these macros for around 4 years, and I've never come across anything that couldn't be fixed easily with "undo".

 
JSON? XML? WTF?

JSON and XML are ways to represent computer data in a text format. JSON is a newer standard, and much easier to read. XML is the native format KM uses for importing and exporting macros.

 
Which format should I use?

Use JSON, because it's more compact and easier to read.

So why did I bother to include XML? As I said, XML is the native format KM uses, so it's remotely possible that converting it to JSON could introduce some translation errors, although I think I handle any possible problems. Still, if a translation problem arises, you can use XML.

 
What are the "rules" for editing JSON or XML?

When you look at the object text, it should be pretty obvious it follows a specific pattern. Just don't deviate from that pattern.

 
What am I allowed to change?

Only change what you could change in the KM editor. For example, consider these actions:

image

 
Below, I've highlighted in yellow the only things you should change in the JSON. Compare it to the image above, and it should start to make sense.

image

 
So be careful, for example, if you wanted to replace a string like "Variable".

 
"Escaping" Text

There are certain characters in JSON that need to be "escaped". In the example above, notice the double-quotes in this action's description:

image

 
This is what it looks like in JSON:

image

 
Notice the "backslash" before each double-quote. That's called "escaping" the quote. The one at the end might be confusing - just remember that the entire string is enclosed in double-quotes, so there will always be a double-quote at the start, and one at the end.

To make it easier for you to escape strings, check out the various [KMET] Utility - Encode macros.

 
JSON Arrays:

One thing to be aware of in JSON is the use of commas in arrays:

image

 
In the above image, the red arrows point to a set of square brackets, which define an array. This array has 2 items in it - the one highlighted in yellow, and the one highlighted in green.

If you look at the white arrows, you'll see that there's a comma after the first item, but not after the last.

You always need a comma between each item in an array. But you don't need one at the end.

So one common problem arises when you copy the last item from an array, and paste it in somewhere, and forget the comma. Or you paste an item after the last item, and forget to add the comma before it.

OTHER USES

Click to expand

You can generate your own JSON or XML, and paste it into KM. In fact, that's what I do in my macro KMFAM: Favorite Actions and Macros.

You can use some of the KMET actions to help:

  • [KMET] Utility - Encode Text for JSON (Sub-Macro) (or the XML version)
  • [µKMET Sub-Macro] Set Clipboard to Objects Text
  • [KMET] Paste Objects into KM

TROUBLESHOOTING:

Click to expand

If nothing happens when you try to paste the objects back into KM, here's some possibilities:

  • Did you remember to copy the entire text from your text editor?

  • Did you select where to paste the objects into KM? It's just like when you copy and paste things in KM - it needs to know where you want to paste them.

  • Did you mess up the JSON or XML somehow? If in doubt, use a text comparison program and compare the original text with your modified text, and see if something looks obviously wrong. BBEdit has "Find Differences" under the "Search" menu that should be able to help. You can use the macro [KMET] Utility - Copy Selected Objects as JSON Text to get the original text from KM.

Have Fun!

16 Likes

Nice! And you mentioned BBEdit so double nice. :slight_smile:

My use case would probably be to clone a macro and point it at other files / directories.

I could also see use cases where I glean info from the XML and turn it into documents (like I do in sd2html).

Finally, for now, using BBEdit's diff function might also prove handy.

1 Like

Has anybody tried using this with Big Sur yet?

I get the following error message when using any of the edit or copy macros ...

I have tried just about everything to troubleshoot the problem and ultimately created a brand-new installation of Big Sur on an external drive and installed KM and the KMET macros. Even with this completely fresh setup, I continue to get the same error. It seems that I should have eliminated any potential software and operating system conflict/error issues. The only thing I can think of is that it is a Big Sur problem. Any help would be appreciated. Thanks!

I know why this happens. I'll post an update soon.

Do me a favor and test this for me:

Delete the macro [KMET Sub-Macro] Get Selected Objects as Text.

Download and install this:

KMET Patch.kmmacros (32.5 KB)

Let me know if it solves the problems. Thanks.

Hi @DanThomas,
I just downloaded your Patch macro and tested it.
I can confirm your Patch macro has fixed the issue!
Thank you so much for sharing it!
This instantly becomes one of my favorite macros!

Awesome, and thanks for letting me know! I don't use it a lot, but when I do, I can't imagine not having it. :grinning_face_with_smiling_eyes:

Yes, that did the trick for me too. Thank you!!

1 Like

A clarification on usage:

  1. I select an Action, Macro, or Group and run the KMET Edit as JSON or KMET Edit as XML and it opens BBEdit on a temporary file.
  2. I edit the file as needed.
  3. I then copy the edited version to the clipboard and run the KMET Paste with nothing selected. It will paste the object into whatever group is open, so if I have the KMET group open to run the Paste, it will get pasted into the KMET group.
  4. If I have the original group open when run KMET Paste, I'll have duplicate macro entries unless I changed the title while I was doing the edit, so I need to clean that up if necessary, at least by disabling the original version.
  5. At this point, the BBEdit file is completely unnecessary and can be closed without saving.

Have I got that right?

Pretty much. It's technically OK to have two macros with the same name, in the same group. The new macro will automatically get a new UUID and all that stuff. Of course, figuring out which was the original and which one's the copy is tricky. :slight_smile:

For me, when I'm just editing an existing macro, I edit the actions, not the macro. That way I can just delete the old actions and paste in the changed actions.

In my case, I was editing "Until any of the following are true:" with a list of images that could be matched. When I created the macro in a 2-monitor system, the option "In the front window's screen" was available and that's what I used. But when editing it on my laptop without an external monitor, only "all screens" was available. By editing the whole macro, I could see what the desired string was in the older options and could paste that into the newer options so that the whole macro would be consistent.

Totally makes sense. It sounds like you've got a good understanding of how things work.

Thanks. I'm already loving having these available.

1 Like

Could your macro automatically add a Comment as a prior action, as the first action in a macro, or as the first otherwise empty macro in a group, as appropriate, where the Comment would say "Pasted from JSON/XML by [KMET] Paste on 2022-0212 at 04:05:06"?

That would completely disambiguate anything while not otherwise affecting the function (except for Debugger displaying the Comment when it runs) (and except for trigger conflicts, etc.).

Well, I suppose it could, but it won't be doing that anytime soon. :slight_smile: You could always edit the title.

Yep. Title editing is even easier.

1 Like

If this macro isn't on the Best Macros List, it needs to be.

Dan, as always, outstanding job. I just discovered this yesterday and used it to change a ton of my DND variables from DND_whatever to DND__whatever so when I use them in a prompt the DND is hidden. It worked beautifully and allowed me to do in just 5 minutes what otherwise would have taken me several days.

Thanks again for always sharing your stuff!

-Chris

2 Likes

Thanks for letting me know. That's exactly how I was hoping people would use it. :slight_smile:

3 Likes

Hey, Dan.

I have this working with actions, but not with macros. To troubleshoot, I made a new macro with one Set Variable action only. Run [KMET] Edit Selected Objects as JSON Text, change the variable name from "Local__Text" to "Local__Text1" in BBEdit (no other changes), Select All, Copy, return to the same group as the original macro in KM Editor with no macro selected, run [KMET] Paste Objects into KM and wait for the magic to happen...

No magic.

I've also tried with xml. One thing that seems odd is if I copy an existing macro as xml, the Paste menu item in not available and I am not able to paste. However, if I copy an Action as xml, paste does work. I'm not really sure if I should be able to paste xml for a new macro, but I think so.

Any ideas?

I mostly only ever use it for actions, and it looks like you're right - I can't paste in a macro either. I suspect something changed in KM from the time I originally wrote this.

But I know what's wrong, so I'll see if I can fix it.

As for pasting in XML actions directly, yes, I noticed that too, and it's definitely a change in KM.

Anyway, give me some time, and I'll see what I can do. I'll look at it now, and if it looks like it's going to take too long, I'll let you know.

1 Like