Dictionary and Accessing Keys

I'm having some difficulty with dictionaries and accessing the keys to return a value. Here is my scenario:
I want to create a dictionary of musical instruments - dictionary name "partName_Abb." The key will be something like "1st Violin." The value will be "Vln1." This dictionary will be loaded with lots of instruments and I want KM to do one of the two ideas below:

  1. Get the name of the application's window (%WindowName%1%). This window CONTAINS the name of the instrument along with other info. I would like KM to search through the dictionary and find the correct key that corresponds to the name contained in the front window and type the dictionary's value into a "Save As" dialog box.
    So in the above example's dictionary: The window name would be "48548 - 1st Violin" I would need KM to search the dictionary to find the key word that is contained in this window name "1st Violin" then perform a "Save As" and typing the value "Vln1" in the dialog box.
  2. Look into the menus of the application where a list of these instruments are located and cycle through the menus where the menu item will be searched through the dictionary to find the correct key then the value will be typed into a "Save As" dialog box.

The only difference between the two options (other than window vs. menu) is that the menu has the exact name and the window has the name along with a filename, a hyphen and some spaces which I find difficult to extract to get JUST the instrument name.

#1 seems to be the most doable but I'm wondering your thoughts?

Is there a way to substitute "Violin 1" in the pix below with the value's result? It would great to be able to use a token here but I'm not sure which or if I'm thinking through this correctly (I tend to believe the latter).


FWIW: I've read as much as I can on KM Dictionaries and everything on this forum. Unfortunately the documentation seems 1/2 done for those of us just learning KM. If the main Dictionary Wiki page showed a complete example it would have saved me a ton of time wrapping my head around the way KM handles them.

Thanks for any help,

What application ?


I've been able to get the window's name by using Set Variable "WindowName" to %WindowName%1%. The problem is taking that info and use part of the result to see if it's within the dictionary. Thanks

Can you give the generalised form of the window name that covers every possible variation that you might encounter ?

Although, if possible, try and avoid using things like window names and menu items, which seems a rather unreliable quantity to rely on for information.

Have you had a look at the file located at:

I think that contains a list of all the possible instruments.

Hello CJK - Here is a pix of the window. Each will have the file name, space, hyphen, space, Instrument name.

I would love to avoid using windows or menu items but that is how the program works. There is no other way to grab the part name. As far as the xml resource file - that it for the program itself. It has nothing to do with the Instrument names that are created for clients. I need to create a specific list of file names that match what my clients use.

Maybe I'm not describing this well enough. I'll try again: In order to export a PDF file from the Graphic tool I need to view the actual instrument window in order to export a PDF via the Graphic Tool, NOT via the Printer because I need to make proper names for the individual PDFs. MakeMusic has failed to ever fix this problem with their program so I need to come up with a solution. So when I'm at the instrument's window I can export the PDF of that instrument via the graphic tool. When it brings up the dialog box to save the PDF file, KM needs to put in the correct Instrument name abbreviation that my different clients use which are not always the same - the reason I want to use a Dictionary or a file with each client's listings.

I hope that makes sense and thank you for your patience.

Replace the literal string value I assigned to Input with your token %WindowName%1%. The name of the instrument will be saved to the variable Instrument (or whatever you wish to change that to).

1 Like

Perfect! That is what I was looking for to move forward. I was going to investigate Regex tonight because I was sure there had to be a way. I really appreciate you taking the time to help out. I may revist this thread but I think this solves my issue. I'll update the thread if it works out and is solved.


Sounds like you have things under control. KM Dictionaries can be a challenge to understand and use. If you get stuck again, please post/upload your script/macro, and we'll try to help.

For more examples, you might search using the forum tag:

Good luck, and let us know how it goes.

Thanks JM for the link on the forum. Some of those I've read, others never came up in my searches. Of course, I spoke to soon of success (when will I learn). My problem with dictionaries is I have no idea how to retrieve the values to make a comparison to the script CJK helped with. It's been a frustrating 3 days and many wasted hours. When I have a script that seems to make sense to me and still is not working, I will post. I'm learning a lot but KM is a LOT more geekier than I expected. Cheers!

OK. I'm still not grasping how to compare a variable to a Dictionary key to then output the value so let me upload a simple scenerio. Let's say I create a Variable 1 that has the word "apple" and in a dictionary called "Fruits" it has a key "apple" with a value "red". I would like a script that searches the dictionary "Fruits" to find Variable 1's word "apple". Once it does, then it returns "red". I'm trying to do this with For Each Item set to The dictionaries or The dictionary keys but I cannot get the Variable 1 and the dictionary's key to equal. Thanks for your help.

Test Apples

Keyboard Maestro Actions.kmactions (3.8 KB)

Hi Steve,

The trick is to use the variable itself when referencing the dictionary. This modified version of your macro will display the apple key's value, red, when run:

Example Macro.kmmacros (2.7 KB)

1 Like

Hi gglick,

That worked well and has helped me get a lot closer to what I'm trying to do. Could you give me just a short explanation of what is going on in %Dictionary[Fruits,%Variable%Variable_1%]%
Are the two variables comparing each other by putting them next to each other? Any resources out there to discover this info on my own?


Hi Steve,

Glad that helped. The %Variable%[VARIABLE NAME]% syntax isn't comparing two variables, though I can see how it may have looked like it was given that the variable being referenced in that example is named Variable_1. In KM, this is just the syntax used to access a variable's value by the variable name. By comparison, a variable like this:


would be referenced with the same syntax like this: %Variable%Instrument% and would display the variable's value, "Violin 1". In other words, the %Variable% part at the beginning tells KM that the next word in the syntax is the name of a variable; you can think of it as a kind of prefix. When KM runs a macro that uses this variable syntax, it replaces the syntax with the value of the variable being referenced. Thus, when used to access a dictionary key, the syntax


changes to

%Dictionary[Instruments,Violin 1]%

which is in turn the syntax needed to access the value of a dictionary key named "Violin 1".

If you'd like to read more about variables themselves, I'd suggest starting with this page from the KM wiki, perhaps at this sub-section:


Thanks, Gabe. That is a very clear explanation. I did go to the wiki page but it wasn't as clear. Looks like everything is working perfectly in this macro - I'm done! Now what other mischief should I get into :slight_smile: Thanks again!


If this is the wiki page you're referring to:

Please feel free to make any suggestions to make it clearer, now that you have the answer.

Well.... I'm not really THE GUY who has the answers. The wiki just wasn't clear to ME. It may be clear to everyone else. Everyone here has a much better grasp on KM and I am just a "Newbie." I was just giving my experience from my perspective. I've been with groups where this was helpful and taken in the spirit of being helpful. If at some point I feel worthy enough to adjust the wiki, I will submit it to the group and see what everyone thinks. But right now...there is no way I would even try. I am way too green. Cheers and thank you all for a great forum.

Team, on the above example. Is there a way to retrieve the key's name instead of the value associated with the key?

I tried the above, but it is not working for me to look for the key since it is retrieving the key's value to match with instead of the key itself.

@edgarsalas You may be thrown off by the (incorrect) reuse of "Variable_1" for both the key to find (where he set it to apple) and then again in the For Each loop where it was set to the name of the key during that loop.

Whatever variable name you set at the start of the For Each block will contain the key during that loop.
Here is an example that might help.