Dictionary Question - Renaming the key?

Hello Folks,

I understand how to get a value from a dictionary key using the Set Dictionary Value action. What I don't know how to do is rename an existing Key, from within Keyboard Maestro. I use @DanThomas's Dictionary Editor and that works fine. But I'm trying to automate the process from within Keyboard Maestro.

For example, from within a URL dictionary, change the key Microsoft to MSFT.

Microsoft - https://www.microsoft.com

to

MSFT - https://www.microsoft.com

Can anyone shed light on that, if it's possible? Or do I have to read that key and value, delete it and recreate it with the new key and old value? I've looked around the forum and couldn't find it.

I hope that makes since.

Thanks much!
KC

Yes, exactly. Once a Dictionary Key is set to no value both the Key and the Value are gone from the Dictionary. You can effectively change the name of a key with two Set Dictionary Actions. And as you have @DanThomas's Dictionary Editor you can confirm that the old key has been renamed to the new key with the original content still in it.

Change Name of Dictionary Key.kmmacros (3.8 KB)

Click to Show Image of Macro

2 Likes

Another thing I do with dictionaries is SWAP two values. If I have two regular variables A and B, I can swap them like this:

C=A
A=B
B=C

You can do the same thing with dictionary entries. In the case of A and B, you would change them to dictionary references, while C could remain a regular variable (probably LocalC, to make it a local variable.)

The reason I'm swapping dictionary entries, if you are curious, is that I have a dictionary which stores the text values of buttons on my Stream Deck (actually, I'm using my iPad as a Stream Deck device, which is much better in my opinion.) By swapping two values I can have the buttons swap positions.

1 Like

Thank you for your response @Zabobon. Your example matches how I was performing the task. I was just thinking there was a more direct way to perform the task.

Thanks, as always, for jumping in!
KC