Skip to content

Instantly share code, notes, and snippets.

@ryanbugden
Last active July 24, 2023 07:57
Show Gist options
  • Select an option

  • Save ryanbugden/d90b284ec9a2648ede01d944e84ad5e9 to your computer and use it in GitHub Desktop.

Select an option

Save ryanbugden/d90b284ec9a2648ede01d944e84ad5e9 to your computer and use it in GitHub Desktop.
RF start-up script that adds a menu item which saves all open fonts.
from AppKit import NSApp, NSMenuItem, NSAlternateKeyMask, NSCommandKeyMask
from mojo.tools import CallbackWrapper
from mojo.subscriber import Subscriber, registerRoboFontSubscriber
from mojo.UI import PutFile
class saveAllFontsMenuItem(Subscriber):
'''
A startup script that places a menu item in the File dropdown menu.
Clicking this item will save all open fonts and disregard any other
types of unsaved documents like scripts.
Ryan Bugden
'''
def build(self):
# put in the menu item
title = "Save All Open Fonts"
self.main_menu = NSApp().mainMenu()
font_menu = self.main_menu.itemWithTitle_("File")
if not font_menu:
print("Save All Fonts Menu Item - Error")
return
font_menu = font_menu.submenu()
if font_menu.itemWithTitle_(title):
return
index = font_menu.indexOfItemWithTitle_("Save All")
self.target = CallbackWrapper(self.saveAllFonts)
self.new_item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(title, "action:", "")
# self.new_item.setKeyEquivalentModifierMask_(NSAlternateKeyMask | NSCommandKeyMask)
self.new_item.setTarget_(self.target)
# self.new_item.setIndentationLevel_(1)
font_menu.insertItem_atIndex_(self.new_item, index+1)
def saveAllFonts(self, sender):
for f in AllFonts():
if f.path == None:
fam_name, sty_name = str(f.info.familyName), str(f.info.styleName)
path = PutFile(f"Save {fam_name} {sty_name} as...", f"{fam_name}-{sty_name}.ufo")
f.save(path)
else:
f.save()
if __name__ == '__main__':
registerRoboFontSubscriber(saveAllFontsMenuItem)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment