Get Menulings
Posted on December 23, 2010
Filed Under Applescript, Python | Leave a Comment
Need to get the list of menulings on your menubar? Menulings are those small icons that do things like control your wireless, battery or the like. If you do need a list here’s a bit of python code to do it. (Note it assumes you have Appscript)
def getmenuings(): menus = app(u'System Events').processes[u'SystemUIServer'].menu_bars[1].attributes[u'AXChildren'].value.get() l = [] for m in menus: l.append(m.attributes[u'AXDescription'].value.get()) return l |
This returns a list of strings representing each menuling.
Why would you want to do this? Well, you could check, for instance, to see if the Spaces menuling is active. (This is activated via the Spaces/Expose system preference — click the “Show Spaces in menu bar” checkbox) You could use this to figure out the current space you are in.
#!/usr/bin/python import sys from appscript import * def getmenuings(): # returns a list of menulings represented as strings menus = app(u'System Events').processes[u'SystemUIServer'].menu_bars[1].attributes[u'AXChildren'].value.get() l = [] for m in menus: l.append(m.attributes[u'AXDescription'].value.get()) return l def getspacemenu(): # returns the menuling number of the spaces menuling menus = app(u'System Events').processes[u'SystemUIServer'].menu_bars[1].attributes[u'AXChildren'].value.get() i = 0 for m in menus: name = m.attributes[u'AXDescription'].value.get() if name == u'spaces menu extra': return i i = i + 1 def getcurrentspace(menu): # given the menu of the spaces menuling this gets the current space return app(u'System Events').processes[u'SystemUIServer'].menu_bars[menu].menu_bar_items[0].value.get() def test(): m = getmenuings() if u'spaces menu extra' in m: c = getspacemenu() print getcurrentspace(c) if __name__ == '__main__': test() # change to 0 for success, 1 for (partial) failure sys.exit(0) |
Comments
Leave a Reply