In Design script - file name that auto updates
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.");
}
