LoveDroid
LoveDroid

Reputation: 85

JavaScript Photoshop: Change font through scripting not working for all fonts

I am writing a script for photoshop to change font family to different font but the script works with some fonts and some not.

Here is the part of the script that is responsible for changing the font family:

var origDoc = activeDocument;
var fullName = origDoc.name; 

var myLayerRef = origDoc.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = fullName ;
var myTextRef = myLayerRef.textItem;
myTextRef.position = new Array( 100, 100);
myTextRef.size = 35;
myTextRef.font = 'Calibri';  //Font family name
myTextRef.contents = myLayerRef.name;

As you can see that the font family set for now is Calibri and when I run the script, the font changes to Calibri family, same for Verdana and others. BUT when I choose for example 'Arial', 'Comic Sans MS' the font keeps the default font family which is Myriad pro.

Also, when I set the font family to 'Arial-BoldMT' it works fine.

My goal is to have this font works which is like barcode font, but when I set its family name it doesn't work either. (The font is installed on my computer). https://www.barcodesinc.com/free-barcode-font/

I want to know based on what the font gets recognized or not.

Upvotes: 1

Views: 1127

Answers (2)

Ghoul Fool
Ghoul Fool

Reputation: 6947

You probably want ArialMT.

There a difference between the font names and the postscript font names. Font name in the case is ArialMT, postscript name Arial.

font info

One rather (inelegant) solution is to compare the postscript and font names to find out which is which. This script will work on a text layer and loop through your installed fonts and return the names. This just basic and fill fail on a non-text based layer.

var fontsInstalled = new Array();    
var psFontsInstalled = new Array();    
    
// get installed font names    
getInstalledFonts(fontsInstalled);    
    
// get installed postscript font names    
getInstalledFonts(psFontsInstalled, true);    
    
var srcDoc = app.activeDocument;    
var currentFontLayer = srcDoc.activeLayer;    
getFontContents(currentFontLayer)    
    
function getFontContents(alayer)    
{    
  var info = new Array;    
    
  var textContents = alayer.textItem.contents;    
  // var fontSize = alayer.textItem.size;    
  var fontFace = alayer.textItem.font;    
    
  var postScriptFontFace = gimmePostScriptFontName(fontFace, fontsInstalled, psFontsInstalled)    
    
  info.push([fontFace, textContents, postScriptFontFace]); // pushing items onto an object    
    
  // get the font contents    
  var tempFontFace     = info[0][0];    
  var tempFontContents = info[0][1];    
  var tempPSFontFace   = info[0][2];    
      
  // postscript font name only    
  var str = "Font: " + tempFontFace + "\n" + "PostScript: " + tempPSFontFace;    
    
  alert("Font info:\n" + str);
}    
    

function getInstalledFonts(arr, bool)    
{    
  if (bool == undefined) bool == false;    
    
  numOfFonts = app.fonts.length;    
  for (var i=0, numOfFonts; i < numOfFonts; i++)    
  {    
    // use app.fonts[i].postScriptName for postscript names    
    // use app.fonts[i].name for font names    
    
    // for postscritp names    
    if (bool)    
    {    
      arr.push(app.fonts[i].postScriptName);    
    }    
    else arr.push(app.fonts[i].name);    
  }    
  return arr    
}    
    
        
function gimmePostScriptFontName(str, arr1, arr2)    
{    
  for (var i = 0;  i < arr1.length; i++)    
  {    
    if (arr2[i] == str)    
    {    
      return arr1[i];    
    }    
  }    
}
 

Upvotes: 2

Yuri Khristich
Yuri Khristich

Reputation: 14537

Adobe Illustrator has the same trouble. It uses some 'internal' names for fonts. For example Arial is ArialMT, Comic Sans MS is ComicSansMS etc. Sometimes it's almost impossible to guess what is the 'internal' name of a given font. I wrote the little script that shows an 'internal' name of a selected text object:

 alert(String(app.activeDocument.selection[0].textRange.characterAttributes.textFont).slice(10,-1));

If you have Illustrator you can try it. As far as I can tell, your barcode fonts have the names: Free3of9 and Free3of9Extended.

Upvotes: 1

Related Questions