Determining the Path to a Font file

It would certainly help.

To actually make a class, go to File → New File, and choose Objective-C class; give it a name, say MyNewClass. You will get two new files. In the .m file, paste the method code from Stefan or Ric between the @implementation and @end statements, then save. Got to the .h file and paste in just the signature of the method – up to the first { – before the @end line but outside the { and }. Instead of ending it with a {, end it with a semicolon, like this:

@interface MyNewClass : NSObject {
}

  • (NSString *)pathToFontNamed:(NSString *)fontName size:(CGFloat)fontSize;

@end

Save, and you’re set. You can either instantiate it (see the chapter Instantiating Script Objects), or you can just change the parent property of your script to the new class, and inherit the method.

Thanks. I’ll give it a try.

Greetings, It’s been a long time!

I need some help please. I’ve been using this class in a project I started 10 years ago and it’s been working fine, but now won’t compile. Below is the code as it stands now. I know there have been a lot of changes over the years, that I’m not up to date on, so was hoping a kind soul would be able to save me :). Thanks so much!

[format]@implementation MyPathToFontClass

  • (NSString *)pathToFontNamed:(NSFont *)fontName size:(CGFloat)fontSize
    {
    CTFontDescriptorRef fontRef = CTFontDescriptorCreateWithNameAndSize ((const struct __CFString *)fontName, fontSize);
    CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute);
    NSString *fontPath = [(__bridge NSURL *)url path];
    //CFRelease(fontRef);
    //CFRelease(url);
    return fontPath;
    }
    @end[/format]

I’m getting the following issues:

[format]Unknown type name ‘CTFontDescriptorRef’; did you mean ‘CFFileDescriptorRef’?
Implicit declaration of function ‘CTFontDescriptorCreateWithNameAndSize’ is invalid in C99
Implicit declaration of function ‘CTFontDescriptorCopyAttribute’ is invalid in C99
Use of undeclared identifier ‘kCTFontURLAttribute’

  • (NSString *)pathToFontNamed:(NSFont *)fontName size:(CGFloat)fontSize > Expected a type
    Cast of Objective-C pointer type ‘id’ to C pointer type ‘const struct __CFString *’ requires a bridged cast
    Use __bridge to convert directly (no change in ownership)
    Use CFBridgingRetain call to make an ARC object available as a +1 ‘const struct __CFString *’
    Incompatible integer to pointer conversion initializing ‘CFFileDescriptorRef’ (aka ‘struct __CFFileDescriptor *’) with an expression of type ‘int’[/format]

@mac.jedi

The first line of the error message tells you that the compiler does not understand the ‘CTFontDescriptorRef’ class type.

So I suspect that you have upgraded to a newer operating system version, from the one used when creating your original code, and the ‘CTFontDescriptorRef’ class type is no longer supported on the newer OS version.

It’s important to quote your OS version when posting code on this forum, as a lot has changed to the newer “Big Sur” & “Monterey” operating system versions.

This works for me on “High Sierra”, but I have not checked it on my “Mojave” or “Catalina” partitions.


- (NSString *)filePathOfFont:(NSFont *)font {
	CTFontDescriptorRef fontRef = CTFontDescriptorCreateWithNameAndSize ((CFStringRef)[font fontName], [font pointSize]);
	CFURLRef fontURL = (CFURLRef)CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute);
	NSString *fontPath = [NSString stringWithString:[(NSURL *)CFBridgingRelease(fontURL) path]];
	return fontPath;
}

//And I would call the function something like this
NSFont *font = [NSFont fontWithName:@"Menlo" size:12.0];
NSString *fontPath = [self filePathOfFont:font];
NSLog(@"%@", fontPath);

Please quote your operating system version for further help.

You do realise that this is an AppleScript forum, and not an Objective-C specific forum ?

Regards Mark