"If Condition matches custom_variable" - Possible?

Hello,

I'm trying to configure BTT as a Workspace Manager (opens files/apps and rearranges windows across displays). This requires a lot of IF statements to identify whether a specific app/window is already open, switching to it if so, waiting for the switch to happen successfully, etc.

I already have a few of these where the conditions are hardcoded into each step and are a pain to reconfigure whenever I want to add another app. What I am looking to do is to set all the required variables in the first few steps so that I don't need to go into each condition and reconfigure everything.

TL:DR - Is it possible to set a condition that compares two variables, one of them being a custom variable?

The easiest would be to use an if condition that compares the two variables via the "If Java Script Returns True" action:

async function compareXY() {
	let variable1 = await get_string_variable("variableName1");
	let variable2 = await get_string_variable("variableName2");
	
	return variable1 == variable2
}

Apologies, but I can't seem to get it working, even for a test! I copied your code and just changed the variables. Clicking on run does not display a result, and neither does triggering (doesn't continue past IF). As you can see, the variables are in fact set to the same value. Puzzled!

Also, I realize i wrote a poor TL:DR for my post. In order to get this to work I would need to access the current app name to compare it using this JS method. Is it possible to access a "BTTActiveAppName" variable (or something)? BTT has an AppName variable in CAGs and Advanced Conditions - is there anyway to script either?

Even better of course would be to be able to use custom variables to compare to a variable in Advanced Conditions like I'd previously suggested, but I guess that may not be technically feasible?

Thank you Andreas!

Try this (slight variation of Andreas' script):

async function compareXY() {
	let variable1 = await get_string_variable("focused_window_app_name");
	let variable2 = "BetterTouchTool";
	
	return variable1 == variable2;
}

You need to make sure that the dialog box that says
"Async function to call" is filled with the name of the function, in this case compareXY

Your screenshot cuts off above this box, so it's not possible to tell if you've set this correctly or not.

1 Like

Your setup looks good, make sure to enter the correct function name as mentioned by @telephanth.

You can get any variable listed in CAGs or advanced trigger conditions via BTT's scripting (either get_string_variable or get_number_variable)

	let variable1 = await get_string_variable("BTTActiveAppName");

Unfortunately with the default condition editor it is not possible to compare two variables, this is a technical limitation of using the system provided "predicate editor". Maybe at some point in the future I will switch to a custom implementation.

@Andreas_Hegenberg @telephant

Thanks gents, got it to work. Indeed, I hadn't used the JS trigger before (nor much of JS really) so I didn't realize I had to change "Async function to call".

Unfortunately it is performing really poorly - which I find surprising for such a basic script. Maybe there's some other issue going on here? It is taking a whopping 6 seconds for the HUD to appear:


This would be really powerful now that BTT has IF statements / action flow!

ah that should bascally be instant, maybe the variable you are using runs some apple script and maybe there is an issue with that. I might not have tested this in the if condition :slight_smile:

Could you post the full script you are using?

I am using focused_window_app_name from ATC. I also tried appName from CAG; same results.

async function compareXY() {
	let variable1 = await get_string_variable("focused_window_app_name");
	let variable2 = await get_string_variable("AppNameTarget");

	// Return true if both are equal
	return variable1 === variable2;
}

I‘ll check tomorrow! could be some deadlock as 6 seconds sounds like the failsave timeout :slight_smile:

I just tried to reproduce, but here it is instant. Are you sure the function name is set correctly? Which version of macOS/BTT are you on?