Skip to main content
Known Participant
August 19, 2009
Question

How can I get a font's filename from the font's name?

  • August 19, 2009
  • 1 reply
  • 15832 views

Hello all. I'm trying to copy a font from C:\WINDOWS\Fonts to another directory, but it's not always possible. To copy it I need the font's filename but, through Javascript, I can only get its name (not to confuse with the file name), family or PostScript name. For instance, "OCR A Extended"'s filename is "OCRAEXT.TTF", but I couldn't find a way to resolve the second name from the first (and tried a lot of things).

My first workaround has been opening a File.openDialog when the font's name and filename do not match, so the user goes to "WINDOWS\Fonts" and selects the file, but this directory is a bit tricky and will never accept the selection. However, if the font is any other directory that "Fonts", the file can be selected without problems.

I can figure some ways to solve this (for instance, asking the user to get the filename by himself and type it in a text box) but I'd rather ask here just in case there's an easy way I couldn't find out in the manual neither in the forums.

Many thanks in advance

This topic has been closed for replies.

1 reply

Paul Riggott
Inspiring
August 20, 2009

This is a bit crude but may help in getting a list of fonts along with thier filename, maybe you could expand it?

var myfolder= Folder("/c/windows/fonts");
var filelist=myfolder.getFiles ("*.otf");
var fontList=[];
for(var z in filelist){
filelist.open('r');
filelist.encoding= 'BINARY';
var temp=filelist.read();
filelist.close();
var tmp =temp.substr (temp.search(/All rights reserved/i)+20,60);
tmp=tmp.replace(/\d|\./g,'');
fontList.push([[tmp.substr (0,tmp.search(/;/))],[decodeURI(filelist)]]);
}
alert(fontList.length + " OTF Fonts: First font is: "+fontList[0][0]+ " Filename = " + fontList[0][1]);

Known Participant
August 20, 2009

Amazing, that worked great. Thanks a lot! Now I'll modify it a bit to improve the performance but this solved all my problems.

Again, thank you very very much

Known Participant
August 20, 2009

One last question: I've tried that script including ttf fonts in the fontlist, but the first member of the scructure (the font name) is always empty for ttf fonts.

I guess that's because the string to find is not "All rights reserved". Do you know what should I find?

Thanks a lot beforehand