-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactwin.py
executable file
·88 lines (65 loc) · 2.45 KB
/
actwin.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
#!/usr/bin/env python3
"""
Copyright Sean Bruton
Contact sbruton$$$$ᣣ££tcd.ie. (Take away the currency symbols)
"""
import argparse
import sys
import subprocess
def cmd_output_to_str_list(output):
# TODO: Sense checks
output_str = str(output)
output_list = output_str.split('\\n')
# Remove the bytes marker from the first element
output_list_as_str = [output_list[0][2:]]
# Trim the last apostrophe
for idx in range(1, len(output_list) - 1):
output_list_as_str.append(output_list[idx])
return output_list_as_str
def get_current_desktop():
# TODO: Test this for no valid desktop, e.g. ssh connection
current_desktop = subprocess.check_output(['xdotool get_desktop'],
shell=True)
return int(current_desktop)
def get_window_ids(name):
cmd = 'xdotool search --class ' + name
try:
output = subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError as e:
raise ValueError("Could not match search pattern: {}".format(name))
output_str_list = cmd_output_to_str_list(output)
output_int_list = [int(el) for el in output_str_list]
return output_int_list
def find_window_id_on_current_desktop(window_ids, desktop_id):
cmd = 'wmctrl -l'
output = subprocess.check_output(cmd, shell=True)
output_str_list = cmd_output_to_str_list(output)
ids_desktops_list = [el.split()[:2] for el in output_str_list]
for window in ids_desktops_list:
if int(window[1]) == desktop_id:
if int(window[0], 0) in window_ids:
return int(window[0], 0)
raise ValueError("No window on desktop matching search pattern")
def activate_window(name):
try:
window_ids = get_window_ids(name)
except ValueError as e:
print(e)
return 1
curr_desktop = get_current_desktop()
try:
id = find_window_id_on_current_desktop(window_ids, curr_desktop)
except ValueError as e:
print(e)
return 1
cmd = 'xdotool windowactivate ' + str(id)
subprocess.check_output(cmd, shell=True)
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Activate window on desktop matching a name")
parser.add_argument("searchname",
help="The search name of the window, e.g. chrome",
type=str)
args = parser.parse_args()
sys.exit(activate_window(args.searchname))