Skip to main content
AidanCable
Participant
January 30, 2025
Answered

Creating Text Frames Script Help

  • January 30, 2025
  • 2 replies
  • 443 views

Hi all, im trying to get a text frame placed through a script inDesign (multiple text frames)
However when it places, it cant find the font family, and it wont change pt size, and is always overset... I have used chatgpt to help me but im hitting a wall now and pulling my hair out...

Can anyone tell me what I am doing wrong? (apart from using chatgpt...)
I have run a script to identify the font, but no matter what combination of words, tabs, spaces it just cant locate it.

Thanks in advanced!

 

// Extract parts of the document name
var docName = app.activeDocument.name.replace(/\.[^\.]+$/, ""); // Remove file extension
var parts = docName.split("_"); 

// Ensure we have enough parts
var firstPart = parts[0] || "";
var secondPart = parts[1] || "";

// Function to add text frames in InDesign
function addTextFrame(contents, xPos, yPos) {
    var page = app.activeDocument.pages[0]; // Use the first page
    var textFrame = page.textFrames.add();
    textFrame.contents = contents;
    
    // Set font properties
    var textRange = textFrame.texts[0];
    textRange.appliedFont = app.fonts.item("Roboto Light"); // Use 'Roboto Light' font
    textRange.pointSize = 8; // Font size 8pt
    
    // Position the text frame
    var frameWidth = 50; // Adjust width as needed
    var frameHeight = 10; // Adjust height as needed
    textFrame.geometricBounds = [yPos + "mm", xPos + "mm", (yPos + 5) + "mm", (xPos + frameWidth) + "mm"];
    
    return textFrame;
}

// Function to get today's date in MM/DD/YYYY format
function getFormattedDate() {
    var today = new Date();
    var day = today.getDate();
    var month = today.getMonth() + 1;
    var year = today.getFullYear();
    return (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day + '/' + year;
}

// Define text positions (all in millimeters)
var fileNameX = 86;  
var fileNameY = -1.9;

var scodeNumberX = 92.5; 
var codeNumberY = -5.3;

var secondPartX = 135;  
var secondPartY = -5.33;

var todayDateX = 171;  
var todayDateY = -5.33;

// Create the text frames with specified positioning
addTextFrame(docName, fileNameX, fileNameY);
addTextFrame(firstPart, codeNumberX, codeNumberY);
addTextFrame(secondPart, secondPartX, secondPartY);

 

Correct answer Robert at ID-Tasker

I think this: 

textRange.appliedFont = app.fonts.item("Roboto Light"); // Use 'Roboto Light' font

should be split into appliedFont and FontStyle.

 

2 replies

Robert at ID-Tasker
Legend
January 30, 2025

And what are the DEFAULT - currently active / selected - ParaStyle and CharStyle - on the corresponding Pallets? 

 

Because, when you create TextFrame - those are used / applied by default. 

 

And what are the properties of the default TextFrame Object Style? 

 

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
January 30, 2025

I think this: 

textRange.appliedFont = app.fonts.item("Roboto Light"); // Use 'Roboto Light' font

should be split into appliedFont and FontStyle.

 

AidanCable
Participant
February 4, 2025

It had to be split to work you are right! It doesnt seem to matter about default paragraph style 🙂

text.appliedFont = "Roboto";
text.fontStyle = "Light";

Thanks for your help!
Robert at ID-Tasker
Legend
February 4, 2025

You're welcome. 

 

But what do you mean by default ParaStyle doesn't matter?