partial name text variable that updates when saved as
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);
}
