How can I capture the "currently active" frontmost window?

I noticed that I can capture a window by ID but I'd like to dynamically capture my currently active frontmost window.

It would require some scripting:

This script would save it to your downloads folder:


async function screenshotFocusedWindow() {
     let foucsedWindowID = await get_number_variable("BTTActiveWindowNumber");
		
    let screenshotActionDefinition = {
        "BTTPredefinedActionType" : 169,
        "BTTPredefinedActionName" : "Capture Screenshot (Configurable)",
        "BTTScreenshotOptions" : `-l;;${foucsedWindowID};;-t;;png;;~\/Downloads\/Screenshot_{datetime}_{random}.png;;`,
        "BTTScreenshotDateFormat" : "yyyy-MM-dd HH.mm.ss",
  };
	let result = await trigger_action({json: JSON.stringify(screenshotActionDefinition), wait_for_reply: false});

	return false;
}

I'm thinking this is a good chance to combine Keyboard Maestro and BTT. I can call a KM macro that screenshots the currently active front window and copies it and call that from BTT when I hit that spot in my macro

running the java script should be more performant, but yes that would also work