Having trouble changing fonts in JavaScript Photoshop using scripting on certain fonts

I have created a script for photoshop that is designed to change the font family to a different type. However, I am experiencing some inconsistencies in its performance.

Here is the section of the script responsible for altering 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;

While the script successfully changes fonts to Calibri and Verdana, it fails to do so for 'Arial' and 'Comic Sans MS', reverting back to the default font family which is Myriad pro.

Interestingly, setting the font family to 'Arial-BoldMT' works without any issues.

My objective is to make the script work with a barcode-like font, but even though the font is installed on my system, specifying its family name doesn't produce the desired result.

I am curious about the criteria based on which the script recognizes or fails to recognize certain fonts.

Answer №1

If you're looking for the right font, consider using ArialMT.

It's important to note the distinction between font names and postscript font names. In this case, the font name is ArialMT, while the postscript name is Arial.

https://i.stack.imgur.com/qdPvf.png

An option, albeit not the most elegant one, is to compare the postscript and font names to determine which is which. By running a script on a text layer that loops through your installed fonts and returns the names, you can identify the font accurately. However, please be aware that this method may not work well with non-text based layers.

var fontsInstalled = new Array();    
var psFontsInstalled = new Array();    

// Retrieve installed font names    
getInstalledFonts(fontsInstalled);    
    
// Fetch 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];    

  // Display 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++)    
  {    
    // Utilize 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];    
    }    
  }    
}

Answer №2

When working with Adobe Illustrator, you may encounter a common issue related to font names. The program uses specific 'internal' names for fonts, which can be different from their regular names. For instance, Arial is actually referred to as ArialMT, while Comic Sans MS is identified as ComicSansMS. This can make it challenging to determine the correct 'internal' name of a particular font. To help address this problem, I created a script that reveals the internal name of a selected text object:

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

If you have access to Illustrator, feel free to give it a try. From my observations, barcode fonts in Illustrator are typically named as Free3of9 and Free3of9Extended.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The data type 'unknown' cannot be assigned to the type 'any[]', 'Iterable<any>', or (Iterable<any> & any[])

I have been working on creating a custom search filter in my Angular project, and it was functioning properly. However, I encountered an error in my Visual Studio Code. In my previous project, everything was working fine until I updated my CLI, which resul ...

Adding JSON data to an array in Angular JS using the push method

I am encountering difficulties with adding data to an existing array. Currently, I have set up a table to display the data, but I want to also include the data in the table when a user enters an 8-digit barcode. Factory angular.module('app.pickU ...

Guide on altering the background color of a table row depending on the data in its cells with the help of AngularJS

I am looking to dynamically change the background color of a row based on specific cell data. If the first four characters in a table cell match a certain value, I want the entire row to change its color to red. Currently, my code changes the row color ba ...

Is there a way for me to update the placeholder text in my script from "CHANGE ME

Can anyone help me troubleshoot this code that's not working on my computer? I have a paragraph with the text CHANGE ME inside an element with the id "warning". I want to update this text when the login button is clicked, but it doesn't seem to ...

Interactive canvas artwork featuring particles

Just came across this interesting demo at . Any ideas on how to draw PNG images on a canvas along with other objects? I know it's not a pressing issue, but I'm curious to learn more. ...

Manipulate state in parent component from child component using onClick function with React hooks

Hello, I am facing a challenge trying to store data from a modal (child function) within the main (parent) function. Depending on which button is clicked, the modal loads specific HTML content (all buttons perform the same action). export default function ...

Error: The object being referenced (scope.awesomeThings) is undefined and unable to be evaluated

Each time I run the grunt test command, I encounter this error. I set up a project using yo angular and attempted to execute the example code provided in Yeoman's scaffold. Something seems to have gone awry here - below is the code snippet that I trie ...

Clicking on an item will now automatically remove it from the selection, rather than requiring you to click on a specific

Is there a way to customize the design of select2 items in a multi-select box so that users can easily remove selected items by clicking anywhere on the button, rather than just the small "cross" icon on the left-hand side? Refer to the image; instead of n ...

Expanding Module Scope to Just the Request (employing Require, Node.js)

Original Query The project I am currently working on is using Express to manage HTTP requests. The structure of the project is quite intricate, with multiple embedded require statements. Our main challenge lies in maintaining access to the request and re ...

tips for optimizing javascript file caching

https://i.stack.imgur.com/UhWD1.pngMy web application was created using "pug" technology about 9-8 years ago, and more recently, pages have been added in an innovative framework (vue.js). However, whenever there is a transition between an old pug page and ...

What is the process for extracting the period value from SMA technical indicators within highcharts?

Can someone assist me in retrieving the period value from SMA indicator series by clicking on the series? series : [{ name: 'AAPL Stock Price', type : 'line', id: 'primary', ...

What is the best way to incorporate a transition on transform using Styled-components?

I attempted to add a transition on the transform property, but unfortunately, it did not produce any visible changes. I tested other properties such as: background-color, color... and they worked perfectly fine. live code: source code: // styled-compo ...

What is the best way to update the value of a specific key in discord.js?

As I struggle to explain properly due to my limited English proficiency, I am reiterating my question. In my config.json file, there is a key named "status" with a corresponding value of "online". I am attempting to change this value but haven't been ...

The Javascript countdown feature may experience issues on Safari and IE browsers

Why does this function work in Chrome, but not on IE or Safari? function countdown(){ var dDay = new Date().getUTCDate() + 1; var dMonth = new Date().getUTCMonth() + 1; var dYear = new Date().getUTCFullYear(); var BigDay = new Date(dYear+ ...

Implementing Angular CDK for a dynamic drag-and-drop list featuring both parent and child elements

I'm currently working on setting up a drag and drop list within Angular using the Angular CDK library. My goal is to create a list that includes both parent and child elements, with the ability to drag and drop both parent items as well as their respe ...

Refreshing ng-repeat dynamically with $http without reloading the page

Just a quick heads-up, I'm new to AngularJS In my AngularJS application, I am using the $http service to fetch data. Each row has an update button that needs to trigger a server-side method (the API is already returning data) to sync with a third-par ...

Tips for safely storing JWT in the browser

I am currently working with Angular 10 on the front-end and obtaining a JWT from backend services. I am seeking the best method to securely store my Okta JWT in the browser while also mitigating the risk of XSS and XSRF attacks. I have looked into storing ...

Learn the best way to retrieve the highest number from a Array<String> in TypeScript or JavaScript

Can someone help me create a function in JS or TS that meets the following requirements? I am looking for a functional programming approach. ・Input type: Array(String) ・Output type: string or undefined Examples Input Result ["" ...

Struggling to understand how to utilize Firebase for logging in with Github

On my homepage, there is a link that directs to the following: <a href="/login">Login with Github</a> Within my app.js file, I have the following code snippet: app.get('/login', function(req, res) { var ref = new Firebase(&ap ...

"Using Angular and TypeScript to dynamically show or hide tabs based on the selected language on a website

When switching the language on the website, I want to display or hide a specific tab. If the language is set to German, then show the tab; if any other language is selected, hide it. Here's my code: ngOnInit(): void { this.translate.onLangChange.s ...