0

I am new to scripting and I found this script online that seems to do what I need after I tweaked it a bit. I am trying to close a popup window once it opened, once the popup window opened I need a flag to change from false to true, and vise versa the next time the window opens.

It seems like the place where I currently define the flag "connected" is a part of the loop and its being re-initialized every iteration, hence always true. I need to initialize it somehow only once and pass it inside the loop.

@if (@CodeSection == @Batch) @then

@echo off
    set SendKeys=CScript //nologo //E:JScript "%~F0"    
    color 0a

    :loop
        %SendKeys% "{TAB}{TAB} "
        timeout /t 60 /nobreak >nul
    goto :loop

@end
var WshShell = WScript.CreateObject("WScript.Shell"); 
var connected = true;
if (WshShell.AppActivate("Popup window title")) {
    if (connected) {
        WScript.Echo("Inside Connected");
        connected = false;
        WshShell.SendKeys(WScript.Arguments(0));    
    }
    else {
        WScript.Echo("Inside Disconnected");
        connected = true;
        WshShell.SendKeys(WScript.Arguments(0));    
    };
             
}   

I also tried using the set command right after the "set SendKeys" like this "set connected=true" and then I can use this variable like this %connected% within the scope but its not defined when I try to access it in the if statement.

Any help would be much appreciated!

user692942
  • 16,398
  • 7
  • 76
  • 175
Roman
  • 51
  • 2
  • Every loop in the batch section reinitiates the JavaScript section, so the JS code of one call does not relate by any means with another one. You could et the JS section return the connection status via its exit code which you could check in the batch file section using [`if ErrorLevel`](https://ss64.com/nt/if.html), the [`ErrorLevel` pseudo-variable](https://ss64.com/nt/errorlevel.html) or by [the conditional operators `&&` and `||`](https://ss64.com/nt/syntax-redirection.html); – aschipfl Oct 02 '21 at 19:08
  • alternatively, you could let the JS section return the status in the [_STDOUT_ stream (handle `1`)](https://ss64.com/nt/syntax-redirection.html) and capture it in the batch section using [`for /F`](https://ss64.com/nt/for_cmd.html)… – aschipfl Oct 02 '21 at 19:08
  • Ended up writing this script in Python.. so much less headache. Thanks for the help though! – Roman Oct 05 '21 at 13:53

0 Answers0