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 to change Character Name?

I know that TokenMod or ChatSetAttr can update the token_name, but is there a script out there that can update the actual name of the character sheet?  I see that Jakob answered a previous post that it wasn't possible with ChatSetAttr, but that it wouldn't be hard to do with a script, but I haven't found a script that does that. Basically I have several duplicate NPCs (e.g. Goblin) from different modules that I would like to differentiate by adding LMoP or DoIP to their character names.  If I have token actions set up for my current NPC, then transmogrify new ones from a different module, the token actions call will always pull the more recently created/imported character sheet, which doesn't have those token actions set up. The goal would be for a macro that basically does this (though this doesn't work because TokenMod and ChatSetAttr can't access the character name): !token-mod --set character_name|"@{selected|character_name} DoIP" !setattr --sel --character_name|"@{selected|character_name} DoIP"
The Aaron
Forum Champion
API Scripter
Use this: !set-char-name Some Name or !set-char-name %%NAME%% DoIP Anything after the command will be used as the name for all characters that selected tokens represent.  Using %%NAME%% will get replaced with the current name of the character. Code: on('ready',()=>{ on('chat:message',msg=>{ if('api'===msg.type && /^!set-char-name(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){ let name = msg.content.replace(/^[^\s]*\s+/,''); (msg.selected || []) .map(o=>getObj('graphic',o._id)) .filter(g=>undefined !== g) .map(t=>t.get('represents')) .reduce((m,i)=>[ Set([...m,i])],[]) .map(id=>getObj('character',id)) .filter(c=>undefined !== c) .forEach(c=>c.set('name',name.replace(/%%NAME%%/,c.get('name')))) ; } }); });
Thank you, once again!
Would it be possible to add a string to remove a section as well, to reverse the process?  E.g.  !set-char-name %%NAME%% LMoP --remove|DoIP would add LMoP and remove DoIP from the character name? Or maybe a simpler way: !remove-char-name DoIP That would remove DoIP then I could use the !set-char-name to LMoP. I haven't screwed my tokens up yet, but I may want to edit them in the future and need to remove those identifiers en masse. Side question: from my searching it looks like the API still doesn't have access to the character tags -- is that true? Or is there a way to edit the character tags now through the API?
The Aaron
Forum Champion
API Scripter
Yup: on('ready',()=>{ on('chat:message',msg=>{ if('api'===msg.type && /^!set-char-name(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){ let name = msg.content.replace(/^[^\s]*\s+/,''); (msg.selected || []) .map(o=>getObj('graphic',o._id)) .filter(g=>undefined !== g) .map(t=>t.get('represents')) .reduce((m,i)=>[ Set([...m,i])],[]) .map(id=>getObj('character',id)) .filter(c=>undefined !== c) .forEach(c=>c.set('name',name.replace(/%%NAME%%/g,c.get('name')))) ; } if('api'===msg.type && /^!remove-char-name(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){ let regex = new RegExp(`${msg.content.replace(/^[^\s]*\s+/,'')}`,'ig'); (msg.selected || []) .map(o=>getObj('graphic',o._id)) .filter(g=>undefined !== g) .map(t=>t.get('represents')) .reduce((m,i)=>[ Set([...m,i])],[]) .map(id=>getObj('character',id)) .filter(c=>undefined !== c) .forEach(c=>c.set('name',c.get('name').replace(regex,'').trim().replace(/\s+/g,' '))) ; } }); });
I am slowly trying to understand scripting, but am a complete novice. How would I alter this to make it allow me to remove a prefix instead of a suffix (or as an option)? It seems If I wanted to use this to Add a prefix to a name, rather than a suffix, I can simply use !set-char-name <prefix> %%NAME%% But running the  !remove-char-name <prefix>  doesn't work, cause I assume the code is telling it to look at the end of the string.
The script will remove whatever comes after the command.  For removing part of the character name, you only need: !remove-char-name NAMETOREMOVE In other words, if your character name is "Goblin DoIP" but you want the name to be just "Goblin" the command is:  !remove-char-name DoIP The script will remove whatever it finds after the  !remove-char-name  command. You don't use any other symbols like " " or < > around the name. If the character name is now "Goblin" but you want to add something, such as "LMoP" so that the character name is "Goblin LMoP" you would use: !add-char-name %%NAME%% LMoP The  %%NAME%%  part of the command takes whatever the current name is and replaces it in the new name. So you could also get "Small Goblin" if you started with "Goblin" and used: !add-char-name Small %%NAME%

Edited 1607498387
EDIT: Nevermind, this wasn't working for me at first, but now I see it does work. Not sure what my hiccup was, but it works now. I understand that. But, using your example, if I add a prefix such as "Small" to one or more using  !add-char-name Small %%NAME% that works fine. I already have that working. But If I later want to remove "Small" and I try to do  !remove-char-name Small that doesn't remove the "Small", I suspect because it's not at the end of the string. And I would like to strip a prefix, because my brain organizes things with prefixes, not suffixes.
Some scripts can take a bit of time to run.  The first time I used this script I didn't think it had gone through, so I ran it again, and ended up getting something like "Goblin DoIP DoIP" when they both processed.  I'm not sure how to help you troubleshoot if it's just a case of how long to wait -- you could look at adding a sendchat message from the API once the processing is finished.  But otherwise I'm glad it's working for you now!