3

I am creating a batch file to handle firefox tabs. I want to close the last tab in firefox using windows command.

I have tried this command using title name

taskkill /IM firefox.exe /FI "WindowTitle eq localhost*"

It closes all the window

I have searched in windows command, there are no direct way. Is there any possibilities ?

ManiMuthuPandi
  • 1,594
  • 2
  • 26
  • 46

1 Answers1

6

You will have to use VBScript code to send Ctrl+w keys to Firefox.

Save this code as closeActiveTab.vbs and run it with cscript closeActiveTab.vbs in your batch file or embed the code inside.

Dim Shell, WMI, query, process

Set Shell = CreateObject("WScript.Shell")

Set WMI = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
query = "SELECT ProcessId FROM Win32_Process WHERE Name = 'firefox.exe'"

For Each process In WMI.ExecQuery(query)
    Shell.AppActivate process.ProcessId
    WScript.Sleep 100
    Shell.SendKeys "^w"
Next

Based on another answer.

Community
  • 1
  • 1
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • To close only the last tab open in the for loop you have to exit immediately. thank you very much for the solution – Tommaso Aug 23 '21 at 11:56