Tip – Deleting a Dictionary Quickly

Digging around a bit I saw that you can delete a dictionary by setting all it's keys to null. Most examples show using a For Each with the Dictionary Keys collection to set each key individually to null.

There is a way to do the same thing in a single step. Use the "Set Dictionary to JSON" action and pass it an empty JSON structure:

image

The "Set" means to replace all the existing keys with the keys from the JSON, but there are no keys in the JSON (while still being valid JSON) so it effectively removes all keys, which causes KM to delete the whole dictionary.

Here's an example macro that will let you pick from a list of your current dictionaries and delete the selected one.

Remove Selected Dictionary.kmmacros (3.0 KB)

7 Likes

I've written a macro that lets me view, delete, display, modify keys for, and gets statistics on, all the dictionaries that I have. I think it's about time I post it, because I've been using it for months and it's delightful. My only regret is that my skill with Custom HTML Forms is weak so it doesn't look as appealing as it could. Maybe someone else can touch it up a bit.

2 Likes

That's great, but can you do it that easily from inside a Custom HTML Prompt action? I see how to set dictionary keys from inside the Custom HTML Prompt action, but I don't see a way to delete the whole dictionary. Perhaps KM is missing a way to set a JSON variable from its list of Javascript functions near the bottom of this page:

https://wiki.keyboardmaestro.com/action/Custom_HTML_Prompt

It also seems to be missing a way to Append and Prepend text variables.

How did you get the curly braces in there?

I reckon he typed them...

But where specifically are you referring to?

In the very top screenshot where he sets the JSON to {}. It doesn't look typed in:

https://forum.keyboardmaestro.com/uploads/default/original/3X/e/3/e34c0669b1befeae7873d8da47a1847f92c8fb0b.png

In that part of the Action where the {} is, anything can be typed in or pasted in, including those two symbols. The default Action Set Dictionary to JSON comes in with that field blank.

So, to make a similar Delete Dictionary Action for yourself you could just copy and paste {} or you can press Shift+[ and Shift+] to type them in. As you already have {} in your question I'm thinking I'm probably misunderstanding what you are asking... :grinning:

Sorry, I didn't realize that "{}" above was just typed in.

2 Likes

Various Keyboard Maestro Actions preview the result, and this looks like one of them.