v6.721 Named Trigger Variables?

From the releae notes:
Named triggers can act like reusable functions: they can declare variables (text or number), and the "Trigger Named Trigger" action lets you pass values directly.

I have seen there is a new checkbox in the Named triggger: "Named Trigger Requires Variables" (it opens 4 fields for 2 vars)

@Andreas_Hegenberg could you explain how this new feature works?

Thanks!

here is an example:

When calling the named trigger you can provide values for variables and the named trigger can then use these variables (e.g. in scripts like in that example, but any action can be converted to Java Script. Or they could be used in if conditions etc. inside of the named trigger.)

Thanks, I get it.

One question regarding the variables in the trigger: will you add an option to set the vars as persistent or temporary?

no, named trigger variables are always temporary. But of course you can use the set variable action instead!

Why set them always as temporary vars?

they are supposed to be used like function parameters, this is important for some other stuff I am working on and I don't want to mix that with persisting stuff.

Very cool, I've been hoping for a way to add parameters to triggers!

Some questions about using this feature:

Assume I define a trigger "My_New_Trigger2", with the following variables:

  • var1, type Number
  • var2, type Text
  1. I see that each of these has two fields below it, "Variable 1, Default Options" and "Variable 1 Description". Is the Default Options for providing a default value for the field in case no value is provided when the trigger is called?
    • In my initial try at this, I put 13 in the default field, and I then have the following in my JavaScript action:
    • async function someJavaScriptFunction() {
      let var1 = await get_number_variable("var1");
      return var1;

      }

I had assumed this should return 13 when run from the run button in the BTT config panel, but I'm getting undefined as a result. In your sample, the contents of the "Variable 1, Default Options" field is "the desired window width", but this looks like a description to me, so I clearly don't fully understand the intended usage...

  1. What's the syntax for calling triggers with variables/parameters from JavaScript?
    Old usage (documented at Using JavaScript (not JXA) | BetterTouchTool Documentation )
    says this:

let result = await trigger_named({trigger_name: 'Action5', wait_for_reply: true});

  • I assume that some params need to get added in the {} here, but are they the names of the variables, in my case var1 and var2, or something else?
  1. I have found various examples in the documentation of JavaScript functions returning values using either return X; or returnToBTT(X);
    • I have a bunch of JS functions, some of which use one form and some of which use the other. Which of these is correct?

(sorry about the numbering, that didn't turn out how I wanted!)

1.) Ah, the "Default Options" name is bad, sorry! It's not a default value - it's a list of suggested values (one per line). These show up as autocomplete suggestions when you configure the "Trigger Named Trigger" action, and they are also provided to AI / Spotlight so they know which values make sense for that variable. The variable itself is only set when a caller actually passes a value. That's also why the "Run" button gives you undefined - it just runs the named trigger without passing any variables. I'll rename that field and add a better description with the next build.

For now, if you want a default value, the easiest way is to handle it in your script:

 async function someJavaScriptFunction() {
   let var1 = await get_number_variable("var1") ?? 13;
   return var1;
 }

2.) You just add the variables as additional keys, using the variable names you defined:

 let result = await trigger_named({
   trigger_name: "My_New_Trigger2",
   wait_for_reply: true,
   var1: 13,
   var2: "some text"
 });

One caveat: currently, when calling from Java Script, make sure to pass number variables as actual numbers (var1: 13, not var1: "13"), otherwise get_number_variable won't find them. The next build will automatically convert the value based on the type you declared in the named trigger, like the "Trigger Named Trigger" action already does.

3.) Both work :wink: If your script is an async function, just use return x; - BTT waits for the promise and uses the resolved value. returnToBTT(x) is the older style, which is mainly needed if your script is not an async function (e.g. callback-based code). For new scripts I'd recommend the async function + return variant.