App Window Count Conditional

I am attempting to create an automation that only triggers if there is one window open in an app. Is there a condition for window count?

I think this would ideally be an Advanced Condition on visible_window_list. This may be possible using the matches conditional, but you'd have to contain all the logic in a single regex pattern (which would be fairly complex given your requirement).

Alternatively, you could use the Run Real Javascript action. To adapt the code below to your use case, change:

  • TextEdit to the app you want to count visible windows for;
  • YourNamedTrigger to a Named Trigger that performs the actual actions (this trigger only runs the JavaScript, which then might call the Named Trigger).
(async ()=> {

const window_list = await callBTT('get_string_variable', {variable_name:'visible_window_list'} );

let count = (window_list.match(/TextEdit/g) || []).length;

if (count == 1){
await callBTT('trigger_named', {trigger_name: 'YourNamedTrigger'});
}

returnToBTT("done");

})();

Possibly also worth noting:
These JavaScript functions do actually have the ability to return values to BetterTouchTool, but I haven't been able to find anything in the documentation or forums regarding how to use or handle those values after the JavaScript has executed.
If you can figure that bit out, you may be able to contain this whole flow in a single trigger.

As far as I'm aware, there is no action type of the genre "take the value from the previous action, process it in some way, then maybe continue to further actions". This may be a Keyboard Maestro mindset that is outside the BetterTouchTool ethos.

Let me know if you have any trouble.

Cheers,
Vito

1 Like