KMET: Edit KM Objects as Text (Search & Replace)
Requires KM 10 or higher
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:
- Delete the existing macros (or the group), if any.
- Unzip and double-click the .kmmacros file to install.
- 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:
Â
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
-
In the KM editor, select one or more actions, macros, or groups.
-
Run the macro [KMET] Edit Selected Objects as JSON Text (or [KMET] Edit Selected Objects as XML Text).
-
Make your changes in the text editor.
-
Copy the modified text to the clipboard.
-
Back in the KM editor, use [KMET] Paste Objects into KM to paste it back into KM.
-
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:
Â
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.
Â
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:
Â
This is what it looks like in JSON:
Â
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:
Â
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!