Here you go: on('ready',function(){
"use strict";
let nameFromSide = function(token,prev){
let imgs=_.map(token.get('sides').split(/\|/),(i)=>{
return i.replace(/%([a-zA-Z0-9]{2})/g,(m,n)=>String.fromCharCode(parseInt(`0x${n}`)));
}),
img=token.get('imgsrc'),
tableItem=_.chain(findObjs({type:'tableitem'}))
.reduce( (m,i)=>{
m[i.get('rollabletableid')] = m[i.get('rollabletableid')]||[];
m[i.get('rollabletableid')].push(i);
return m;
},{})
.filter( (t)=>{ return _.keys(t).length===imgs.length;})
.reduce( (m,t)=>{
let count=_.reduce(t,(cnt,ti)=>{
if(_.contains(imgs,ti.get('avatar'))){
cnt++;
}
return cnt;
},0);
if(count===imgs.length){
m.push(t);
}
return m;
},[])
.first()
.filter(t=>t.get('avatar')===img)
.value()[0];
if(tableItem && tableItem.get){
token.set({
name: tableItem.get('name'),
showname: true
});
}
};
on('change:token:currentSide',nameFromSide);
on('add:token',function(obj,prev){
if(obj.get('sides').length){
nameFromSide(obj,prev);
}
});
});
This will just work, provided you don't change the original table by adding or removing entries. It will find all the rollable table with the same number of sides and the same images. If it finds more than one, it will just take the first one (which is generally the oldest one). You can change the names in the table with impunity, but if you ever need to add images, you'll have to recreate the token in order for it to work with the new expanded table. Doing it this way means you can reuse the same images in multiple tables without getting the wrong names. Let me know if you have any issues. Happy Rolling!