Last active
April 11, 2024 16:34
-
-
Save mikebeaton/7e2c364e876c8d22e84e2456592d9d39 to your computer and use it in GitHub Desktop.
Batch file to activate and restore an existing program if it exists, or start a new instance otherwise (in this case, Fork.exe)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@if (@X)==(@Y) @end /* JScript comment | |
@echo off | |
setlocal | |
for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq Fork.exe" ^| find /I "Fork.exe"') do set pid=%%i | |
if "%pid%" == "" ( | |
%localappdata%\Fork\Fork.exe | |
) else ( | |
cscript //E:JScript //nologo "%~f0" "%~nx0" "%pid%" | |
) | |
exit /b %errorlevel% | |
endlocal | |
@if (@X)==(@Y) @end JScript comment */ | |
var sh=new ActiveXObject("WScript.Shell"); | |
if (sh.AppActivate(WScript.Arguments.Item(1)) == 0) { | |
sh.SendKeys("% r"); | |
} |
Simpler version if you don't want all the features:
@if (@X)==(@Y) @end /*
for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq Zoom.exe" ^| find /I "Zoom.exe"') do set pid=%%i
cscript //E:JScript //nologo "%~f0" "%~nx0" "%pid%"
@if (@X)==(@Y) @end */
var sh=new ActiveXObject("WScript.Shell");
sh.AppActivate(WScript.Arguments.Item(1))
👍 ty
How do i use this code for same batch which is running?
I don't want to call another batch file.
How to mix code ?
example
@if (@x)==(@y) @EnD /* JScript comment
@echo off
setlocal
for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq Fork.exe" ^| find /I "Fork.exe"') do set pid=%%i
if "%pid%" == "" (
%localappdata%\Fork\Fork.exe
) else (
cscript //E:JScript //nologo "%~f0" "%~nx0" "%pid%"
)
exit /b %errorlevel%
endlocal
@if (@x)==(@y) @EnD JScript comment */
var sh=new ActiveXObject("WScript.Shell");
if (sh.AppActivate(WScript.Arguments.Item(1)) == 0) {
sh.SendKeys("% r");
}
mybatch code ........
I'm getting error var not recognize, compilation error bla bla bla
share full working script
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Built from several references, including:
https://stackoverflow.com/questions/35988863/using-command-line-batch-to-switch-to-focus-on-app (especially this answer)
https://stackoverflow.com/questions/2323292/assign-output-of-a-program-to-a-variable-using-a-ms-batch-file
https://stackoverflow.com/questions/31723571/use-appactivate-to-change-the-active-window
https://community.qlik.com/t5/QlikView-Scripting/VBScript-AppActivate-Restore-Window-not-working/m-p/1113050
With thanks!