Skip to content

Instantly share code, notes, and snippets.

@martin-ueding
Created January 8, 2019 11:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-ueding/45c7ae78a091db3accfcffaf65588d9f to your computer and use it in GitHub Desktop.
Save martin-ueding/45c7ae78a091db3accfcffaf65588d9f to your computer and use it in GitHub Desktop.
Script to move Plasma panel to the primary screen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright © 2019 Martin Ueding <dev@martin-ueding.de>
# Licensed under the MIT/Expat license.
# Based on https://bugs.kde.org/show_bug.cgi?id=356225#c312
import argparse
import configparser
import difflib
import os
import subprocess
def get_current_primary():
output = subprocess.check_output('xrandr').decode()
lines = output.split('\n')
for line in lines:
if 'connected primary' in line:
words = line.split()
return words[0]
raise RuntimeError('No primary screen could be found')
def get_plasmashell_screen_id(identifier):
config = configparser.ConfigParser(strict=False)
config.read(os.path.expanduser('~/.config/plasmashellrc'))
for key, value in config['ScreenConnectors'].items():
if value == identifier:
return key
raise RuntimeError('Screen could not be found in plasmashellrc')
def set_appletsrc_screen(primary_id, dry):
path = os.path.expanduser('~/.config/plasma-org.kde.plasma.desktop-appletsrc')
with open(path) as f:
content = f.read()
paragraphs = content.split('\n\n')
for i, paragraph in enumerate(paragraphs):
lines = paragraph.split('\n')
if any(line == 'plugin=org.kde.panel' for line in lines):
others = [line for line in lines if not line.startswith('lastScreen=')]
others.append('lastScreen=' + primary_id)
paragraphs[i] = '\n'.join(others)
new_content = '\n\n'.join(paragraphs)
diff = list(difflib.unified_diff(content.split('\n'), new_content.split('\n')))
if len(diff) > 0:
print('Will make the following changes:')
for d in diff:
print(d)
if not dry:
with open(path, 'w') as f:
f.write(new_content)
else:
print('No changes necessary')
def main():
options = _parse_args()
primary_name = get_current_primary()
print('Found primary screen: {}'.format(primary_name))
primary_id = get_plasmashell_screen_id(primary_name)
print('Found matching ID: {}'.format(primary_id))
set_appletsrc_screen(primary_id, options.dry)
def _parse_args():
'''
Parses the command line arguments.
:return: Namespace with arguments.
:rtype: Namespace
'''
parser = argparse.ArgumentParser(description='Changes the `lastScreen` attributes of the plasmashell containments such that the panel is on the primary screen.')
parser.add_argument('--dry', '-n', action='store_true', help='Change nothing, only display proposed changes')
options = parser.parse_args()
return options
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment