Here's an alternative version which uses the StringSet object provided by Opus, reducing the amount of code.
(In general, searching a set is usually more efficient than searching an array. In this case it won't make any real difference as filenames will only contain a handful of words. But it still makes things easier.)
Another difference is that the word matching is done case-insensitive (moo = Moo etc.).
If you want it to be case-sensitive, change DOpus.Create.StringSetI() to DOpus.Create.StringSet().
It also takes care of file extensions by using the built-in properties to split the stem and extension. (The original script would turn moo moo moo.txt into moo moo.txt as it saw the last word as moo.txt. The script below takes care of that.)
function OnGetNewName(getNewNameData)
{
var splitby = " ";
var joinwith = " ";
var ArrOld = getNewNameData.newname_stem_m.split(splitby);
var SetUnique = DOpus.Create.StringSetI();
var ArrNew = [];
for (var i = 0; i < ArrOld.length; i++)
{
if (SetUnique.insert(ArrOld[i]))
{
ArrNew.push(ArrOld[i]);
}
}
return ArrNew.join(joinwith) + getNewNameData.newname_ext_m;
}
Thanks. Very elegant solution! I stumbled upon the file extension problem when testing the original version, I had assumed the Ignore extension option had stripped it. Another thing learned
How else would you know how to create a StringSet at all? You have to go there to make one, at which point it tells you how to choose which one you make.