Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Parse GTK Menu Model
from __future__ import print_function
import re
import signal
import subprocess
import sys
from gi.repository import Gio, Gtk, GLib
xwininfo = subprocess.check_output(["xwininfo"]).decode('utf-8')
xid = int('Window id: (0x[0-9A-Fa-f]+) ', xwininfo).group(1), 16)
def get_x_property(xid, prop):
value = subprocess.check_output("xprop {} -id {}".format(prop, xid).split()).decode('utf-8')
return' = "([^\"]+)', value).group(1)
# Workaround Ctrl+C not working with gio-gtk3
signal.signal(signal.SIGINT, signal.SIG_DFL)
bus = Gio.bus_get_sync(Gio.BusType.SESSION)
menus = Gio.DBusMenuModel.get(bus, get_x_property(xid, "_GTK_UNIQUE_BUS_NAME"),
get_x_property(xid, "_GTK_MENUBAR_OBJECT_PATH"))
menus_ref = [menus]
def iterate_menus(menus, level=0):
global menus_ref
print(level*"\t","callback,",menus," menusize: ",menus.get_n_items())
for i in range(menus.get_n_items()):
miter = menus.iterate_item_attributes(i)
print(level*"\t","attr:",miter.get_name()," = ",miter.get_value())
miter = menus.iterate_item_links(i)
menu = miter.get_value()
print(level*"\t","link:",miter.get_name()," = ",menu)
menu.connect("items-changed", on_items_changed, level+1)
def on_items_changed(menus, position, removed, added, level=0):
iterate_menus(menus, level)
menus.connect("items-changed", on_items_changed)
menus.get_n_items(), False).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.