Skip to content

Instantly share code, notes, and snippets.

@clsv
Last active April 15, 2023 19:54
Show Gist options
  • Save clsv/88d96725d7c4159ec7a43117c71e3597 to your computer and use it in GitHub Desktop.
Save clsv/88d96725d7c4159ec7a43117c71e3597 to your computer and use it in GitHub Desktop.
Send notification in XFCE4 when current workspace was changed
#!/bin/env python3
from Xlib import X, display
import subprocess
# Open the display
d = display.Display()
# Get the root window
root = d.screen().root
# Select for SubstructureNotifyMask and PropertyChangeMask events
root.change_attributes(event_mask=X.SubstructureNotifyMask | X.PropertyChangeMask)
# Flush the display
d.flush()
# Id for notification
notify_id = "0"
# Get the names of all workspaces
workspaces = subprocess.check_output(['wmctrl', '-d']).decode().splitlines()
# Map the workspace numbers to their names
workspace_names = {i: name.split()[9] for i, name in enumerate(workspaces)}
# Get the initial workspace number
prev_workspace = root.get_full_property(d.intern_atom('_NET_CURRENT_DESKTOP'), X.AnyPropertyType).value[0]
# Enter the event loop
while True:
event = d.next_event()
if event.type == X.PropertyNotify and event.atom == d.intern_atom('_NET_CURRENT_DESKTOP'):
# Handle workspace change event
# Get the current workspace number
curr_workspace = root.get_full_property(d.intern_atom('_NET_CURRENT_DESKTOP'), X.AnyPropertyType).value[0]
# Check if the workspace has changed
if curr_workspace != prev_workspace:
# Handle the workspace change event
workspace_name = workspace_names.get(curr_workspace, f"Workspace {curr_workspace}")
notify_id = subprocess.check_output(['notify-send', "-i", "workspace-switcher-right-bottom", "-t", "3000", "-p", "-r", f"{notify_id}", "Workspace", f"{curr_workspace} - {workspace_name}"]).decode().splitlines()[0]
# Update the previous workspace number
prev_workspace = curr_workspace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment