List (Display) Dictionary Contents

Is there a way to list out (display) all the keys and values of a given dictionary?

(And list out all the current dictionaries?)

I would also like to be able to delete a particular key and delete a particular dictionary.

Hey John,

See the Dictionaries user manual section of the Keyboard Maestro Wiki.

Here's how to extract key names and values as a text list to a pair of Keyboard Maestro variables without having to iterate through them using a Dictionaries collection:

List Dictionary Keys and Values to KM Variables v1.00.kmmacros (2.5 KB)

AppleScript Code for Macro Above
------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2019/06/21 16:51
# dMod: 2019/06/21 16:51 
# Appl: Keyboard Maestro
# Task: List Dictionary Keys and Values to KM Variables.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro_Engine, @List, @Dictionary, @Keys, @Values, @Variables
------------------------------------------------------------

set dictonaryName to "Your_Dictionary_Name"

set AppleScript's text item delimiters to linefeed

tell application "Keyboard Maestro Engine"
   
   tell dictionary id dictonaryName
      set keyList to name of dictionary keys
      set valueList to value of dictionary keys
   end tell
   
   setvariable "keyList" to (keyList as text)
   setvariable "valueList" to (valueList as text)
   
end tell

------------------------------------------------------------

Run these AppleScripts one at a time in the Script Editor.app and examine the results.


# Get Keyboard Maestro Dictionary Objects.
tell application "Keyboard Maestro Engine"
   set dictList to dictionaries
end tell

# Get Name of Dictionaries.
tell application "Keyboard Maestro Engine"
   set dictNameList to name of dictionaries
end tell

# Delete a Dictionary by Name.
tell application "Keyboard Maestro Engine"
   delete dictionary "Your Dictionary Name"
end tell

# Delete a Dictionary Key by Name.
tell application "Keyboard Maestro Engine"
   delete dictionary key "KeyName_01" of dictionary "Your Dictionary Name"
end tell

-Chris

1 Like

Thanks so much. I'll try these out. I did look at the manual on Dictionaries but couldn't figure out how to do these tasks.

Hey John,

That's not too surprising. Some of those wiki and manual entries are hard to figure out.

-Chris

Dictionaries can be hard to deal with. The below Macro will list the Key and Value for all of your Dictionaries using the first Group, and for one specified dictionary using the second Group. The second Group uses the same For Each on Dictionary keys.

You will need to replace the Dictionary Name in the second Group with one of your Dictionaries.

Example Output

image

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   List All Dictionary Key Values [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/4/8/4831cf3e38af79500c26fbe980963c599258c027.kmmacros">List All Dictionary Key Values [Example].kmmacros</a> (14 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

Author.@JMichaelTX 

**PURPOSE:**

* **List All Keys & Values for All Dictionaries**

**HOW TO USE**

1. First, make sure you have followed instructions in the _Macro Setup_ below.
2. Trigger this macro.

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  ??
.
1. Assign a Trigger to this maro.
2. Move this macro to a Macro Group that is only Active when you need this Macro.
3. ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
   * Set Variable to Local__DictName 
     * in the second Group

**REQUIRES:**

1. **KM 8.2+**
2. **macOS 10.11.6 (El Capitan)**

TAGS:  @Dictionary @Key @Value @List

USER SETTINGS:

* Any Action in _magenta color_ is designed to be changed by end-user

ACTION COLOR CODES

* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions


**==USE AT YOUR OWN RISK==**

* While I have given this a modest amout of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

---

![image|629x1967](upload://hUag3p8VUl27S64gZ2v5beyU0EG.png)

`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

### Questions?