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

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

I had a go at making a more limited but arguably more user-friendly plugin:

Set_Multiple_Variables.zip (7.3 KB)

The only thing is, it sets the first three variables, but not the last two. I can't, for the life of me, figure out why...

Here's an example test macro you can try once the plugin is installed:

Set Multiple Variables - Test.kmmacros (21 KB)


Have I made an error in either of these?

Plugin Action XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Author</key>
	<string>Neil Thomas</string>
	<key>Icon</key>
	<string>icon.png</string>
	<key>Name</key>
	<string>Set Multiple Variables</string>
	<key>Parameters</key>
<array>
    <dict>
        <key>Label</key>
        <string>Variable 1</string>
        <key>Type</key>
        <string>String</string>
    </dict>
    <dict>
        <key>Label</key>
        <string>Value 1</string>
        <key>Type</key>
        <string>TokenText</string>
    </dict>
    <dict>
        <key>Label</key>
        <string>Variable 2</string>
        <key>Type</key>
        <string>String</string>
    </dict>
    <dict>
        <key>Label</key>
        <string>Value 2</string>
        <key>Type</key>
        <string>TokenText</string>
    </dict>
    <dict>
        <key>Label</key>
        <string>Variable 3</string>
        <key>Type</key>
        <string>String</string>
    </dict>
    <dict>
        <key>Label</key>
        <string>Value 3</string>
        <key>Type</key>
        <string>TokenText</string>
    </dict>
       <dict>
        <key>Label</key>
        <string>Variable 4</string>
        <key>Type</key>
        <string>String</string>
    </dict>
    <dict>
        <key>Label</key>
        <string>Value 4</string>
        <key>Type</key>
        <string>TokenText</string>
    </dict>
       <dict>
        <key>Label</key>
        <string>Variable 5</string>
        <key>Type</key>
        <string>String</string>
    </dict>
    <dict>
        <key>Label</key>
        <string>Value 5</string>
        <key>Type</key>
        <string>TokenText</string>
    </dict>
</array>
	<key>Results</key>
	<string>None</string>
	<key>Script</key>
	<string>SetMultipleVariables.scpt</string>
	<key>Timeout</key>
	<integer>1</integer>
	<key>Title</key>
	<string>Set Multiple Variables</string>
</dict>
</plist>

Script
use AppleScript version "2.4"
use scripting additions
	
	set inst to system attribute "KMINSTANCE"
	tell application "Keyboard Maestro Engine"
		
		set var1 to system attribute ("KMPARAM_Variable_1")
		set val1 to system attribute ("KMPARAM_Value_1")
		set var2 to system attribute ("KMPARAM_Variable_2")
		set val2 to system attribute ("KMPARAM_Value_2")
		set var3 to system attribute ("KMPARAM_Variable_3")
		set val3 to system attribute ("KMPARAM_Value_3")
		set var4 to system attribute ("KMPARAM_Variable_4")
		set val4 to system attribute ("KMPARAM_Value_4")
		set var5 to system attribute ("KMPARAM_Variable_5")
		set val5 to system attribute ("KMPARAM_Value_5")
		
		setvariable var1 instance inst to val1
		setvariable var2 instance inst to val2
		setvariable var3 instance inst to val3
		setvariable var4 instance inst to val4
		setvariable var5 instance inst to val5
		
	end tell		
4 Likes

Well Neil, this is the result I get with your test:

2023-10-05_17-16-06

so that seems OK.

The only thing I would suggest (but may be totally irrelevant since it's working for me as-is) is this: since your plug in is just setting values having a timeout is completely unnecessary so I'd set the Timeout number to 0 in your Keyboard Maestro Action.plist file.

Another thing - and this is what I call "best practice" - to avoid confusion with anyone else's plug ins I always preface the name of mine with my initials "TFL". Of course, this is just a convention I've adopted but at least it makes identifying plug ins in the actions list that much easier and ensures there's no clash of names in the future. For example, on my system I have:

2023-10-05_17-31-04

Anyway - food for thought!

3 Likes

Weird. It's working for me too now. Very odd, as I'd tried dozens of times and even restarted my mac during testing. Now it just works. Gift horse / mouth etc. :man_shrugging:t2:

The default timeout for KM actions is 99hrs, so I figured it should be something more than 0. Guess not. Thanks!

Good idea!

That's true, but "1" maps to a 1-second timeout which may be too short on r e a l l y...s l o w Macs (ahem).

Anyway, if you want the default, then just leave out the Timeout entry altogether from the plist file since it's an optional key.

Well, that's quantum entanglement for you...

1 Like

Thanks for your input. The only problem is that the number of variables is limited to 5. It's nice if the macro needs to set 5 variables (no more, no less).

For sure, @tiffle's approach is much more efficient if you're setting short variable values. I mainly had a go at this alternative approach for the opportunity to develop my plugin-making skills.

That said, it's actually very easy to adjust the plugin to increase the number of variables if you wish, by editing the .plist and .scpt files in the plugin folder.

The main advantage of this plugin is that each variable has its own text box, so if your values are a little longer, it's still relatively readable.

Five seemed to be a number just large enough to justify using a plugin rather than native actions in order to save vertical space, without being so large that it would begin to defeat the object by bloating the macro.

You can of course use multiple of these plugin actions if for some reason you're setting a lot of explicit variables at the same point in your macro which, for me personally, doesn't come up that often.

@noisneil - look back at the OP and you’ll see this.

This is why I took the approach I did.

1 Like

Yeah, as I said, you've already solved it in a much more economical fashion, but you know, more is more.

Especially since with yours you can set variables to multi-line values - something I didn’t find a way to do!

1 Like

I tried to find a way to programmatically generate the plugin XML with user-defined variable numbers, but it seems like a non-starter. It would be cool if there was a way to add a + button to a plugin to dynamically create new fields, but I don't think that's possible either.