python - How can I find the full path to a font from its display name on a Mac?


Translate

I am using the Photoshop's javascript API to find the fonts in a given PSD.

Given a font name returned by the API, I want to find the actual physical font file that font name corresponds to on the disc.

This is all happening in a python program running on OSX so I guess I'm looking for one of:

  • Some Photoshop javascript
  • A Python function
  • An OSX API that I can call from python

Toutes les réponses
  • Translate

    Unfortunately the only API that isn't deprecated is located in the ApplicationServices framework, which doesn't have a bridge support file, and thus isn't available in the bridge. If you're wanting to use ctypes, you can use ATSFontGetFileReference after looking up the ATSFontRef.

    Cocoa doesn't have any native support, at least as of 10.5, for getting the location of a font.


  • Translate

    open up a terminal (Applications->Utilities->Terminal) and type this in:

    locate InsertFontHere
    

    This will spit out every file that has the name you want.

    Warning: there may be alot to wade through.


  • Translate

    There must be a method in Cocoa to get a list of fonts, then you would have to use the PyObjC bindings to call it..

    Depending on what you need them for, you could probably just use something like the following..

    import os
    def get_font_list():
        fonts = []
        for font_path in ["/Library/Fonts", os.path.expanduser("~/Library/Fonts")]:
            if os.path.isdir(font_path):
                fonts.extend(
                    [os.path.join(font_path, cur_font) 
                     for cur_font in os.listdir(font_path)
                    ]
                )
        return fonts
    

  • Translate

    I haven't been able to find anything that does this directly. I think you'll have to iterate through the various font folders on the system: /System/Library/Fonts, /Library/Fonts, and there can probably be a user-level directory as well ~/Library/Fonts.