Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of installed fonts OS X / C

I'm trying to programatically get a list of installed fonts in C or Python. I need to be able to do this on OS X, does anyone know how?

like image 517
user134600 Avatar asked Jul 11 '09 05:07

user134600


People also ask

How do I find all the Fonts installed on my Mac?

In the Font Book app on your Mac, select a font collection in the sidebar to see the fonts in it. If the preview pane isn't shown, choose View > Show Preview. All Fonts: Every font associated with the Computer and User collections, as well as additional system fonts available for download.

How can I get a list of installed Fonts?

View Installed Fonts In Windows 10 or 11, type Control Panel in the search field and select it from the results. With Control Panel in Icon View, click the Fonts icon. Windows displays all the installed fonts.


4 Answers

Python with PyObjC installed (which is the case for Mac OS X 10.5+, so this code will work without having to install anything):

import Cocoa
manager = Cocoa.NSFontManager.sharedFontManager()
font_families = list(manager.availableFontFamilies())

(based on htw's answer)

like image 164
Miles Avatar answered Oct 12 '22 23:10

Miles


Why not use the Terminal?

System Fonts:

ls -R /System/Library/Fonts | grep ttf

User Fonts:

ls -R ~/Library/Fonts | grep ttf

Mac OS X Default fonts:

ls -R /Library/Fonts | grep ttf

If you need to run it inside your C program:

void main()
{ 
    printf("System fonts: ");
    execl("/bin/ls","ls -R /System/Library/Fonts | grep ttf", "-l",0);
    printf("Mac OS X Default fonts: ");
    execl("/bin/ls","ls -R /Library/Fonts | grep ttf", "-l",0);
    printf("User fonts: ");
    execl("/bin/ls","ls -R ~/Library/Fonts | grep ttf", "-l",0);
}
like image 42
Brock Woolf Avatar answered Oct 12 '22 23:10

Brock Woolf


Not exactly C, but in Objective-C, you can easily get a list of installed fonts via the Cocoa framework:

// This returns an array of NSStrings that gives you each font installed on the system
NSArray *fonts = [[NSFontManager sharedFontManager] availableFontFamilies];

// Does the same as the above, but includes each available font style (e.g. you get
// Verdana, "Verdana-Bold", "Verdana-BoldItalic", and "Verdana-Italic" for Verdana).
NSArray *fonts = [[NSFontManager sharedFontManager] availableFonts];

You can access the Cocoa framework from Python via PyObjC, if you want.

In C, I think you can do something similar in Carbon with the ATSUI library, although I'm not entirely sure how to do this, since I haven't worked with fonts in Carbon before. Nevertheless, from browsing the ATSUI docs, I'd recommend looking into the ATSUGetFontIDs and the ATSUGetIndFontName functions. Here's a link to the ATSUI documentation for more information.

like image 28
hbw Avatar answered Oct 13 '22 01:10

hbw


Do you want to write a program to do it, or do you want to use a program to do it? There are many programs that list fonts, xlsfonts comes to mind.

like image 44
Mike Mu Avatar answered Oct 12 '22 23:10

Mike Mu