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

Looking for Script to Swap Token GM Notes and Token Name

I've been using this scriptlet from The Aaron to swap token names from modules to generic descriptors -- e.g. the gnomes in Gnomengarde for DoIP are all linked to the Rock Gnome Recluse character sheet, but each one has a unique token name.  However, I want the tokens to simply display 'Rock Gnome #', so I added the given token name to the GM Notes field (using !set-gmnote), and then changed the token name to 'Rock Gnome #' (using the TokenNameNumber script ). This works ok, but there's still a bit of a manual process, and I can see that switching back during a game (when players meet the Gnomes I'll put their names back on the tokens... which is why I kept the names to begin with) would be much easier if there was a way for a script to pull from the token GM Notes field. I'm hoping there's an easy way to just swap the GM Notes field with the Token Name, so that I could set up a API Call macro ('!TokenNameSwap' or similar) and it would put the Token Name in the GM Note field and the GM Note as the Token Name.  Best case scenario would be to have the GM Notes field have a 'marker' of some kind for the token name, like "Token name: Fibberdish" that could be pulled from the GM Notes while leaving the rest intact (in case there's other notes) but as of now that isn't something I'm currently using. But I also know that requires a script that can parse the GM Notes field for that marker and only replace that line, so definitely beyond what I'd be able to kludge together. Does anyone have anything that could accomplish this? Thanks!
1604695810
The Aaron
Roll20 Production Team
API Scripter
The command is: !swap-token-name It will find the line like this: token name: <Something> and swap that with the token's current name.  Running it twice on the same token should be an identity operation. Note that it ignores case and whitespace on the line, so this will also work: TokenName:<Something> It will only use the first line it finds, but it might replace the name in the notes if it occurs other places (though it should also change it back just fine).  Special characters in names might confuse it (like letters with accents, etc.).  If that happens, give me some examples and I'll fix it. =D Script: on('ready',()=>{ const blockElements = [ 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul', 'pre', 'address', 'blockquote', 'dl', 'div', 'fieldset', 'form', 'hr', 'noscript', 'table','br' ]; const rStart=new RegExp(`<\\s*(?:${blockElements.join('|')})\\b[^>]*>`,'ig'); const rEnd=new RegExp(`<\\s*\\/\\s*(?:${blockElements.join('|')})\\b[^>]*>`,'ig'); const getLines = (str) => (rStart.test(str) ? str .replace(/[\n\r]+/g,' ') .replace(rStart,"\r$&") .replace(rEnd,"$&\r") .split(/[\n\r]+/) : str .split(/(?:[\n\r]+|<br\/?>)/) ) .map((s)=>s.trim().replace(/<[^>]*>/g,'')) .filter((s)=>s.length) ; const regex = /^token\s*name:\s*([^\r\n]*)$/mi; const SwapTokenName = (t) => { let notes = t.get('gmnotes'); let lines = getLines(unescape(notes)).filter(l=>regex.test(l)); if(lines.length){ let m = lines[0].match(regex); let oldName = t.get('name'); let newName = m[1]; t.set('name',m[1]); let newNotes = notes.replace(newName.replace(/\s/g,'%20'), oldName.replace(/\s/g,'%20')); t.set('gmnotes', newNotes); } }; on('chat:message',msg=>{ if('api'===msg.type && /^!swap-token-name(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){ let tokens = (msg.selected || []) .map(o=>getObj('graphic',o._id)) .filter(g=>undefined !== g) ; tokens.forEach(SwapTokenName); } }); });
Amazing!  Thank you!
1604700992
The Aaron
Roll20 Production Team
API Scripter
No problem. =D