Skip to main content
Sally27994394stp5
Participant
July 6, 2023
Question

partial name text variable that updates when saved as

  • July 6, 2023
  • 1 reply
  • 160 views

I have been working on a script that will take the first 17 characters of the file name. the script works to populate the portion of the file name needed, but I want it to update as I "save as" and change the file name.  what i am trying to accomplish is using a document as a base template, where i will save out multiple file names and have this text correspond with the new file name.  my script is below.  any help would be greatly appreciated

 

// Get the active document
var activeDoc = app.activeDocument;

// Get the selected text frame
var selectedTextFrame = app.selection[0];

// Check if a text frame is selected
if (selectedTextFrame instanceof TextFrame) {
// Set the initial content of the text frame to the first 17 characters of the file name
selectedTextFrame.contents = getPartialFileName(activeDoc.name);

// Variables to store old and new file names
var oldFileName = activeDoc.fullName;
var newFileName = "";

// Add an event listener for the "beforeSave" event
app.eventListeners.add("beforeSave", function (event) {
oldFileName = activeDoc.fullName;
});

// Add an event listener for the "afterSave" event
app.eventListeners.add("afterSave", function (event) {
newFileName = activeDoc.fullName;

// Check if the file name has changed
if (oldFileName !== newFileName) {
selectedTextFrame.contents = getPartialFileName(newFileName);
}
});
} else {
alert("Please select a text frame to apply the script.");
}

// Function to get the partial file name
function getPartialFileName(fileName) {
return fileName.substr(0, 17);
}

This topic has been closed for replies.

1 reply

m1b
Community Expert
Community Expert
July 6, 2023

Hi @Sally27994394stp5, here's an example script that I got working the way you describe. However, it won't run as a normal script—you must put it in the "Startup Scripts" folder.

 

1. To create a "Startup Scripts" folder, go to the Scripts Panel, right-click on "User" and choose "Reveal in ... ", then in the revealed folder, if no "Startup Scripts" folder exists, make it.

2. Save the following script into it, as "Filename Updater.js" for example.

3. In your Indesign document, set the name of the text frame you wish to update to "UpdateMeWithFileName".

4. Restart Indesign so the startup script can run.

5. Open your document and Save As.

 

/**
 * Save script into the startup scripts folder.
 * Will instantiate a "afterSaveAs" event listener
 * that sets a text frame's contents to the file name.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/partial-name-text-variable-that-updates-when-saved-as/m-p/13918011
 */
//@targetengine "FileNameUpdater"

(function () {

    var listenerID = 'FileNameUpdater',
        textFrameName = 'UpdateMeWithFileName',
        listener = app.eventListeners.itemByName(listenerID);

    if (!listener.isValid) {
        // make the event listener
        listener = app.eventListeners.add("afterSaveAs", updateFileNameInDocument);
        listener.name = listenerID;
    }

    // the function that will be called by the event listener
    function updateFileNameInDocument(ev) {

        var doc = ev.target,
            textFrame = doc.textFrames.itemByName(textFrameName);

        if (textFrame.isValid)
            textFrame.contents = doc.name.slice(0, 17);

    };

})();

 

Let me know how it works for you.

- Mark