Skip to main content
Participant
Aug 23, 2022 3:49 AM UTC
Answered

Partial File Name Text Variable that updates when you rename the file

Hi all,

I've seen a couple of old posts related to this topic, but I'm new to scripting and I've not been able to solve my specific issue myself so far. 

 

I'd like to insert a text variable into an indd document that takes the first 7 characters of the file name and inserts it as a variable. All documents are named as J00XXXX_filename.indd, where J00XXXX is a unique job number. I'm creating a template where we would open it, save it with the new job number (always following the J00XXXX naming convention) and I'd like the text variable to change when you rename the file.

 

Any help would be much appreciated! Thank you!

This topic is closed to new replies. Start a new post to keep the conversation going.
Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
Aug 23, 2022 7:32 AM UTC

Here is a quick & dirty start-up script:

#targetengine "kasyan"

var eventListener = app.addEventListener("afterOpen", updateTextVar, false);

function updateTextVar(event) {
	try {
		var doc = app.documents[0];
		var textVarName = "My Partial File Name";
		var textVar = doc.textVariables.item(textVarName);
		var m = doc.name.match(/^J00.{4}/);

		if (m != null) {
			var custName = m[0];
			
			if (!textVar.isValid) {
				textVar = doc.textVariables.add({variableType: VariableTypes.CUSTOM_TEXT_TYPE, name: textVarName});
				
			}

			if (textVar.variableOptions.contents != custName) {
				textVar.variableOptions.contents = custName;
			}
		}
	}
	catch(err) {}
} 

If the "My Partial File Name" text variable doesn't exist, it will be created; if exists, it will be updated.

For example, we have a file at the start: J00abcdefghijklm.indd

Then we rename the file -- say to J00bcdefghijklm.indd -- and after opening it, the text variable is updated automatically.

 

 

6 replies

Community Expert
Aug 23, 2022 8:23 AM UTC

Hi @Emma238317970zju ,

we already had a discussion about this about one year ago:

 

10 first letters/digits Partial filename
charlieg19576563, Jun 29, 2021
https://community.adobe.com/t5/indesign-discussions/10-first-letters-digits-partial-filename/td-p/12145405

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Translate
Participant
Aug 24, 2022 5:50 AM UTC

Thank you Uwe, I spent a lot of time going through similar older questions and was struggling to get the solution to work for me being a novice, but appreciate the time to show me some new learnings!

Translate
Participant
Feb 7, 2023 2:02 PM UTC

Hi! I am trying to implement this script as well but having issues. Can you send me a link to the script please?

thank you so much for your help!

 

Translate
Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
Aug 23, 2022 7:32 AM UTC

Here is a quick & dirty start-up script:

#targetengine "kasyan"

var eventListener = app.addEventListener("afterOpen", updateTextVar, false);

function updateTextVar(event) {
	try {
		var doc = app.documents[0];
		var textVarName = "My Partial File Name";
		var textVar = doc.textVariables.item(textVarName);
		var m = doc.name.match(/^J00.{4}/);

		if (m != null) {
			var custName = m[0];
			
			if (!textVar.isValid) {
				textVar = doc.textVariables.add({variableType: VariableTypes.CUSTOM_TEXT_TYPE, name: textVarName});
				
			}

			if (textVar.variableOptions.contents != custName) {
				textVar.variableOptions.contents = custName;
			}
		}
	}
	catch(err) {}
} 

If the "My Partial File Name" text variable doesn't exist, it will be created; if exists, it will be updated.

For example, we have a file at the start: J00abcdefghijklm.indd

Then we rename the file -- say to J00bcdefghijklm.indd -- and after opening it, the text variable is updated automatically.

 

 

Translate
Participant
Aug 24, 2022 5:49 AM UTC

this worked perfectly for me, thank you for taking the time to explain it so clearly for me, it's very much appreciated!!

Translate