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

What is the best way to have the sidebar of a webpage slide out over the main body content that is being displayed?

Issue Summary I am experiencing an issue with the positioning of my sidebar, which is initially located 66% offscreen using -translate-x-2/3. The sidebar is meant to be pulled into view onmouseover, however, the main body content remains stuck in place. I ...

Ways to display tinyMCE content in a unique manner

I've been diving into node.js/express/mongoDB and decided to create a blog. I encountered an issue with rendering the content inputted through tinyMCE as HTML - instead, it's displaying the tags around my content. How can I properly display it as ...

`the issue of $scope object not being passed correctly to ng-if and ng-class ternary conditions

**app.js:** $scope.servers = [ {name:'SQL Server', status:"up"}, {name:'Web Server', status:"down"}, {name:'Index Server', status:"down"} ]; **index.html:** <table> ...

How to Eliminate Image Flickering While Loading in a React Component

Currently, I am developing a React component that takes an imageUrl and displays it on a canvas with a deliberate 2-second delay to mimic loading time for larger images. However, I have encountered a problem: when the imageUrl changes in the parent compone ...

Leveraging this within the realm of promises utilizing babel

While utilizing Babel, I encountered a problem that I have not yet been able to solve. The issue arises when I use promises within a class method and I require access to the this object of the class inside the promise in order to call class functions. He ...

Using JQuery to reverse the action of hiding an image

Having some trouble with my Javascript and JQuery skills, so bear with me. I've set up a gallery of images where users can drag and drop them into a designated drop zone. Everything works fine when dragging an image from the gallery to the drop zone a ...

If the next element in the sequence happens to be the final element, then conceal a separate

Continue pressing the downward button consistently on until you reach the bottom. The down arrow should disappear slightly before reaching the end. Is there a way to achieve this using the code provided below? I'm new at this, but I believe I need t ...

Why isn't Meteor.call functioning in the stub?

As I delve into the realm of async JavaScript coding, I have encountered a gist that has left me puzzled: https://gist.github.com/dariocravero/3922137 Specifically within client_save.file.js - there are parts of this code snippet that baffle me: fileRead ...

Encountering Error 500 with Jquery Min.Map File

ERROR: GET request to http://domain.com/assets/js/jquery-1.10.2.min.map returned a 500 Internal Server Error Can anyone help me figure out what's causing this error? I checked the log files in /var/log/error but couldn't find any information. T ...

What is the proper way to implement v-model for a custom component within the Vue render function?

Below is the code that I am working with: ... var label_key_map = { a: "1", b: "2", c: "3", d: "4" } render: (h) => { var form_data = {} for (let key in label_key_map) { var form_item = h( 'FormItem', {props: {prop: key}}, ...

Attempting to store a specific h1 text.event as a variable, allowing it to be saved upon input of the initial letter

After typing the second key, you can continue to input more characters as desired. It is possible to customize the text displayed in the h1 using your own name or any other text of your preference without needing a textbox. $(document).keypress(functio ...

Is there a way to customize the appearance of an unordered list by setting it to display as an image instead of default bullets? I want to

I have been attempting to achieve this desired outcome. However, my efforts to reproduce it resulted in the check marks being rendered at a smaller size than intended due to using an SVG file. An example of this issue can be seen in the following image: I ...

What is the best way to initiate an onload event from a script embedded within a jquery plugin?

Currently, I am in the process of developing a jQuery plugin. One issue that I have encountered involves a script tag being dynamically added for LivePerson.com. The script is supposed to trigger an onLoad function specified in the configuration. However, ...

Displaying a dropdown selection that showcases two object properties lined up side by side

I need the select option dropdown values to display employee names along with their titles in a lined up format. For instance, if my values are: Bob Smith Director Mike Kawazki HR Jane Doe Manager I want them to be shown as: Bob Smith Director Mi ...

Mozilla browser experiencing issues with mouse move event functionality

Here, I have a background image on the body and with the following script, when the user moves the mouse, the image in the background also moves. body { background-image: url('../images/1.png'); background-size: 98%; background-posi ...

Tips for executing a jQuery nested template in a recursive manner

Imagine a world where JSON objects and jQuery templating collide in a thought-provoking inception-like scenario. How can we dive deeper into this rabbit hole? The catch is, I'm a bit lazy and hoping the code will do most of the heavy lifting... Q:> ...

"Animating a card to slide in from the left side upon clicking a button in a React app

How can we create a feature where, upon clicking "Apply Coupon" in Image 1, a window slides in from the left just above the webpage (as shown in Image 2)? Additionally, in Image 2, there is a blue transparent color on the webpage adjacent to this sliding w ...

NodeJS guide: Enabling cross-domain access for web services

Currently, I am developing a nodejs server and facing the challenge of needing to access additional services through ajax from a different domain. Can anyone provide guidance on how to bypass the cross-domain restriction within nodejs code? Please note th ...

How come the express.static() function is failing to load my .js and .css files from the intended path?

const express = require('express'); const app = express(); const server = require('http').Server(app); const io = require('socket.io').listen(server); const path = require('path'); let lobbies = new Array(); app.us ...

Having trouble with my Express.js logout route not redirecting, how can I troubleshoot and resolve it?

The issue with the logout route not working persists even when attempting to use another route, as it fails to render or redirect to that specific route. However, the console.log("am clicked"); function works perfectly fine. const express = require('e ...