Feature Request: Set Values for "Multiple" Variables in "One" Action

Hi @peternlewis,

I'd like to have one action to set values for multiple variables.
It will be like the Prompt for User Input action, just without the prompt and the values are all the default values for the Prompt for User Input action.

For instance, I have these actions:

I'd like to have an action like this (taken from the Prompt for User Input action):
image

I can set more variables by clicking the "Plus" icon.

Benefits:

  • This will significantly reduce the height of the macro in the Editor without having to collapse the actions.
  • The reduced height will allow us to view and edit the macro easier.
  • If I want to disable all the variables (debug), I only need to select this one action to disable. I don't have to do multiple-selection or first group them together.

If there are other good alternatives, please let me know.

PS:
I know I can use AppleScript to do some similar things:

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	setvariable "Local_Var 01" instance kmInst to "1"
	setvariable "Local_Var 02" instance kmInst to "2"
	setvariable "Local_Var 03" instance kmInst to "3"
	setvariable "Local_Var 04" instance kmInst to "4"
end tell

But a native KM action with the input box has many advantages:

  • It looks much nicer.
  • It's easier to add and delete variables, and
  • It allows variables and tokens for values.
2 Likes

Hi @martin - or you could simply roll your own third=party plug in:

KM 0 2021-03-30_23-21-25

I know it's not quite as you envisaged, but in the short term it would do the job.

Here's the beginnings of the plug-in:
EDIT:
The plugin is complete see later in this discussion Feature Request: Set Values for "Multiple" Variables in "One" Action (post 15).

Hi @tiffle,
Thanks for recommending this. I did not know it before. It's definite an improvement.
Does it allow text tokens? Like

var1,%SystemClipboard%
var2,%Variable%Var%

Hi,
I haven't actually written the code that makes the plug in work, but I specified (in the plist file) that the field containing the variable/value pairs is of type TokenText, so yes you could use any tokens that you like and the KM engine will take care of it for you.

To implement the code you'll need to edit the action.scpt file. Personally, I would use AppleScript to split out the pairs and perform the assignments.

Oh. I thought the plugin was uploaded by someone already. :joy:

I'll then wait for @peternlewis's response. If he has no plan to implement it, I'll try to make a plugin for myself when I get time. It'll be a bit challenging to me due to my very limited knowledge.

Sorry if I wasn't clear - it's just that I've already done something vaguely similar with one of my own plugin ins and thought I'd give you a kick-start by suggesting an alternative to waiting for an enhancement to KM to come along together with a "template" for you to work on and thus "roll your own".

If you're not confident with taking this further, let me know and I'll put something together by this weekend (I'm busy right now) and about to go to bed...

Yes, it is good idea to have a single action to initiate variables.

In the meantime, if need quick solution without using plugin , you can try the following actions.
I use @= as delimiter to reduce the possibility of data having that as delimiter ( you can use Search Action using regex to extract data but for simple need, this maybe sufficient.)
The other constraint is that data must not span multiple line.

4 Likes

I generally don't like implementing multiple implementations of the same basic functionality (when I do it is because I want a new one to replace the old one but can't actually remove the old one).

In this case, you can collapse all the Set Variable actions which would give similar height, and double clicking an action to edit its value (double click in the title is shorthand for collapse/expand) will let you edit them.

There are additional options for the Set Variable action, most particularly how to process tokens, which would have to be all-or-nothing with a multiple-variable setting action, so that is one problem.

So basically, while this is an interesting idea, I can't see implementing it I'm afraid.

1 Like

You can also group variable assignments like so:

Group.kmactions (1.6 KB)

Option-Clicking on the triangle open-widget in one of the Set-Variable Actions will open ALL of the Set-Variable Actions at once.

-Chris

2 Likes

Hey Folks,

I think I like @macdevign_mac's Keyboard Maestro native method here better than using AppleScript, but I automatically built one with AppleScript before seeing his.

:sunglasses:

Since it will handle multi-line content in the variable assignments I'm going to post it.

Another thing to take note of is that I've discovered the Display Text in a Window action can handle Tables. The one displayed was created in Pages.

-Chris


Multiple Variable Assignments v1.00.kmmacros (27 KB)

2 Likes

There are many actions in KM can be removed and we can still do the same work with other KM actions. The point is if they are removed, we may need more actions to do the same job, and doing this will add more actions and take more time. Therefore, having those actions is perfectly justified, because they save time for users. This is also the main point of my feature request.

Thanks, Chris. I did mention this in my OP.

Thanks for posting your macro. There is much I can learn from it.

That's a smart way to do it. Thanks!

Thanks, @tiffle. @macdevign_mac and @ccstone have posted alternative methods. I think I'm ok to use their method. But if you could make a plugin, it would be great. The advantage is that only one plug-in action is needed.

True, but you don't mention the grouped items can be opened and closed all at once.

:sunglasses:

-Chris

1 Like

I forgot to explicitly state it. But that was what I meant to say. :joy:

Hi @martin,
some spare time came up so I completed the plugin for you.

This is the file:
TFL Set Multi Variables.zip (552.7 KB)

To install the plugin just download the zip file and drag it onto the KM icon in the dock. Then be sure to quit the KM engine (in File>Quit Engine) and then restart the engine (File>Launch Engine). The plugin will now be available as an action to add to your macros as normal.

The default appearance is this:
KM 0 2021-03-31_11-48-08

and in use it might look like this:
KM 1 2021-03-31_11-48-42

As you can see, you can specify a delimiter to separate the variables from their values (the default is ",") but you can be more adventurous as shown in the example.

You can also use variable tokens as shown.

NOTES:

  1. Each variable/value pair must be a single line of text even after any tokens have been expanded.
  2. The plugin in will fail with error code -999 if it encounters ANY error - such as multiple-line assignments, or incorrect/mismatching delimiters. In such a case a brief notification will appear while the full error message can be found in the KM log.
  3. If you use the plugin you do so at your own risk and I accept no liability for any loss; I have performed limited testing on the plug in so I am confident it will not misbehave, but YMMV. You have been warned.

When I get time I will post this plug in to the Plug Ins category of the KM forum, but that won't be for a few days now.

Hope that helps but if you use the plug in let me know how you get on.

The plugin and full description can be found here

6 Likes

It works great! Since Peter does not have plan to implement it, and your plugin is closest to my feature request, I'll mark it as the answer so that others who search for it may easily see it. Thank you very much!

2 Likes

BTW, I've seem many examples where multiple actions are used to just clear some variables when the macros are done. This plugin will serve well for that purpose as well. Just leave blank after the comma (set as the text delimiter):

That is a handy plugin. Thank you very much!

1 Like

You’re welcome.

The plugin and full description can be found here

2 Likes