Actually you need to pass â~/Library/Fonts/â to the nsopenpanel for it to open in the userâs font folder. The tilde means the current logged in userâs home folder path on every machines, no distinction whatsoever. The version you posted was the path to the local font folder on the sartup disk.
Itâs strange that we canât get access to the fontâs path. Iâll check this when time permits and post back any result.
Good luck with your project!
Browser: Safari 6533.18.5
Operating System: Mac OS X (10.6)
This is why I asked. On my system, the font folder in my home folder path is empty. All my fonts are in the Fonts folder in the Library folder of the start up disk. Like I said, I donât know anything about font management. They just magically show up when I want to change them :). But it seems that the location can vary, perhaps depending on how they were installed? Regardless, it seems like it would be useful to have a way of finding the location of the Font folder. Interestingly, if I use âfonts folderâ in applescript, it points to Fonts folder in Library in the system folder.
Yeah, well there is so many apps and tools that can play in the fonts folders that it is hard to make sense of it.
Yes in AS you can use fonts folder but if you use either âof system domainâ or âof locale domainâ or âof user domainâ (I could be off on the exact spelling, memoryâŚ) then you can specify which one you want. Check in the AppleScript editor, within the library window, the standard additions I think, it will have more details.
Browser: Safari 6533.18.5
Operating System: Mac OS X (10.6)
FWIW, I think stuff like this is a good reason to consider having a base Objective-C class in your projects that your app delegate inherits from. You can then quickly add methods like this and call them directly, without having to add/instantiate/add property.
Stefan: Is there some special reason you put âNSString stringWithString:â in there?
Yeah, I agree â I sometimes forget about that option. Another fix that makes the method simpler, is to pass the font name and size instead of the font object â since you have to call current applicationâs NSFontâs fontWithName_size_ and pass it that information anyway and then the objective-C method just gets those values back from the font object (of course, if you already have the font object, then Stefanâs way would be easier). So, this is the code I have now:
script FontPathAppDelegate
property parent : class "GetPath"
on applicationWillFinishLaunching_(aNotification)
set myPath to pathToFontNamed_size_("Times-Roman", 12)
log myPath
end applicationWillFinishLaunching_
end script
Probably itâs overcautious and not needed,
but my intention is, if the derived path depends on the url object,
itâs safer to create a new string object before releasing the url object.
Uhh ⌠you guys lost me about 10 posts back when you started talking about custom classes :). Would the âAdding Objective-C Codeâ section in Shaneâs book be a good place to start the deciphering process? Iâve glanced at it but I havenât really gone over it very thoroughly since for some reason when I see the â.hâsâ and â.mâsâ, my brain seems to go into a shut down mode :).
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:
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.
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]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]
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 ?