Suggestion: Variable Containers + Other Enhancements

This isn't a suggestion that would affect functionality at all - purely just for organization purposes. I'd love to be able to make folders for variables, to keep them nicely grouped together (and so that I'm not scrolling through my hundreds of variables all the time).

Also, it'd be awesome (maybe this already exists?) to be able to rename a variable and have that propagate through any macros that use that variable.

When I first started using KM I felt the same way about that. But the more I learn, the more I know this request is impossible. If such a feature existed, it couldn't possibly work 100% of the time, because variables come in a wide variety of expressions. Just for starters, have you considered the variables that are expressed like this: "$KMVAR_varname" inside a script action? There is no way on earth that KM could possibly know where all the variable names are located in a macro, for reasons like this. (In fact in some cases I even construct variable names dynamically using KM code!) Maybe for some users who only use a few "forms" of variables it could work. But it wouldn't work for advanced users.

But there are other things you can do to help with this problem. For example, I used to use a variable called "Mouse" or "LocalMouse" to store mouse coordinates. But that's pretty painful to search for because when you use the string "Mouse" in the KM editor to search for your variables called Mouse, you also get KM actions called Mouse. So to solve this all my mouse variables are called "Cat" instead. Then searching works much better.

What I recommend is you explain why you want this variable replacement feature and we might be able to recommend ways to achieve what you want without having this feature implemented.

That's a fair point. That doesn't involve modifying any user code. This would require changing only the KM Editor, not user code. So it's certainly much more doable. I'm not opposed to this idea. I'm not sure that there's enough support for it, but I'm not opposed to it.

But as with my previous point, there are some things you can do to reduce your problem here. For example, do you use Local variables in your macros? That will probably get rid of the majority of variables in that list. Good programming should be using local variables. Although to be honest local variables are harder to debug. So my compromise is that when a macro works I convert its variables to local ones.

Here's a question. Why do you have any global variables at all? Can you convert them all to locals? I imagine that you can convert 90% to locals. Yes, you may have some variables that are permanent storage vessels or are used to (temporarily) pass data from one macro to another. As for the latter, those can be deleted programmatically once the passing of the data is completed. As for the former, I'll bet you have a source for the creation of that data and in theory could re-generate them if they were deleted. I'll bet if you took a close look you could easily re-code your macros so that 99% of the global variables no longer exist.

And one more thought. Variables in the global variable list are organized alphabetically. If you stopped using global variables like X and started all variables with the macro name, like MoveMouse_X then your variables would be kinda organized in a way that you might like.

Hypothetical bonus question: if you deleted all the variables in your global variable list, would your macros stop working? Are you storing permanent data in them?

I'd like to make two points that may eliminate, or at least reduce, your need for such a container:

  1. Maximize use of Local and Instance Variables , which should greatly reduce the number of global variables that you need to maintain and resuse, since these variables are deleted when the Macro terminates.
  2. For Global Variables, I assign a prefix that is based on the primary macro where the variable is created/used.
    • For example, in my Macro "Move and Set Window Size Per App & Win Name", I use a prefix of "MSW__" (Move and Set Window), like this:
      • "MSW__WindowList"
    • This allows you to easily search/identify all of the Variables associated with a specific Macro.
    • You could think of the prefix as the "container" for this group of Variables.

OK, I say two points, but here's one more: Use KM Dictionaries to group strongly related "variables", which are known as "keys" in a Dictionary.

HTH.

Yep, already using prefixes - still have a looooot of variables - makes the list crazy long. I do a lot of applescript and UI scripting, so I think I'm averse to using local/instance variables due to that. A good number of my macros interact with other macros (IE, one macro that sets some variables, but another that uses them, so that I/the end user doesn't have to define things every time).

Dictionaries is something I hadn't realized KM had, though! I'll likely use those - but is there an easy way to view a dictionary like you can with variables? I don't see dictionaries pop up in the preferences, or in the value inspector at all.

As to the impossibility of KM actually finding every instance of a variable (even within scripts) - I tend to think of applescripts as one of the more advanced features of KM, and I'd be perfectly fine with having to swap out the variables from my custom scripts - because I can load THOSE into a text editor and do a script-wide find/replace. But swapping them out in other KM actions/macros means I have to hunt them all down.

I don't understand your concern. AppleScript (and JXA) have full access to both Local and Instance Variables used by the Macro that calls the script.

Sounds like a good use case for Instance Variables and Sub-Macros. Any Instance Variable created or used by the Main Macro, and any Sub-Macros it calls, can be shared by all of these.

If you are writing AppleScripts, then you should be using Script Debugger 7, which has a built-in find/replace that includes RegEx. SD7 comes with a free fully-functional 30-day trial, and then, if you don't buy it, will downgrade to SD Lite.

I'm curious as to how many sets of variables you have (one set means one prefix).
To be clear, I also have several Macro systems that require global variables to share. In effect, I'm using KM Global Vars like a database. But I'm guessing that I have less than 10 sets of Global Vars.

BTW, are you using the menu/shortcut for Insert Variable by Name (⌘⌃V)? This is very powerful, and should quickly allow you to select the variable of interest from a large number.

For example, one of my variable sets is for a "TimeTracker", so by typing:
tt__ SPACE client
I can quickly see my TimeTracker variables related to "client":

image

Even with hundreds of variables, if you use a prefix this should let you quickly find your variable.