/
openlink.py
executable file
·82 lines (62 loc) · 2.25 KB
/
openlink.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
"""Open a URL in a general-purpose Firefox window,
ignoring small windows that might be used for specific purposes
like social networking or chat.
Copyright 2023 by Akkana Peck: Share and enjoy under the GPLv2 or later.
"""
import sys, os
import subprocess
import time
def open_in_existing_firefox(url, minwidth=800):
"""Find the oldest firefox window that's bigger than minwidth pixels
and open a URL in it using xdotool events.
"""
def find_biggest_firefox_window_and_desktop():
maxwidth = 0
biggest_win = (None, None)
proc = subprocess.run(["wmctrl", "-l", "-G"], capture_output=True)
for line in proc.stdout.splitlines():
if not line.endswith(b'irefox'):
continue
words = line.split()
windowid = words[0]
desktop = words[1]
w = int(words[4])
if w < minwidth:
continue
if w > maxwidth:
maxwidth = w
biggest_win = (windowid, desktop, w)
return biggest_win
# Find the widest firefox window
windowid, desktop, winwidth = find_biggest_firefox_window_and_desktop()
if not windowid:
print("Can't find a firefox process")
return False
# Switch to appropriate desktop if needed
if desktop:
subprocess.call(['wmctrl', '-s', desktop])
# This sometimes doesn't finish in time, and part of the text
# ends up in the wrong window. So delay slightly:
time.sleep(.5)
# Move mouse to center of urlbar (winwidth/2)
subprocess.call(['xdotool', 'mousemove', '--window', windowid,
f'{winwidth/2}', '75'])
time.sleep(.3)
# Open a new tab
subprocess.call(['xdotool', 'keydown', 'Ctrl', 'keydown', 't',
'keyup', 't', 'keyup', 'Ctrl'])
time.sleep(.3)
# insert url
subprocess.call(['xdotool', 'type', url])
time.sleep(.1)
# hit Enter to go there
subprocess.call(['xdotool', 'keydown', 'Return', 'keyup', 'Return'])
# Return apparent success
return True
def quit(event):
print("Quitting")
sys.exit(0)
if __name__ == '__main__':
for url in sys.argv[1:]:
open_in_existing_firefox(url)