Feature request: Limited variable scope

I’m finding myself creating “Set Variable to %Delete%” actions in a lot of my macros to keep down the steady proliferation of useless variables. This works fine, but it does make me think that it might be useful to be able to limit the scope of a variable as part of a “Set Variable to…” action so that it’s limited to the current macro and self destructs at the end.

2 Likes

That would be great!

@peternlewis, can you please add to your list? :grinning:

That sounds useful. Rather than making a new thread, I thought I’d ask here:

Is there a way to create a macro that deletes all variables? I’ve tried a few ways but I’m stumped (and still a keyboard maestro simpleton :smile:).

If this should be a separate thread, I can do so. Thanks!

Pretty easy to do in AppleScript:

tell application "Keyboard Maestro Engine"
  repeat with v in variables
    set value of v to "%Delete%"
  end repeat
end tell

Dangerous in the extreme.

And yes, it really should be a separate thread. :smile:

I actually have implemented “Local” variables - variables that are limited in scope to the current macro execution (and also independent for different macro executions of the same macro).

The problem with it is that it is really hard to explain and potentially very confusing, so currently it remains a disabled feature.

The implementation basically said any variable with name starting with "Local " is local to the macro execution group (which includes macros that executed it and macros it executes except for asynchronously executed macros which go off on their own execution group).

The problems happens hen you try to then set such a variable in the Variables preference pane or the like, it gets confusing very quickly.

1 Like

Is there a way to create a macro that deletes all variables?

Magic is usually best avoided with deletion – in this case you get the most control by deleting variables manually from the list in Keyboard Maestro > Preferences > Variables

But if you want to consider middle ground between control and power you could always script it with a 'keep list':

See Deleting (some) Keyboard Maestro variables – protecting others

Yep, I could see how that could be confusing. I really don't like tying variable scope to its name.

Peter, I think iNik's suggestion should be easier to implement and also very clear to the user. In the "Set Variable" actions, just add an option in the gear menu to "Delete Variable on Macro Exit". (or similar words).

If you wanted to go one further step, you could have an Action that is "Delete All Variables Created by This Macro on Macro Exit" (OK, needs a shorter title)

That way you don't have to explain "local" vs "global" variables to non-programmer users.

Thank you all! I have a very long list from lots of tinkering. I will
delete them manually as suggested and also make a separate thread next
time. :slight_smile: