Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Script Help - reference list of macro names

I've modified Marco R's script for creating a Loot Macro Mule  to create a Macro Mule character of my own.  I'm stuck on one little bit where I'm trying to remove/replace any Collection Macros that are generated by the script, but currently it is deleting all  Collection Macros. Here's the section of code: if(msg.type=="api" && msg.content.indexOf("!createCollectionMacros")==0){ macroList =[ [`Menu`,`%{${mmName}|Menu} &{noerror} `], [`Stats`,`%{${mmName}|Stats} &{noerror} `], [`Rolls`,`%{${mmName}|Rolls} &{noerror} `], [`Spells`,`%{${mmName}|Spells} &{noerror} `] ] mmCollectionMacro = findObjs({type:'macro'})[0]; if(mmCollectionMacro){ macro = getObj('macro',mmCollectionMacro.id); var existingMacros = findObjs({type: 'macro'}); _.each(existingMacros, function(macro) { macro.remove(); }); _.each(macroList, function(item){ createObj('macro',{ name:item[0], action:item[1], visibleto: "", playerid: msg.playerid, istokenaction:true }); }); } else { let macro = createObj('macro',{ }) _.each(macroList, function(item){ createObj('macro',{ name:item[0], action:item[1], visibleto: "", playerid: msg.playerid, istokenaction:true }); }) } sendChat(`${scriptName}`,`/w gm Collections Macros for "${mmName}" were created`); log(`Collections Macros for "${mmName}" were created`); } }) I'm pretty sure I just need to modify this line, but I can't figure out how to reference the list of names from the macroList: var existingMacros = findObjs({type: 'macro'}); I've been trying variations of things like this, but they're not working and I'm at the limit of my javascript coding skills: var existingMacros = findObjs({type: 'macro', name: macrolist[0]}); Any help would be appreciated!
1658526665

Edited 1658541154
GiGs
Pro
Sheet Author
API Scripter
I havent read your script, I just skimmed to the end but what you probably want is some kind of filter, like var existingMacros = findObjs({type: 'macro'}).filter(x => x=== macrolist[0]); This will take the existingMacro array, and discard any that aren't equal to macrolist[0], so you might want a different kind of filter (or might want t compare some specific key in the filter like var existingMacros = findObjs({type: 'macro'}).filter(x => x.id === macrolist[0]); As I said, I haven't looked at yyour script, so don't know exactly what is needed, but this is a good place to start.
1658940041

Edited 1658941659
Hmm, I've tried a bunch of variations on this, but I haven't been able to suss it out yet, as it's still deleting everything. Here's a few examples that I've tried:             var existingMacros = findObjs({type: 'macro'}).map(n => n.name).filter(x => x.id === macrolist[0]);             var existingMacros = findObjs({type: 'macro'}).map(n => n.name).filter(x => x === macroList[0]);             var existingMacros = findObjs({type: 'macro'}).filter(x => x=== macrolist[0]);             var existingMacros = findObjs({type: 'macro'}).filter(x => x.name === macroList[0]);             var existingMacros = findObjs({type: 'macro'}).filter((x) => { return x.get('name') === macroList[0]}); If anyone else is able to help, here's the full section of code: on("ready",function(){ const mmName = 'MacroMuleTest';     on("chat:message",function(msg){    if(msg.type=="api" && msg.content.indexOf("!createCollectionMacros")==0){         macroList =[              [`Menu`,`%{${mmName}|Menu} &{noerror} `],             [`Stats`,`%{${mmName}|Stats} &{noerror} `],             [`Rolls`,`%{${mmName}|Rolls} &{noerror} `],             [`Spells`,`%{${mmName}|Spells} &{noerror} `]                     ]         mmCollectionMacro = findObjs({type:'macro'})[0];         if(mmCollectionMacro){             macro = getObj('macro',mmCollectionMacro.id);            var existingMacros = findObjs({type: 'macro'}).filter(x => x=== macrolist[0]);             _.each(existingMacros, function(macro) {                 macro.remove();               });               _.each(macroList, function(item){                   createObj('macro',{                   name:item[0],                   action:item[1],                   visibleto: "",                   playerid: msg.playerid,                   istokenaction:true                 });               });             } else {                            let macro = createObj('macro',{               })               _.each(macroList, function(item){                        createObj('macro',{                     name:item[0],                     action:item[1],                     visibleto: "",                     playerid: msg.playerid,                     istokenaction:true                 });               })             }         sendChat(`${scriptName}`,`/w gm Collections Macros for "${mmName}" were created`);         log(`Collections Macros for "${mmName}" were created`);     }        })     })
I'm sure there's a better way to do this with a filter, but I just compared them all manually:             var existingMacros = findObjs({type: 'macro'}).filter(x => x.get('name') === Menu || x.get('name') === Stats || x.get('name') === Rolls || x.get('name') === Spells);
1658964354
The Aaron
Roll20 Production Team
API Scripter
Something like: 1 let existingMacros = findObjs ({ type : 'macro' }). filter ( m => [ 'Menu' , 'Stats' , 'Rolls' , 'Spells' ]. includes ( m . get ( 'name' ));
The Aaron said: Something like: 1 let existingMacros = findObjs ({ type : 'macro' }). filter ( m => [ 'Menu' , 'Stats' , 'Rolls' , 'Spells' ]. includes ( m . get ( 'name' )); Yeah that's a bit prettier! :)  Is there a way to simply compare the filter to the macroList array? 
1658967508
The Aaron
Roll20 Production Team
API Scripter
Yeah, this would probably work: 1 let existingMacros = findObjs ({ type : 'macro' }). filter ( m => macroList . map ( n => n [ 0 ]). includes ( m . get ( 'name' ));