Generate instances remotely (Glyphs SDK)

Hey!

I am tinkering a bit with the code from the “remote scripts” folder in the Glyphs SDK repository.

I am trying to export some instances from a .glyphs file with this script:

import os
import time

from Foundation import NSURL, NSConnection, NSObject  #  type: ignore


def application(appName, port=None):
    if port is None:
        port = "com.GeorgSeifert.Glyphs3"
    conn = None
    tries = 0

    while (conn is None) and (tries < 10):
        conn = NSConnection.connectionWithRegisteredName_host_(port, None)
        tries = tries + 1

        if not conn:
            time.sleep(1)

    if not conn:
        print("Could not find a JSTalk connection to " + appName)
        return None

    return conn.rootProxy()


class GSStdOut(NSObject):
    def setWrite_(self, text):
        print(text, end="")

    def setWriteError_(self, text):
        print(text, end="")


def export_instances():
    """
    This will export all instances of the font at 'path' as TrueType fonts.
    """
    path = os.path.expanduser("~/Desktop/test/font_name.glyphs")
    doc = Glyphs.openDocumentWithContentsOfFile_display_(path, False)  # type: ignore
    print("Exporting:", doc.displayName())
    font = doc.font()
    for instance in font.instances():
        print("Instance:", instance)
        instance.generate_(
            {
                "ExportFormat": "TTF",
                "ExportContainer": "woff",
                "Destination": NSURL.fileURLWithPath_(
                    os.path.expanduser("~/Desktop/test/")
                ),
            }
        )

    doc.close()
    print("Ready!")


if __name__ == "__main__":
    Glyphs = application("Glyphs")
    GSApplication = Glyphs
    if Glyphs and Glyphs.orderedDocuments():
        currentDocument = Glyphs.orderedDocuments()[0]
    else:
        currentDocument = None
    export_instances()

I get this output in the terminal

Exporting: font_name.glyphs
Instance: <GSInstance 0x600006674f00> Regular (100.0, 100.0, 0.0, 0.0)
Instance: <GSInstance 0x6000066770c0> Thin (70.0, 49.0, 0.0, 100.0) (Contrast Thin)
Instance: <GSInstance 0x600006675440> Light (70.0, 73.0, 0.0, 100.0) (Contrast Light)
Instance: <GSInstance 0x600006677900> Regular (70.0, 98.0, 0.0, 100.0) (Contrast Regular)
Instance: <GSInstance 0x600006677600> Medium (70.0, 120.0, 0.0, 100.0) (Contrast Medium)
Instance: <GSInstance 0x600006676580> Bold (70.0, 156.0, 0.0, 100.0) (Contrast Bold)
Instance: <GSInstance 0x600006677480> Heavy (70.0, 190.0, 0.0, 100.0) (Contrast Heavy)
Instance: <GSInstance 0x600006675ec0> Black (70.0, 228.0, 0.0, 100.0) (Contrast Black)
Instance: <GSInstance 0x600006676f40> Ultra (70.0, 279.0, 0.0, 100.0) (Contrast Ultra)
Ready!

but no export in Desktop/test. Any clue?

Does the “test” folder exist? If not, it seems to skip it and put the files directly on the desktop.

Hey Georg, thanks for your quick reply. The test folder exists and it also contains the .glyphs file opened.

It works for me. Can you send me the .glyphs file you are trying to export?

Tried another .glyphs file and it works, sorry.
But I have a couple more questions:

  • should font.instances() include also variable fonts exports? I only see static files in the script output
  • Does the ExportFormat setting corresponds to “outline flavor” from the OTF panel?
  • And then ExportContainer to “file format”?

Yes.

Yes

Yes.

What are the options here? TTF works fine but OTF does not, it crashes the app.

Can you post the crashing code?

sent via email along with the .glyphs file.

I can’t reproduce the crash.

For variable instances, you need to add

"removeOverlap": False

The .generate_ method wasn’t aware of variable instance and defaults to True.