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

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.

The plist defines the user interface for the plug in action and I haven’t discovered a method of dynamically changing it while in the KM Editor. I don’t know enough about the ins and outs of Mac software development to definitively say it’s possible or not - maybe @peternlewis can shed some light on this?

1 Like

You can't have variants of the plugin installed, so that does not seem like it is going to work.

You could generate the XML for the action, set of actions or whatever. For example, given a list of variables, you could generate the XML for the Group action or a For Each action that dealt with all the variables any way you wanted. The XML could even indicate that the root level action was un-disclosed.

Hi Peter,

Is it possible provide an option to suppress the popup for the Prompt for User Input action?

This option could be added to the right click menu (see below). I can then use the default value field to set values for multiple variables in one action.

Of course, the problem is that if a pop up window is suppressed then it is no longer a pop up window. So maybe a new action can be added that works similar to the Prompt for User Input action but without the popup feature. When the variables values are simple (e.g., numbers or consisting of a few characters/words), this action will be very useful.

I'm afraid I don't have any plans to do either of these.

It doesn't really make sense to have a variant of the Prompt for User Input action that doesn't actually prompt but does set all the variables.

And I don't see enough value in an action to set a bunch of variables to values that exceeds simply using a sequence of Set Variable to Text actions (optionally within Group action).

Especially when there are a variety of other ways that it could be done, such as passing a multiline block of text to a subroutine or a Favorite action that sets all the variables.

Interested in these techniques. Any references?

1 Like

There have been various macros posted around the forum, but for example:

4 Likes

Thanks. That's very cool.

The subroutine idea intrigues me. How do you return the variables to the caller macro without knowing their names explicitly?

For a subroutine, the same would work as shown if you past in the text, and assuming only Instance or global variables were used.

1 Like

Hey Peter, I replicated this for my personal use, but it doesn’t seem to work when I want to set the variables to %Delete%... what am I doing wrong here?

Download Macro(s): Set multiple variables.kmmacros (6.7 KB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 13.6
  • Keyboard Maestro v10.2

Because when you set variable debug__varValue to %Delete% in the Search using Regular Expression action, it “deletes” the variable.

So when it is later used, it has no value (not %Delete%).

You can see this stepping through with the Macro Debugger

If you want to make an equivalent that deletes the variables, don't include the value and just use %Delete% in the Set Variable to Text action.

BTW, this regex (.*):(.*) is incorrect, it should be (.*?):(.*). Otherwise, if you had, for example:

VariableName:This is cool: you can jump on cheese

The result would be a variable name of “VariableName:This is cool” and value of “ you can jump on cheese”

3 Likes