Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Aug 19, 2009 Aug 19, 2009

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

TOPICS
Actions and scripting
15.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Aug 20, 2009 Aug 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]);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 20, 2009 Aug 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 20, 2009 Aug 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 20, 2009 Aug 20, 2009

I haven't found a way with ttf files yet, but will keep looking.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 21, 2009 Aug 21, 2009

Thanks Paul. I've found some info, but still have to find out how to use it properly: http://scripts.sil.org/cms/SCRIPTs/page.php?site_id=nrsi&item_id=IWS-Chapter08#3054f18b

I'll keep investigating.

EDIT: more info -> http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6.html#Overview

Also I've found that in most ttf fonts, the name is next to "All rights reserved" too, but the script doesn't get them. Curious...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 21, 2009 Aug 21, 2009

It really does get confusing! I haven't been able to make sense of the format at all. I have a file open in the hex editor but the offsets just don't add up at all. I think we need Mike or X to show us how its done?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Aug 21, 2009 Aug 21, 2009

After a quick look at several ttf files I don't think this can be done with a string search. And unless you need to do this a lot, I don't think it would be worth the effort to write a ttf file parser. The files I looked at didn't follow the table in the first link posted.

But I have an answer that might be helpful. The windows font folder is special type of folder. You can't click on a font to add it to the dialog like normal folders. You have to type the first few letters of the font you want and choose it from the dropdown. If the user does that the file is returned by File.openDialog().

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 21, 2009 Aug 21, 2009

Thanks for the hint and for your time, Michael Finally I've decided that, in case the font filename can't be found, the user will have to find it and copy it to a "prompt" dialog. Then the script checks if it exists in "WINDOWS/Fonts" and if so, continues processing. If not, a default font is set to the layer.

It's really annoying that a standard like ttf files are impossible to parse...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 21, 2009 Aug 21, 2009

I have been looking at the registry, if you run regedit and look here:-

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Fonts

you will see a complete list of fonts and thier file names.

Maybe you know someone that knows vbs so that you could extract a compete list?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 22, 2009 Aug 22, 2009

I have managed to get a file with a complete list of fonts with thier filenames using vbs

It creates Fontlist.txt on the desktop, you could even call the script from the jsx and read the values in once the file has been created.

Set wshShell = WScript.CreateObject("WScript.Shell")
Set wshSysEnv = wshShell.Environment("PROCESS")
sMyFile = "c:" & wshSysEnv("HOMEPATH") & "\Desktop\Fontlist.txt"
Dim objFileSystem, objOutputFile
Dim strOutputFile
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(sMyFile, TRUE)

Dim str
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Fonts"
objReg.EnumValues HKEY_LOCAL_MACHINE, _
strKeyPath,arrEntryNames,arrEntryZZZ

For Each entry in arrEntryNames
str = wshshell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\Fonts\" & entry)

       objOutputFile.WriteLine(entry & "," & str)
Next
objOutputFile.Close

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 03, 2009 Sep 03, 2009

Hello! Sorry for the delay, I was on holydays. I'll try what you said in your last post as soon as possible. Thanks you a huge lot, Paul

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 07, 2017 Mar 07, 2017
LATEST

Here is a scenario, in which I get a list of system font names (not file names) on Windows.

First of all it's needed to execute JS or VBS (in my case) code, which can retrieve a list of fonts names.

We can send the code to system.callSystem() as parameters, or we can run VBS (JS) file:

54. system.callSystem("wscript.exe " + data_folder_fs_name + "\\" + script_name);

During execution, the code saves the list of fonts as txt file. Font list is done.

var fonts_arr = get_font_list (false).replace(/& /g, "\n").split("\n");

var w = new Window("palette {preferredSize: [300, 200]}", "", undefined, {resizeable:true});

w.add("dropdownlist", undefined, fonts_arr);

w.show();

function get_font_list (replace)

{

          var data_folder_name = "u_data";

          var fonts_file_name = "font_list.txt";

          var script_name = "get_fonts.vbs";

          if ($.os.indexOf("Windows") != -1) // If OS is Windows

          {

                 // Set the user folder. All data can be saved here: VBS (JS) file and txt-file

                 var data_folder = new Folder("~" + "\\" + data_folder_name);

                 if (!data_folder.exists) data_folder.create();

                

                 var data_folder_fs_name = data_folder.fsName.replace(/\\/g, "\\\\");

                

                 // Get system Font-folder name

                 var system_fonts_folder = Folder(Folder.system.parent.fsName + "\\Fonts");

                 var font_list_file = new File(data_folder_fs_name + "\\" + fonts_file_name);

                 // Check if the date when the folder was modified is less than the date of font list file. Otherwise we need to update font-list file

                 if (font_list_file.exists && (font_list_file.modified.getTime()  > system_fonts_folder.modified.getTime()))

                 {

                        return read_txt (font_list_file);

                 }

                

                 // If VBS file doesn't exist, then save it

                 var script_fs_name = data_folder_fs_name + "\\" + script_name;

                                                     

                 var script_file = new File(script_fs_name);

                 if (!script_file.exists)

                 {

                        // VBS code gets font names from registry, sort them and save to disk

                        var vbs_code = 'GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default:StdRegProv")._\

EnumValues &H80000002, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", arrNames\

n = UBound(arrNames)\

Do\

    nn = -1\

    For j = LBound(arrNames) to n - 1\

        If arrNames(j) > arrNames(j + 1) Then : TempValue = arrNames(j + 1) : arrNames(j + 1) = arrNames(j) : arrNames(j) = TempValue : nn = j : End If\

    Next\

    n = nn\

Loop Until nn = -1\

For Each Name In arrNames : strValue = strValue & vbCrLf & Name : Next\

CreateObject("Scripting.FileSystemObject").CreateTextFile("' + data_folder_fs_name + '\\\\' + fonts_file_name + '",1).Write Mid(strValue, 2)';

                        script_file.open("w");

                        script_file.write(vbs_code);

                        script_file.close();

                 }

                 // Save font list to disk

                 system.callSystem("wscript.exe " + data_folder_fs_name + "\\" + script_name);

                 return read_txt (font_list_file);

          }

          else

          {

                  // system.callSystem(); // There is nothing here yet. I'm not sure whether this is possible in Mac OS

           }

}

function read_txt (txt_file)

{

    txt_file.open("r");

    var content = txt_file.read();

    txt_file.close();

    return content;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines