-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowserpicker_tkinter.py
executable file
·226 lines (189 loc) · 7.28 KB
/
browserpicker_tkinter.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/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'
BROWSERS = {
"Mozilla Firefox": "firefox",
"Google Chrome": "google-chrome",
}
def get_options():
"""Get active browser windows using xdotool."""
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:
if id: # Skip empty 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 launch_browser(browser_command, url=None):
"""Launch the specified browser with the given URL."""
try:
if browser_command == "google-chrome":
cmd = [browser_command, "--show-profile-picker"]
if url:
cmd.append(url)
subprocess.Popen(cmd)
else:
if url:
subprocess.Popen([browser_command, url])
else:
subprocess.Popen([browser_command])
print(f"Launching {browser_command}...")
kill_window() # Close the picker window after launching
except FileNotFoundError:
print(f"Error: {browser_command} is not installed or not in PATH.")
def get_active_monitor_geometry():
"""Get the geometry of the monitor where the mouse pointer is located."""
# Get mouse position
cmd = ['xdotool', 'getmouselocation', '--shell']
result = subprocess.run(cmd, stdout=subprocess.PIPE)
output = result.stdout.decode('utf-8').splitlines()
mouse_x, mouse_y = None, None
for line in output:
if line.startswith("X="):
mouse_x = int(line.split("=")[1])
if line.startswith("Y="):
mouse_y = int(line.split("=")[1])
if mouse_x is None or mouse_y is None:
return None # Fallback to default behavior if mouse position is not found
# Get monitor geometry from xrandr
cmd = ['xrandr']
result = subprocess.run(cmd, stdout=subprocess.PIPE)
xrandr_output = result.stdout.decode('utf-8').splitlines()
for line in xrandr_output:
if " connected" in line:
# Extract geometry, e.g., "1920x1080+0+0"
parts = line.split()
for part in parts:
if "x" in part and "+" in part: # Ensure it's a valid geometry string
res, offset_x, offset_y = part.split("+")
width, height = map(int, res.split("x"))
offset_x, offset_y = int(offset_x), int(offset_y)
# Check if the mouse pointer is within this monitor's bounds
if offset_x <= mouse_x < offset_x + width and offset_y <= mouse_y < offset_y + height:
return width, height, offset_x, offset_y
return None # Fallback if no monitor geometry matches
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(event=None):
selected_option = curr_var.get()
# Handle selecting an active browser window
window_id = selected_option
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)]
subprocess.run(cmd, stdout=subprocess.PIPE)
cmd = ['xdotool', 'windowactivate', '--sync', window_id]
subprocess.run(cmd, stdout=subprocess.PIPE)
if URL:
cmd = [
'xdotool', 'key', '--clearmodifiers', '--window', window_id, 'ctrl+t',
'sleep', '.1',
'type', '--clearmodifiers', URL
]
subprocess.run(cmd, stdout=subprocess.PIPE)
cmd = ['xdotool', 'key', '--clearmodifiers', '--window', window_id, 'Return']
subprocess.run(cmd, stdout=subprocess.PIPE)
kill_window()
# Initialize GUI
root = tkinter.Tk()
root.title("Select Browser to Open Link")
# Get active browser windows
OPTIONS = get_options()
# Position window on the active monitor
monitor_geometry = get_active_monitor_geometry()
if monitor_geometry:
monitor_width, monitor_height, monitor_offset_x, monitor_offset_y = monitor_geometry
window_width = 800
window_height = 400
x = monitor_offset_x + (monitor_width - window_width) // 2
y = monitor_offset_y + (monitor_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
else:
root.geometry("800x400") # Default size and position fallback
# Add active browser options
if OPTIONS:
curr_var = tkinter.StringVar()
curr_var.set(OPTIONS[0][1])
# Add section label for active browsers
active_label = tkinter.Label(
root,
text="Active Browser Windows:",
font=("Ubuntu", 10, "bold"),
anchor=tkinter.W
)
active_label.pack(pady=(10,5), padx=10, fill=tkinter.X)
# Create frame for active browsers
button_frame = tkinter.Frame(root)
button_frame.pack(fill=tkinter.BOTH, expand=True)
buttons = []
for text, mode in OPTIONS:
b = tkinter.Radiobutton(
button_frame,
text=text,
variable=curr_var,
value=mode,
indicatoron=0,
font=("ubuntu", 11, "bold"),
anchor=tkinter.W,
command=execute_option
)
b.pack(fill=tkinter.X, pady=5, padx=5)
buttons.append(b)
# Add section label for new browser launches
new_browser_label = tkinter.Label(
root,
text="Launch New Browser Instance:",
font=("ubuntu", 10, "bold"),
anchor=tkinter.W
)
new_browser_label.pack(pady=(20,5), padx=10, fill=tkinter.X)
# Add buttons for launching new browsers
def add_launch_button(browser_name):
browser_command = BROWSERS[browser_name]
btn = tkinter.Button(
root,
text=f"Open in {browser_name}",
font=("ubuntu", 11, "bold"),
command=lambda: launch_browser(browser_command, URL),
)
btn.pack(pady=5, padx=10, fill=tkinter.X)
for browser in BROWSERS:
add_launch_button(browser)
# Footer with instructions
footer = tkinter.Label(
root,
text="Use ↑/↓ to navigate, Enter to select, or click buttons to open a browser.",
font=("ubuntu", 10, "italic"),
fg="gray"
)
footer.pack(side=tkinter.BOTTOM, pady=10)
# Keyboard bindings for navigation
if OPTIONS:
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.mainloop()