Almost everyone needs to display variables from time to time. This post lists a variety of ways to get the job done. There are quite a few ways, and this post is written as a list of ideas for new Keyboard Maestro users. But some "old hands" may find a few novel ideas in here too. I'm happy to edit this if errors are spotted.
The most obvious way is the Display Text action. It works like this. Notice the [T] in the right corner. This means you can enter text in the box, including tokens.
The most important tokens to be aware of are:
%Variable%MyVarName% displays the contents of the variable
%Variable%MyVarName[arithmetic]% the Nth item in a list of numbers in a variable separated by commas
%Variable%MyVarName[arithmetic]:% Same as above, but items are separated by the specified character
%Calculate%arithmetic,arithmetic% any list of calculations, including variables or functions
Note that the Display Text action allows you to specify three different display styles:
- The first item displays the text in a pop-up window that requires the user click on OK to close. This window keeps itself in front of all other application windows. This is useful if the data you are displaying is more than two lines long. The window lets you scroll through very large output.
- The second item displays the item briefly in the upper right corner of the screen. It's not very useful if you have much more than three lines of output or your output contains long lines of text.
- The third item displays the text as large as possible, filling up most of the screen, and decreasing the size of the text until it all fits on the screen (up to a point. I wouldn't recommend trying to display over 100 lines of text this way. By default the duration of this option is about 6 seconds long, but you can change the duration using this KM action:
None of the three options above will pause the program's execution. But sometimes for debugging purposes you want to display some data AND also pause for the user to press OK before the program continues. If that's what you want, consider this example of displaying output, using the Alert action:
In that action, you can insert text that you want to display in EITHER of the two white boxes, as they are both valid text boxes for output. You can also choose to play a sound. The first box has the extra advantage of being printed in a larger font. Here's the output of the above action:
In many cases, the above action can be more useful than the Display Text action for displaying text.
Also, if you have an image that you want to display, you can use the following action. It can display either text or graphics, and you can specify which clipboard you want to display. However it doesn't pause the execution of the macro, so you might want to follow it with an ALERT action to give you a chance to pause.
There's a little variation on the Display Text action, and it is the Notification action, as shown in this example. It doesn't have all of the display options of the Display Text action, but it looks a little nicer, and it supports a sound effect.
Here's another way to display variables. Enter this:
That will bring up the Debugger Window, and if you haven't already done so, click here:
When you click on it, all the variables (both local and global) that have been accessed in the running macro at least once will become visible in the Debugger, like this:
You can resize this window, but it does have its limits.
The Execute a Shell Script action has the same options as the Display Text action previously discussed, but it allows for variables to be manipulated before being displayed, using shell commands. Although there are hundreds of possible shell commands, perhaps this one is illustrative of what can be done:
The "tr" command, above, removes all newlines and replaces them with spaces. This can be useful when trying to squeeze as much data as possible into the pop-up window that this command produces. Sometimes when I want to squeeze as much data as possible into that notification, I also strip out all vowels. Like this:
When you do this, you can squeeze a lot more information into that popup. Mind you, you're missing the vowels, but I've used this and it has worked for my debugging purposes.
In addition to displaying messages on your Mac's screen, you can also send messages to your iPhone or Apple Watch if you are away from home. To do that, this is probably the preferred method:
The Prompt for User Input action also works a lot like the Alert action, and has options to customize the way variables are displayed. It even has a mechanism to allow users to modify the variables (I won't explain that here.) Although it looks like it supports variables that are only a single line in length, it actually supports multi-line variables if you are sneaky and smart. And it has some very interesting options for visual access, like a slider bar for your variables. Here is the action, followed by how it looks:
For a multi-line variable, you may want to consider this action for its unique flavor. This action is designed for input but it can be used for output. Here's an example of the action followed by its display: (The main problem is that it displays the lines in alphabetical order, but sometimes that's perfectly acceptable.)
Sometimes you may want to get your program to speak the values of variables or calculations. That can be done like this:
You can also write your variables or expressions into the KM Engine log file, like this:
This isn't a complete list. There's also the Custom HTML Prompt action, but that's a really big one that would take dozens of pages to explain well.
Let me also add a link to a utility that lets you inspect variables (including dictionaries, which is something I haven't discussed in this post.)
So that's all for today. I'll append to this post if I come up with other ideas.