Hello everyone !!
I’m fresh new on linux (using Nobara) since 2 days.
i’m currently trying to create a script which will automatically hide desktop widget when there is no mouse (or keyboard) input for a determined time (in my case 5 seconds).
I very bad at coding so i’m using an AI to do most of the job but nothing it create actually works.
Bellow are the steps i’m supposed to do, along with the script;
1:
sudo dnf install python3-dbus python3-pip
pip install pydbus --user
2:
mkdir -p ~/scripts/
nano ~/scripts/toggle_plasma_widgets.py[test
SCRIPT
#!/usr/bin/env python3
import dbus
import time
from pydbus import SessionBus
— CONFIGURATION —
IDLE_THRESHOLD = 5000 # 5 secondes d’inactivité (ms)
CHECK_INTERVAL = 0.5 # Vérification toutes les 0.5s
LOCK_FILE = “/tmp/toggle_widgets.lock”
— FONCTION POUR ARRÊTER LE SCRIPT —
def kill_myself():
print(“[ERREUR] Arrêt du script.”)
try:
import os
os.remove(LOCK_FILE)
except:
pass
exit(1)
— VÉRIFIER SI ON DOIT TUER LE SCRIPT —
import os
if os.path.exists(LOCK_FILE):
print(“[INFO] Fichier lock détecté. Arrêt du script.”)
os.remove(LOCK_FILE)
exit(0)
— FONCTION POUR RÉCUPÉRER LE TEMPS D’INACTIVITÉ (KIdleTime API) —
def get_idle_time():
try:
bus = SessionBus()
kidletime = bus.get(‘org.kde.KIdleTime’, ‘/org/kde/KIdleTime’)
return kidletime.idleTime()
except Exception as e:
print(f"[ERREUR KIdleTime] {e}")
return 0
— FONCTION POUR MASQUER/AFICHER LES WIDGETS —
def toggle_widgets(hide):
try:
bus = SessionBus()
plasmashell = bus.get(‘org.kde.plasmashell’, ‘/PlasmaShell’)
# Lister les widgets du bureau (location=1)
widgets = bus.call_blocking(
'org.kde.plasmashell',
'/PlasmaShell',
'org.kde.PlasmaShell',
'plasmoidItems',
'',
signature='',
reply_signature='a{sa{sv}}'
)
for widget_path, widget_props in widgets.items():
if widget_props.get('location', 0) == 1: # 1 = Desktop
plasmashell.call_blocking(
'org.kde.PlasmaShell',
'/PlasmaShell',
'org.kde.PlasmaShell',
'setPlasmoidVisibility',
'ssb',
(widget_path, "hidden", hide),
signature='ssb'
)
except Exception as e:
print(f"[ERREUR plasmoid] {e}")
— BOUCLE PRINCIPALE —
if name == “main”:
error_count = 0
max_errors = 3
while True:
idle_time = get_idle_time()
if idle_time == 0:
error_count += 1
print(f"[ERREUR] Impossible de récupérer idleTime (attempt {error_count}/{max_errors})")
if error_count >= max_errors:
kill_myself()
time.sleep(CHECK_INTERVAL)
continue
error_count = 0
toggle_widgets(idle_time > IDLE_THRESHOLD)
time.sleep(CHECK_INTERVAL)
3:
chmod +x ~/scripts/toggle_plasma_widgets.py
4:
~/scripts/toggle_plasma_widgets.py
On step 4 i get the following error:
[ERREUR KIdleTime] g-dbus-error-quark: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name is not activatable (2)
[ERREUR] Impossible to retreave idleTime (attempt 3/3)
Feel free to ask my for translation in the script ![]()
(I’m on Nobara 44, KDE version ofc, and Linux 7.2.4)
thank you!