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?
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.
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.
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)
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);
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With