1

I'm Firefox guy, is there any way to get active tab's url via .batch file?

For example, if url of active tab in Firefox is whatever.com, I need to get whatever.com using bat file.

0

Only the active tab will have a title detectable by tasklist. So:

tasklist /v /fi "imagename eq firefox.exe"  |findstr /r /v "N/A"

or processed with FOR:

for /f "skip=1 tokens=9* delims= " %%a in ('tasklist /v /fi "imagename eq firefox.exe"  ^|findstr /r /v "N/A"') do @echo %%b

though the title could be different than the link.

With sendkeys.bat you can trick the browser to change its title to the current link:

@echo off
::get the title of the active page
for /f "skip=3 tokens=9* delims= " %%a in ('tasklist /v /fi "imagename eq firefox.exe"  ^|findstr /r /v "N/A nsAppShell:EventWindow"') do set "fftitle=%%b"
::get the first 10 sympols needs to be passed to the sendkeys command
set "fftitle=%fftitle:~0,10%"
::opens the console of the browser
call sendkeys.bat "%fftitle%" "{f12}"
::waits for 2 seconds
w32tm /stripchart /computer:localhost /period:2 /dataonly /samples:2  1>nul
::changing the title with the link location
call sendkeys.bat "%fftitle%" "document.title=window.location.href{ENTER}"
::wait for 3 seconds
w32tm /stripchart /computer:localhost /period:3 /dataonly /samples:2  1>nul
::get the new title
for /f "skip=3 tokens=9* delims= " %%a in ('tasklist /v /fi "imagename eq firefox.exe"  ^|findstr /r /v "N/A nsAppShell:EventWindow"') do set "fftitle=%%b"
echo "%fftitle%"

Edit. Direct opening of the console:

@echo off
::get the title of the active page
for /f "skip=3 tokens=9* delims= " %%a in ('tasklist /v /fi "imagename eq firefox.exe"  ^|findstr /r /v "N/A nsAppShell:EventWindow"') do set "fftitle=%%b"
::get the first 10 sympols needs to be passed to the sendkeys command
set "fftitle=%fftitle:~0,10%"
::opens the console of the browser
call sendkeys.bat "%fftitle%" "^+K"
::waits for 2 seconds
w32tm /stripchart /computer:localhost /period:2 /dataonly /samples:2  1>nul
::changing the title with the link location
call sendkeys.bat "%fftitle%" "document.title=window.location.href{ENTER}"
::wait for 3 seconds
w32tm /stripchart /computer:localhost /period:3 /dataonly /samples:2  1>nul
::get the new title
for /f "skip=3 tokens=9* delims= " %%a in ('tasklist /v /fi "imagename eq firefox.exe"  ^|findstr /r /v "N/A nsAppShell:EventWindow"') do set "fftitle=%%b"
echo "%fftitle%"
| improve this answer | |
  • Doesn't change the browser title in my tests. obviously I could not get the active tab's url too – Mehdi Dehghani Oct 5 '17 at 4:59
  • @MehdiDehghani - is there any error printed?Does the the script open the console of the browser? – npocmaka Oct 5 '17 at 6:05
  • I got no error, the browser console got open by the script (Network tab ), but the title doesn't changed. – Mehdi Dehghani Oct 5 '17 at 7:22
  • @MehdiDehghani - ahaaa. Firefox preserves last clicked tab.YOu can click the console tab and it will work.Meanwhile I'll alter the script to directly open the console – npocmaka Oct 5 '17 at 7:46
  • @MehdiDehghani - can you try my update? – npocmaka Oct 5 '17 at 7:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.