Skip to main content
StefanStehlik
Inspiring
February 19, 2023
Answered

Composition naming

  • February 19, 2023
  • 1 reply
  • 1204 views

I would like to check all the compositions in the project panel for a specific name. For example, if I would like to copy a composition with the name "Comp 1". I would like to check if there is the composition with the name "Comp 1" and added suffixes "dup_1", "dup_2". "dup_3". If there is one my goal is to increment the last number. So the second time I copy the composition it would end with "dup_2". "dup_3" "dup_20" etc. Could you help with with these code?

This topic has been closed for replies.
Correct answer Dan Ebberts

Play around with this:

function getUniqueCompName(theName,theSuffix){
	var n;
	var maxN = 0;
	var splitText;
	var curComp;
	var n;
	for (var i =1; i <= app.project.numItems; i++){
		if ((app.project.item(i) instanceof CompItem)  && (app.project.item(i).name.indexOf(theName) == 0)){
			curComp = app.project.item(i);
			idx = curComp.name.indexOf(theSuffix);
			if (idx > -1){
				n = parseInt(curComp.name.substr(idx + theSuffix.length),10);
			}
			if (! isNaN(n)) maxN = Math.max(maxN,n);
		}
	}
	return (theName + theSuffix + (maxN + 1));
}

getUniqueCompName("Comp 1", "dup_");

1 reply

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
February 19, 2023

Play around with this:

function getUniqueCompName(theName,theSuffix){
	var n;
	var maxN = 0;
	var splitText;
	var curComp;
	var n;
	for (var i =1; i <= app.project.numItems; i++){
		if ((app.project.item(i) instanceof CompItem)  && (app.project.item(i).name.indexOf(theName) == 0)){
			curComp = app.project.item(i);
			idx = curComp.name.indexOf(theSuffix);
			if (idx > -1){
				n = parseInt(curComp.name.substr(idx + theSuffix.length),10);
			}
			if (! isNaN(n)) maxN = Math.max(maxN,n);
		}
	}
	return (theName + theSuffix + (maxN + 1));
}

getUniqueCompName("Comp 1", "dup_");
StefanStehlik
Inspiring
February 19, 2023

Thank you Dan, this is great. However, I have one problem, If the comp name ends with _dup. The script is actually adding only one _dup _dup_1, every time it's run, so it won't iterate through numbers. The name of the comp always stays the same and ends with _dup _dup_1. Can you help me with this?

Dan Ebberts
Community Expert
Community Expert
February 19, 2023

You lost me. Can you give an example of what comp names you have and what name you expect when you run the script?