-
Notifications
You must be signed in to change notification settings - Fork 0
/
title_gnome.py
61 lines (49 loc) · 2.11 KB
/
title_gnome.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
import subprocess
import ast
import time
title_left = '<DeaDBeeF> '
title_right = ' </DeaDBeeF>'
suffix_default = title_right
functions = {'title': 'getWindowsByTitle',
'prefix': 'getWindowsByPrefix',
'suffix': 'getWindowsBySuffix',
'substring': 'getWindowsBySubstring'}
command = ("gdbus call --session --dest org.gnome.Shell --object-path /io/github/jieran233/GetAllTitlesOfWindows "
"--method io.github.jieran233.GetAllTitlesOfWindows.{} '{}'").format(functions['suffix'], suffix_default)
output_left = ['(', '(@as']
output_right = ',)'
def _changedTitleCB(_title: str): # Callback function for handling title changes
print("NEW title", _title) # Printing the new title
def set_title_change_polling(callback=_changedTitleCB, _command=command):
last_title = None
while True:
try:
# Use subprocess.Popen to start the system command and capture its output through stdout=subprocess.PIPE
process = subprocess.Popen(_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
# Read command output
output = process.stdout.readline()
if not output:
break
output = output.decode().strip() # Remove newlines from output and print
# print(output)
for lstrip in output_left:
output = output.lstrip(lstrip)
output = output.rstrip(output_right)
output = output.strip()
titles_list = ast.literal_eval(output)
if titles_list: # Check if the list is not blank
title = titles_list[0]
if title != last_title:
last_title = title
callback(title)
# Wait for the command to complete
process.wait()
except KeyboardInterrupt:
break
except Exception as e:
print("title_gnome Error:", e)
raise e
time.sleep(0.1)
if __name__ == '__main__':
set_title_change_polling()