Detect unsaved changes via script



  • I often want to switch from one batch of UFOs to another, without manually closing glyphs, space centers, and font views of 4, 6, or more masters.

    So, I made a simple script:

    for font in AllFonts():
        font.close()
    

    However, this doesn't prompt me to save unsaved changes, so it's currently risky to use. I've searched around for how to detect unsaved changes, but I haven't found the answer yet. Is there some kind of method to check the saved state of open fonts? Alternatively, if it is required to string together observers to check this, is there some combination that would help check for unsaved changes? What I'd ultimately like is a script like this...

    ### The script I want, but don't quite have 
    
    from mojo.UI import dontShowAgainMessage
    
    unsavedFonts = []
    
    for font in AllFonts():
        fontName = font.info.familyName + " " + font.info.styleName
    
        if font.isSaved() is False: #### ✴️ this is what I'd like to check ✴️ ####
            unsavedFonts.append(fontName)
            
        else:
            font.close()
        
    unsavedMessage = "There are unsaved changes in " + unsavedFonts + ". Please close them manually."
    
    dontShowAgainMessage(messageText='Unsaved changes', informativeText=unsavedMessage, alertStyle=1, parentWindow=None, resultCallback=None, dontShowAgainKey='')
    

    Thanks for any pointers!


  • admin

    you can use:

    f = CurrentFont()
    
    document = f.document()
    edited = None
    if document:
        # only font objects with ui has a document object
        edited = document.isDocumentEdited()
    
    print(edited)
    

    but this is good candidate to be added to fontParts


  • admin



  • Oops; nevermind!

    Just found out that macOS has a built-in feature to do this exact thing.

    Just use the shortcut option + command + w.


Log in to reply
 

Looks like your connection to RoboFont ● Forum was lost, please wait while we try to reconnect.