Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

In Design script - file name that auto updates

Community Beginner ,
Feb 19, 2025 Feb 19, 2025

Copy link to clipboard

Copied

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.");
}

TOPICS
Scripting

Views

330
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 04, 2025 Mar 04, 2025

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 fram
...

Votes

Translate
Community Expert ,
Feb 19, 2025 Feb 19, 2025

Copy link to clipboard

Copied

And what's the problem? 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 19, 2025 Feb 19, 2025

Copy link to clipboard

Copied

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. 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2025 Feb 20, 2025

Copy link to clipboard

Copied

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... 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 20, 2025 Feb 20, 2025

Copy link to clipboard

Copied

@Robert at ID-Tasker Thanks so much for your continued support! I do recognise you helped with my last script issue!

 

Typically I would run it once and yes it would be manually by double clicking in script panel when setting up the file and I am happy with that control. But between the setup and actually exporting it for print, the file name may change, and if i change that in explorer, i would like the name to update when I re-open. 

 

In my previous job, the file name remained as a text variable, and I couldn't edit in indesign, it was like a block of text, and when update the file name in file explorer (PC) and then i open the document again in indesign and it would update automatically. The script i supplied is inserting it if i click the script, which is perfect, but then it becomes text like normal text and doesn't update if i change the name in explorer. 

 

Apologies again, I am really not one for coding. But i know these functionalities are possible, so trying to streamline my workflow. 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2025 Feb 20, 2025

Copy link to clipboard

Copied

You could also use built-in variables: 

 

https://community.adobe.com/t5/indesign-discussions/insert-variable-file-name-without-extension/td-p...

 

Always happy to help. 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 20, 2025 Feb 20, 2025

Copy link to clipboard

Copied

Thanks, I've had a look around those links, I'm still not sure how to code it in correctly. It is adding the file name, exactly how I want it, the only problem is its not staying dynamic, its converting to plain text. 

I will keep digging and let you know if i find a resolution.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2025 Feb 21, 2025

Copy link to clipboard

Copied

Your solution places FileName as a static text - there is nothing dynamic. 

 

If you define and place a variable - as contents of your TextFrame - it will auto-update. 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 03, 2025 Mar 03, 2025

Copy link to clipboard

Copied

I'm still not getting this to work. I know i want it to go Type>Text Variables>Insert Variable>File Name into the text box it is creating, but its just not working. The text becomes static and does not auto update. 

 

i've tried event handlers, but with no success, they end up throwing an error that the object no longer exists, and then i have to disable the handler/clear the message 7 times before i can close the document. 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

@Ally C 25

 

Can you post latest version of your code? 

 

And please post as "code" - not a contents of your reply: 

 

1000030033.jpgexpand image

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

@Robert at ID-Tasker Thanks, see below. 

 

#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.");
}

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

@Ally C 25

 

But you're still not using Text Variables - you're "placing" File Name as a "plain text".

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

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. 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

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 😉

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

@Robert at ID-Tasker  honestly, I cannot thank you enough. you have no idea how much you have just streamlined my workflow. This now works PERFECTLY! Thank you thank you thank you thank you! 

 

Script below should anybody need it. 

#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;

    // 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]
    });

    // Add the "File Name" text variable to the text frame
    textFrame.parentStory.texts[0].textVariableInstances.add({
        associatedTextVariable: "File Name"
    });

    // 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.");
}

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

I can imagine 😉 

 

And you're welcome 🙂 

 

Are you Mac or Windows user? 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

Windows user (not by choice. But I am coming around to it.) 

 

And wish i could get the whole coding thing. but it just doesnt work in my head. I cant make sense of it no matter how much i try. I envy your skillset. 

 

Thank you again! 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

Maybe you would like to try my ID-Tasker tool? 

 

Free version will let you browse complete structure of the Document(s) - texts, objects, all styles, interactive elements, etc. 

 

Paid version will let you automate a lot of things like that - and you don't have to be a coder.

 

I can give you access to the full version for free for a few days if you're interested. 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

That sounds great, i wont lie I am under the pump at the moment so wouldn't have time right now. But I will be in touch! 

Can I get the link to the free version please?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

LATEST

Please check envelope in the top right corner. 

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines