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

API script to copy token and add #1, #2, #3 to new copies?

I am looking for a feature I really miss from maptools. When you copy/paste a token each new token got a #Number appended to the name. So that if I copied "Orc" it would paste a new one as "Orc #1" then "Orc #2". This really helps me when differentiating them. Has someone already done this?
1382296668
Lithl
Pro
Sheet Author
API Scripter
The API cannot currently create tokens, even by copying them. That said, the API can create an event to do something when you create a token manually, and you could then alter the token's name. Something like this: on('add:graphic', function(obj) { var allToks = findObjs({ _type: 'graphic', _subtype: 'token', _pageid: Campaign().get('playerpageid') }); var count = 0; _.each(allToks, function(tok) { if (tok.id == obj.id) return; if (tok.get('name') == obj.get('name')) count++; }); if (count > 0) obj.set('name', obj.get('name') + ' #' + (count + 1)); });
I did some initial testing and it would seem to just give the tokens names like "#2" without the "Orc" in them. I did some clumsy tweaks trying to sort it out but this is my first go with the API language and it seems it doesn't work as I would expect (come from java/C). I'll dork with it some more tonight and see what I can do.
1382363842
Lithl
Pro
Sheet Author
API Scripter
Celestian said: I did some initial testing and it would seem to just give the tokens names like "#2" without the "Orc" in them. While I admit I didn't test the snippet, it should append #2/etc. to the token's name when the token is created if there are any other tokens on the same map with the same 'base' name. Naming the tokens just "#2" should only happen if there are unnamed tokens and the new token doesn't have a name (which should only happen if you're copy/pasting a token without a name or dragging in a token from the art library).
I'd just add a line && if (tok.get('name') != '')