Skip to content

Instantly share code, notes, and snippets.

@mathieureguer
Last active March 9, 2018 14:26
Show Gist options
  • Save mathieureguer/9cb48c3527c1968ed2d63b808e95f0f3 to your computer and use it in GitHub Desktop.
Save mathieureguer/9cb48c3527c1968ed2d63b808e95f0f3 to your computer and use it in GitHub Desktop.
Just a little hack to fix the Extension menu sorting in Robofont 1.8 + High Sierra
# fix the Extentions menu sorting in macOS High Sierra in Robofont 1.8
# (set this up as a start up script and you're good to go)
# This is just a hack, you should really check out RoboFont 3
from AppKit import *
from lib.doodleMenus import BaseMenu
# ----------------------------------------
def sort_menu_items(menu):
controller = BaseMenu()
# collect the different sub parts
parts = []
sub_parts = []
for i in menu.itemArray():
if i.hasSubmenu():
sort_menu_items(i.submenu())
if i.isSeparatorItem():
sub_parts.append(i)
parts.append(sub_parts)
sub_parts = []
else:
sub_parts.append(i)
parts.append(sub_parts)
# sort the menu items
sorted_parts = []
for sub_parts in parts:
# get the separator out
if len(sub_parts) > 0 and sub_parts[-1].isSeparatorItem():
sep = sub_parts.pop(-1)
else:
sep = None
# keep alternates together with their parent when applicable
sub_parts_with_alternates = []
for p in sub_parts:
if not p.isAlternate():
sub_parts_with_alternates.append([p])
else:
sub_parts_with_alternates[-1].append(p)
# sort
sub_parts_with_alternates.sort(key=lambda alts: alts[0].title())
# flatten the [parent, alternate] couples
sub_parts_flat = [item for sublist in sub_parts_with_alternates for item in sublist]
# and put the separator back in
if sep:
sub_parts_flat.append(sep)
sorted_parts += sub_parts_flat
# rebuild the menu
menu.removeAllItems()
controller.buildAdditionContextualMenuItems(menu, sorted_parts)
# ----------------------------------------
menuBar = NSApp().mainMenu()
ExtMenu = menuBar.itemWithTitle_("Extensions")
menu = ExtMenu.submenu()
sort_menu_items(menu)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment