5

It's possible to get the current tab with xdotool:

# set focus to address on browser
xdotool search --onlyvisible --classname Navigator windowactivate --sync key F6

# copy address from browser tab to clipboard
xdotool search --onlyvisible --classname Navigator windowactivate --sync key Ctrl+c

# get off the focus from address from browser tab
xdotool search --onlyvisible --classname Navigator windowactivate --sync key F6

# delivery of clipboard content to variable
clipboard=`xclip -o -selection clipboard`

# clear clipboard
xsel -bc; xsel -c

# echo URL of active tab of active browser
echo $clipboard

Since xdotool is buggy and sometimes fires keys without stopping with the code above, I need another way to do the same, perhaps with wmctrl.

This is what I tried, using wmctrl on bash; it didn't work for me:

id=$(wmctrl -l | grep -oP "(?<=)(0x\w+)(?=.*Firefox)")

# set focus to address on browser
xdotool key --window $id "ctrl+l"

# copy address from browser tab
xdotool key --window $id "ctrl+c"

# delivery of clipboard content to variable
url=`xclip -o -selection clipboard`

# clear clipboard
xsel -bc; xsel -c

# echo URL of active tab of active browser
echo $url

3 Answers 3

5

I wouldn't use xdotool for that - it is too fragile and error-prone. Use a proper browser automation tool instead such as Marionette. Install marionatte_driver python module:

pip3 install --user marionette_driver

Start a new instance firefox with --marionette option:

firefox --marionette

Use the following get_url.py:

#!/usr/bin/env python3

import marionette_driver

m = marionette_driver.marionette.Marionette()

m.start_session()
print(m.get_url())
m.delete_session()

Example:

$ ./get_url.py
https://unix.stackexchange.com/questions/629440/how-to-get-the-url-from-current-tab-of-firefox-by-help-of-wmctrl

You can save output of ./get_url.py to a variable using command substitution:

$ url="$(./get_url.py )"
$ echo $url
https://unix.stackexchange.com/questions/629440/how-to-get-the-url-from-current-tab-of-firefox-by-help-of-wmctrl/629447?noredirect=1#comment1177925_629447
7
1

Here is an alternative way to write

# Search for the visible Firefox window(s) and get its window ID
window_id=$(xdotool search --onlyvisible --class "firefox")

# Send the keyboard shortcut to open the URL bar, copy the URL to clipboard and then close the URL bar by sending the Escape key.
# The command is sent to the Firefox window with the specified ID using the --window option.
xdotool key --window $window_id --delay 20 --clearmodifiers ctrl+l ctrl+c Escape

# delivery of clipboard content to variable
clipboard=`xclip -o -selection clipboard`

# clear clipboard
xsel -bc; xsel -c

# echo URL of active tab of active browser
echo $clipboard
0

You can fix the xdotool related issue on your samle code. See follow sample code for fix the xdotool bug:

#!/bin/bash

while true; do

echo Get current_wid
current_wid=$(xdotool getwindowfocus) # Get wid of current window and save this value for go bac later
echo "$current_wid"
echo
echo

echo F5 senden
xdotool windowactivate --sync "$current_wid" key --clearmodifiers F5
echo
echo

echo Fix von aktivem Fenster
xdotool windowactivate "$current_wid" # Done, now go back to where we were
echo
echo

echo "sleep 1"
sleep 1
done

echo "Zum Beenden Enter drücken oder Ctl + C."; read -r

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .