Skip to main content
Known Participant
February 20, 2025
Answered

In Design script - file name that auto updates

  • February 20, 2025
  • 1 reply
  • 997 views

Hi All, 

 

I'm by no means a scripting genius, this has been entirely created using chat gpt. I know it is possible because I have had it in a workplace previously, but did not steal any intellectual property (i didnt create that one) but would love this to work for efficiencies in my workflow. 

 

Script is below. What I need it to do is 

 

1. Create a box, with 'paper' fill

2. insert text variable being file name (it needs to auto update if i change the file name then reopen) 

3. put the box 3mm above the top left corner of the page 

4. put the box on a separate, topmost layer called "FileName" 

 

I have had brilliant people help me before, so hoping to be equally lucky this time! Thank you in advance! 

 

#target indesign

// Ensure a document is open
if (app.documents.length > 0) {
var doc = app.activeDocument;

// Check if the "FileName" layer exists, or create it
var layerName = "FileName";
var newLayer;

// Try to find the existing layer
try {
newLayer = doc.layers.item(layerName);
if (!newLayer.isValid) {
throw "Layer not found"; // If the layer doesn't exist, create it
}
} catch (e) {
// Layer doesn't exist, so create it
newLayer = doc.layers.add({ name: layerName });
}

// Ensure the "FileName" layer is visible
newLayer.visible = true;

// Get the document's filename (without extension)
var fileName = doc.name.replace(/\.[^\.]+$/, ''); // Remove file extension

// Define text box size in millimeters (7mm height, 200mm width)
var boxHeightMM = 7; // Height in mm
var boxWidthMM = 200; // Width in mm (updated to 200mm)

// Positioning: -10mm from the top edge and aligned with the left edge of the document
var docTop = doc.pages[0].bounds[0]; // Top edge of the page
var docLeft = doc.pages[0].bounds[1]; // Left edge of the page

// Adjust the top position: set it to -10mm from the top edge
var topPosition = docTop - 10; // -10mm from the top edge
var leftPosition = docLeft; // Aligned with the LHS of the document

// Create a text frame and set its size and position in millimeters
var page = doc.pages[0]; // Place the text on the first page
var textFrame = page.textFrames.add({
geometricBounds: [topPosition, leftPosition, topPosition + boxHeightMM, leftPosition + boxWidthMM],
contents: fileName // Insert the document's filename as text
});

// Set the text frame's properties via the text range
var text = textFrame.texts[0]; // Get the text range from the text frame
text.appliedFont = "Arial"; // Set font to Arial
text.fillColor = doc.colors.itemByName("Black"); // Set text color to black

// Get the current font size and increase it by 2pt
var currentFontSize = text.pointSize; // Get the current point size
text.pointSize = currentFontSize + 2; // Increase the font size by 2pt

// Check if the "White Fill" swatch exists
var whiteColor;
try {
whiteColor = doc.swatches.itemByName("White Fill");
if (!whiteColor.isValid) {
throw "White Fill swatch not found"; // If the swatch is not valid, create it
}
} catch (e) {
// Create a CMYK color for fill (0/0/0/0, which is white)
whiteColor = doc.colors.add({
name: "White Fill",
model: ColorModel.PROCESS,
colorValue: [0, 0, 0, 0] // CMYK 0/0/0/0 = White
});
}

// Apply the "White Fill" color to the text frame's fill
textFrame.fillColor = whiteColor;

// Ensure the text frame is placed on the "FileName" layer
textFrame.itemLayer = newLayer; // Explicitly assign the layer

// Access the Text Frame Options
var tfOptions = textFrame.textFramePreferences;

// Set the inset spacing (2mm left & right)
tfOptions.insetSpacing = [2, 2, 0, 0]; // [top, left, bottom, right] in mm

// Set vertical justification to center
textFrame.textFramePreferences.verticalJustification = VerticalJustification.CENTER_ALIGN;

} else {
alert("No document open.");
}

Correct answer Robert at ID-Tasker

Thanks @Robert at ID-Tasker - the problem is, its Chat GPT. I have ZERO idea on all this (and really no business doing it, its just that I have created a couple that have worked for me, ableit with your assistance on one of the previous ones). I've had a look at the links you supplied, but again, its a foreign language to me. So i have no idea what it means. I have tried to prompt chat GPT with what I think i udnerstand, but its circular and producing no results. I'm thinking I will have to let it go for now. 

 

thanks for your help. I will let you know if i manage to figure out the script. 


Please check this link:

 

https://indisnip.wordpress.com/2010/09/02/quicktip-insert-text-variable-to-text-frame/

 

And, change this:

// Create a text frame and set its size and position in millimeters
var page = doc.pages[0]; // Place the text on the first page
var textFrame = page.textFrames.add({
geometricBounds: [topPosition, leftPosition, topPosition + boxHeightMM, leftPosition + boxWidthMM],
contents: fileName // Insert the document's filename as text
});

 

into this:

// Create a text frame and set its size and position in millimeters
var page = doc.pages[0]; // Place the text on the first page
var textFrame = page.textFrames.add({
geometricBounds: [topPosition, leftPosition, topPosition + boxHeightMM, leftPosition + boxWidthMM]
});
textFrame.parentStory.texts[0].textVariableInstances.add({associatedTextVariable:"File Name"}

 

Should work 😉

 

1 reply

Robert at ID-Tasker
Legend
February 20, 2025

And what's the problem? 

 

Ally C 25Author
Known Participant
February 20, 2025

oh jeez, that would help! Sorry! 

 

Issue is that its not creating a dynamic file name that changes/updates when i open the file. It creates it with static text, which is great, but sometimes i will do the file name first, then save the doc, and it just stays as "untitled.1" or i will change the file name after closing, and i need it to auto update when i re-open or run it through a batch process. 

Robert at ID-Tasker
Legend
February 20, 2025
quote

oh jeez, that would help! Sorry!  


By @Ally C 25

 

😉 

 

quote

Issue is that its not creating a dynamic file name that changes/updates when i open the file. It creates it with static text, which is great, but sometimes i will do the file name first, then save the doc, and it just stays as "untitled.1" or i will change the file name after closing, and i need it to auto update when i re-open or run it through a batch process. 

 

Do you run your script MANUALLY every time you open your document? 

 

If you want it to be run automatically - you would've to place your script in a "startup script" folder - more info how to do it here: 

 

https://community.adobe.com/t5/indesign-discussions/run-script-automatically-when-opening-a-specific-indesign-document/td-p/8963587