Visual Guide to Understanding Variable and Token [Tips]

One of the most commonly asked questions about Keyboard Maestro is the difference between Variables and Tokens. Understanding the difference is essential to using KM effectively and to prevent bugs from occurring.

One can find more information about them here:

In summary,

  • A Variable is used for storing data to be used later. Only variable can be referenced by %Variable% prefix, eg %Variable%myName%
  • A Token is used for getting the latest data from respective object (e.g. it can be Clipboard, Browser, KM Engine) at execution time.

The following picture with an example may help to explain the difference.

Hence, it can be seen that:

  • Variables get static data from its storage location (will not change unless set with another value)
  • Tokens get dynamic data (e.g. browser'sURL) from respective object (e.g. Chrome app) which may be changed at the point of calling.

This also answer why Variable can never replace Token and vice versa, because they exist for different purpose.


FAQ

Can we create custom tokens?

No. KM has to explicitly support the token as it needs to call information from supported object only (eg Browser app, Clipboard, Address app). Hence custom token is not possible.

What happens if we use Set Variable Action on token ?

The token being read-only is not set, but rather a global variable having the same name as the token is created instead. At the point of getting the value, the token will be called instead of global variable. It is best to avoid using token as name for variable as this is a source of bugs and confusion as the global variable persists in KM.

5 Likes

Thanks for writing this in part because of my recent confusion.

iampariah,
np, when I first started with KM, I too wondering why there is separate variable and token, and sometimes find it hard to distinguish between them (even though KM has a good documentation explaining them), so visualizing them helps a lot.