Serving my own need, I hacked the following script together. It uses xdotool to get a list of running browsers (fixed to Firefox + Chrome right now). It displays the results in a list and allows you to pick the corresponding browser. It switches to the desktop (I'm using i3wm), activates the browser window and types the URL. This is definitely not the prettiest code... But it works :)
#!/usr/bin/env python3
import sys
import tkinter
import subprocess
URL = sys.argv[1] if len(sys.argv) > 1 else None
SEARCH_STRING = 'Mozilla Firefox|Google Chrome'
def get_options():
cmd = ['xdotool','search','--name',SEARCH_STRING]
result = subprocess.run(cmd, stdout=subprocess.PIPE)
window_ids = result.stdout.decode('utf-8').rstrip().split("\n")
options = []
for id in window_ids:
cmd = ['xdotool','getwindowname', id]
result = subprocess.run(cmd, stdout=subprocess.PIPE)
title = result.stdout.decode('utf-8').rstrip()
options.append((title, id))
return options
def kill_window(event = None):
root.destroy()
def select_prev_option(event):
val = curr_var.get()
idx = [i for i, option in enumerate(OPTIONS) if option[1] == val][0]
if idx > 0:
curr_var.set(OPTIONS[idx-1][1])
def select_next_option(event):
val = curr_var.get()
idx = [i for i, option in enumerate(OPTIONS) if option[1] == val][0]
if idx < len(OPTIONS)-1:
curr_var.set(OPTIONS[idx+1][1])
def execute_option(e = None):
window_id = curr_var.get()
cmd = ['xdotool', 'get_desktop']
result = subprocess.run(cmd, stdout=subprocess.PIPE)
current_desktop = int(result.stdout.decode('utf-8').rstrip())
cmd = ['xdotool', 'get_desktop_for_window', window_id]
result = subprocess.run(cmd, stdout=subprocess.PIPE)
window_desktop = int(result.stdout.decode('utf-8').rstrip())
if current_desktop != window_desktop:
cmd = ['xdotool', 'set_desktop', str(window_desktop)]
result = subprocess.run(cmd, stdout=subprocess.PIPE)
cmd = [ 'xdotool', 'windowactivate', '--sync', window_id ]
result = subprocess.run(cmd, stdout=subprocess.PIPE)
if URL:
cmd = [
'xdotool', 'key','--clearmodifiers','--window', window_id, 'ctrl+t',
'sleep', '.1',
'type', '--clearmodifiers', URL
]
result = subprocess.run(cmd, stdout=subprocess.PIPE)
cmd = ['xdotool','key','--clearmodifiers','--window', window_id, 'Return']
result = subprocess.run(cmd, stdout=subprocess.PIPE)
kill_window()
root = tkinter.Tk()
root.tk.call('tk', 'scaling', 4.0)
root.attributes('-type', 'dialog')
OPTIONS = get_options()
curr_var = tkinter.StringVar()
curr_var.set(OPTIONS[0][1])
max_len = max([len(t) for t, i in OPTIONS])
for text, mode in OPTIONS:
b = tkinter.Radiobutton(
root,
text=text,
variable=curr_var,
value=mode,
indicatoron=0,
font=("Arial", 12),
width=max_len,
anchor=tkinter.W,
command=execute_option
)
b.pack(anchor=tkinter.W)
root.bind("<j>", select_next_option)
root.bind("<Down>", select_next_option)
root.bind("<k>", select_prev_option)
root.bind("<Up>", select_prev_option)
root.bind("<Return>", execute_option)
root.bind("<Control-q>", kill_window)
root.bind("<Control-w>", kill_window)
root.protocol("WM_DELETE_WINDOW", kill_window)
root.mainloop()
I created a desktop file pointing to this script and set the default browser using:
xdg-settings set default-web-browser browserpicker.desktop
xdg-mime default browserpicker.desktop x-scheme-handler/https
xdg-mime default browser.desktop x-scheme-handler/http