Batch file - start program ON TOP

I have a simple .bat file that opens a page in Firefox and I created a task in task scheduler to execute this every day at a certain time.

@Echo off
"C:\Program Files\Mozilla Firefox\firefox.exe"
EXIT

The problem is that when there are other programs or folders open, Firefox will open underneath those. Is there any way to start Firefox on top?

0

2 Answers

Use your bat script to call firefox if not running:

@echo off
title <nul && title ...\"%~nx0"
%__AppDir__%mode.com con: cols=50 lines=10
%__AppDir__%tasklist.exe | find /i "firefox.exe" >nul || (
start "" /b "C:\Program Files\Mozilla Firefox\firefox.exe" )

And add/use also VBS code to bring firefox to the foreground:

WScript.CreateObject("WScript.Shell").AppActivate("firefox")

Use adapted code in a hybrid mode your + :

<!-- :@echo off
title <nul && title ...\"%~nx0"
%__AppDir__%mode.com con: cols=50 lines=10
%__AppDir__%tasklist.exe | find /i "firefox.exe" >nul || (
start "" /b "C:\Program Files\Mozilla Firefox\firefox.exe" )
%__AppDir__%timeout.exe /t 2 /nobreak | echo=// waiting 2 seconds to make sure firefox loaded
%__AppDir__%wScript.exe "%~dpnx0?.wsf" & %__AppDir__%move.com nul 2>&0 & goto :eOf || exit /b --><job><script language = "vbscript">WScript.CreateObject("WScript.Shell").AppActivate("firefox")</script></job>

A simple explanation of the hybrid layout would be:

<!-- : bat code and also vbs comment
--><job> vbs script code
</job>

Obs.: Save this script as some_file_name.cmd or some_file_name.bat


// Some links to familiarize yourself with hybrid codes involving bat and vbs:
- Syntaxe
- Examples

// Questions:
- How to call or embedded VBS in Batch script
- Is it possible to embed and execute VBScript within a batch file without using a temporary file?

0

You may succeed with

@Echo off
"C:\Program Files\Mozilla Firefox\firefox.exe"
ping 127.0.0.1 -n 1 > nul
EXIT

I don’t know the reason it works for me, I just happened to have the same problem as you with an application window staying in the back on a Windows 10 system. I already gave up and wanted to output a hint in the batch that the application window may be hidden. To keep the hint visible for a short time before the batch window closed (and in lack of a delay command for batch files) I wanted to use the common ping 127.0.0.1 -n 6 > nul (five seconds delay by six pings to localhost with one second interval and the output redirected to nul, because it is of no interest, here) after starting the application and outputting the hint. Since now the application was opened on top, I removed the hint and reduced the delay to zero seconds (one ping).

Presumably other commands than ping are also suitable for ensuring that the window is opened in the foreground, but the internal commands exit and echo are not suitable.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like