Last active
July 3, 2022 16:31
-
-
Save bgalvao/02eec5f4f9dd62822c2007f821e610f0 to your computer and use it in GitHub Desktop.
Make the fix so that kde shows chrome app icons, like in https://superuser.com/a/1068709. Loops through desktop files in ~/Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
from shutil import copyfile | |
home_dir = os.getenv('HOME') # only works on Linux | |
desktop_dir = os.path.join(home_dir, 'Desktop') | |
chrome_based_browsers = ['brave', 'chrome', 'chromium', 'vivaldi'] | |
def get_wm_class(desktop_file_contents): | |
wm_class = next(filter(lambda s: 'StartupWMClass' in s, desktop_file_contents))\ | |
.split('=')[-1].replace('\n', '') | |
return wm_class | |
def make_new_exec_line(desktop_file_contents, wm_class): | |
el = next(filter(lambda s: 'Exec=' in s, desktop_file_contents)) | |
exec_line = el.replace('\n', '') | |
xdo_append = f' && xdotool search --sync --classname {wm_class} set_window --class {wm_class}\n' | |
return exec_line + xdo_append, desktop_file_contents.index(el) | |
def check_if_xdotool_written(desktop_file_contents): | |
return any(filter(lambda s: 'xdotool' in s, desktop_file_contents)) | |
for f in os.listdir(desktop_dir): | |
if not any([(browser in f) for browser in chrome_based_browsers]): | |
continue | |
desktop_file = os.path.join(desktop_dir, f) | |
desktop_file_contents = open(desktop_file, 'r').readlines() | |
if check_if_xdotool_written(desktop_file_contents): | |
print('skipping because xdotool is already written to', desktop_file) | |
continue | |
wm_class = get_wm_class(desktop_file_contents) | |
new_exec_line, idx = make_new_exec_line(desktop_file_contents, wm_class) | |
desktop_file_contents[idx] = new_exec_line | |
with open(desktop_file, 'w') as nf: | |
nf.writelines(desktop_file_contents) | |
print('written new xdotool line') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment