Last active
November 20, 2017 17:03
-
-
Save Nico-Duduf/0db13217a05f909ff2826ed67ba6bfb1 to your computer and use it in GitHub Desktop.
Extendscript Generating unique name with increment (for AE)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function newUniqueName( newName, stringList ){ | |
//detect digits | |
var reg = "( *)(\\d+)([.,]?\\d*)$"; | |
//clean input | |
var regexClean = new RegExp(reg); | |
newName= newName.replace(regexClean,""); | |
//go! | |
var regex = new RegExp( newName + reg); | |
//The greatest number found | |
var greatestNumber = 0; | |
//The number of digits for the number as string | |
var numDigits = 0; | |
var spaceString = ""; | |
for ( var i =0 ; i < stringList.length ; i ++) | |
{ | |
var currentNumberMatch = stringList[i].match(regex); | |
if (stringList[i] === newName && greatestNumber == 0) greatestNumber++; | |
else if (currentNumberMatch !== null) | |
{ | |
//if its a decimal number, keep only the integer part | |
var numberAsString = currentNumberMatch[2]; | |
//convert to int | |
var numberAsInt = parseInt(currentNumberMatch[2],10); | |
if(numberAsInt >= greatestNumber ){ | |
greatestNumber = numberAsInt + 1; | |
spaceString = currentNumberMatch[1]; | |
} | |
//check if there are zeroes before the number, count the digits | |
if(numberAsInt.toString().length < numberAsString.length && numDigits < numberAsString.length) numDigits = numberAsString.length; | |
} | |
} | |
//add leading 0 if needed | |
if (greatestNumber > 0) | |
{ | |
//convert to string with leading zeroes | |
if (greatestNumber == 1) { | |
greatestNumber++; | |
spaceString = " "; | |
} | |
newName += spaceString; | |
greatestNumber = addZeroes(greatestNumber,numDigits); | |
newName += greatestNumber; | |
} | |
return newName; | |
} | |
function addZeroes(num, numDigits, base) | |
{ | |
if (base == undefined) base = 10; | |
var result = num.toString(base); | |
while(numDigits > result .length) | |
{ | |
result = "0"+ result ; | |
} | |
return result; | |
} | |
function newUniqueEffectName (newName ,layer){ | |
var nbFx = layer.effect.numProperties; | |
var nameFx = []; | |
for( var i = 0 ; i < nbFx ; i++){ | |
nameFx.push(layer.effect(i+1).name); | |
} | |
return newUniqueName(newName, nameFx) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment