Is it possible to check presence of dictionary at all and later presence of selected key?
I see conditions in if related to variables but Dictionaries seems to be different entities?
Is it possible to check presence of dictionary at all and later presence of selected key?
I see conditions in if related to variables but Dictionaries seems to be different entities?
What do you mean "check presence of dictionary"? You can retrieve the contents of a dictionary using this token:
https://wiki.keyboardmaestro.com/token/Dictionary
If you use that token, you can retrieve the value of the dictionary for the key that you specify. Clearly the dictionary "exists" if that token returns a value for the key.
Normally, what a programmer wants to do is check if a value corresponding to a given key in the dictionary exists or not. If you really are trying to determine if an entire dictionary is empty (why are you doing that?) it should be very easy. Just run this code: (but I don't see any reason for doing this.)
But you may be doing something wrong if you attempt to do that, because the concept "check presence of dictionary" is a really strange concept.
If you want to check if a value in the dictionary is empty (a more useful thing to be checking) you can do this:
Typically you may want to specify the key in a variable, in which case you would do this:
That looks awkward, with the nested "%" characters, but it works.
Any questions?
No - it is not strange concept. If you think about dictionary (kind of structure/object) in other programming languages) as set of properties and their values, it is possible that such object not exists yet because you run the application first time. After this pass you write set of values as defaults (possible after asking the user) for next runs until you don't change the defaults. Of course I can use global variables with additional suffixes in name, like AppDefault_path, AppDefault_last_run, etc. but more elegant is use Dictionary with key values.
It is normal and very popular construction in programming world (because dictionary/assoc-array/hashing-table is just one more object reference existing inside the variable - at least in dynamic programming languages).
But I understand that Dictionary is independent body from standard variable and their existence can't be checked, so I have switch to suffixes.
I'm not sure why @Airy's method wouldn't meet your needs? As step one in the macro, check the value of a known key in the dictionary. If it's empty, then the dictionary is empty/doesn't exist, and you can do your "first run" routine and write values to it.
If it isn't empty, then the dictionary exists, and you can continue, knowing it's in place.
I'm not a programmer by any stretch, but this seems to do what you want it to do—what case am I missing where finding an empty value wouldn't confirm an empty/non-existent dictionary?
If it's a dictionary that can sometimes have some empty keys, surely there's one key that will always have a value if the dictionary exists?
-rob.
I showed you how to check if a dictionary exists. I gave you the code that will check if a dictionary exists. Did you try the code? That's how to check if a dictionary exists.
OK OK 
I just have habits from other languages - it is just a bit "around the target", because I have to check all possible keys (which can change in the time) to get info if any of them is inside dictionary.
Lets say that in this case I'm a bit "spoiled"
Anyway thank you.
My code showed you how to check without using "all possible keys." Did you see my Break action?
Yes, of course break will finish checking.
Why I feel that you are irritated  - there are just habits from other languages as I said - and some thinking in "pythonic/objective way". This part with Text operation is good and probably fulfilled my requirements.
First looks was like thinking - conditions exists against variable, so way to compare text with dictionary reference was not obvious for me.
And one doubt yet - what if value for key is empty (aka string with length 0). Is that value still set or disappear? Because for me it is natural, that value for key can be empty, it is different than no value for key, because no key and also can be different if there is no Dictionary at all.
This is a common question by users. If a variable is empty is that the same as being non-existent? This has been discussed many times. The answer is that from a user perspective they are the same thing. There is no difference.
Not in KM. They are the same thing. Empty is no value.
If all keys are empty, there is no dictionary. That's what my code checks.
I don't think I'm irritated. I always try to answer questions politely. Sorry if you think I'm irritated. I have great respect for you as I remember you have answered many questions on this forum that I could not have answered.
At work they call me Sheldon because of my lack of social skills.
Do you mean to check whether a dictionary exists?
Not really, you would have to see if there are any keys in the dictionary or if there dictionary is in the list of dictionaries.
If a key exists within a dictionary?
Dictionaries, and key/values within them, like Variables are dynamic in Keyboard Maestro. If you empty them, they stop existing.
Is this really true?
Today I created macro which only set empty value to  global variable, hoping that it disappears, but looking into variables (in properties tab) I still see it (empty).
What am I doing wrong?
There are several places that a non-existent variable can show up as an empty variable, which may lead you to think that the variable "exists" when in fact it doesn't. For example the debugger can show variables as empty when in fact they don't exist. The fact that the KM Debugger can say "Myvar=" does not "prove" that the variable exists while it's empty. That's not a real proof. It's the same with the KM Editor. It may sometimes show a value as empty, but in the KM Engine, where the master copies of variables are stored, it's non-existent.
This is probably being done as a courtesy or convenience to you. And these aren't the only places.
Please understand that the master copy of all variables is in the KM Engine, which can delete empty variables, but you can't directly see the Engine, so there's no way I can prove that to you. You will just have to trust me. Secondary sources like the Editor and the Debugger are not authoritative.
P.S. What would you say if I could show that the Editor says a value is "empty" and the Debugger says the value is non-existent, or vice versa? Which one would you believe? Which one is the most authoritative to you? The correct answer would be that the Engine is the most authoritative source.
Variables that are empty no longer exist (in the variables file for example).
However, any variable that is used in any macro is know to the editor, so it will show them (for example in popup menus).
I'm not disagreeing with your that statement, (I have too much faith that you are always right!) but there are also variables that are UNKNOWN to the Editor that show up in the list of variables in the Preferences Pane. In the following example, the editor never sees the variable name "UnknownToEditor" in the following code (it's only in a data field), yet that variable, which is known only to the Engine, is displayed in the Preferences Pane too. At no point (correct me if I'm wrong) does the Editor ever see the string "UnknownToEditor" in the Set Variable text window. This happens only in the Engine. Am I right? And yet it shows up in the Preferences Pane.
Does the KM Editor get its copies of all KM variables from the variables file, or from a direct query to the Engine? As a user, the answer doesn't affect me, but it may help address the issue here.
The editor will list variables it knows about, which includes variables that have values and variables that are used in macros (that it knows about, not indirectly referenced variables, or variables referenced via AppleScript or whatever).
So in your example, the variable UnknownToEditor would become known to the editor when it had a value, and would become unknown again if it was set to empty.
Basically, there is no one definition of “variable exists”.
Note that you can also test for keys in JSON dictionaries,
which may prove a bit more tractable and flexible, in some respects.
See:
The "selected key" bit has been covered.
By extension, a non-existent Dictionary won't have any keys  So you can use a similar "For Each" trick:
 So you can use a similar "For Each" trick:
Does Dict Exist?.kmmacros (6.3 KB)
A longer-winded way would be to "For Each" the Dictionary Collection itself, checking names as you go.
If you want to be more like other languages and use an "If Dictionary exists then..." construct -- the "If" action can evaluate the output of a script, and AppleScript can check for Dictionaries:
Does Dict Exist (AS version)?.kmmacros (5.8 KB)
While more concise and possibly fitting your mental model better, unless you have an humungous number of Dictionaries this will be slower than the "For Each" method (AppleScript instantiation overhead). But we're only talking 10s of milliseconds, so go with whatever works for you.
But you can get close! The old-style AppleScript form of
tell application "Keyboard Maestro Engine" to return value of variable "Global_temp"
...will throw an error if "Global_temp" doesn't exist, so you could catch that in a "Try" action. But even easier is to simply ask the Engine:
tell application "Keyboard Maestro Engine" to return exists variable "Global_temp"
...which will be true if "Global_temp" exists, false if not.
That's very close. In fact, it's a polite way of saying I was wrong. You are just so good at coming up with ideas.