How to batch rename token names (not file name) outside MT?

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
User avatar
Oryan77
Dragon
Posts: 452
Joined: Sun Feb 20, 2011 3:14 pm

How to batch rename token names (not file name) outside MT?

Post by Oryan77 »

Is there any way to batch rename token names (not their file names) outside of Maptool? Apparently I need to change a single character in all of my token names so I can get Wolph's newer versions of the BoT to work. I had used a semicolon in my token names (Devil; Pit Fiend) and I guess I can't do that anymore. So I need to remove it or use a different character.

I'm hoping there is a way to change the token name data without needing to drag/drop every file in MT and then save them back out (again...uhg). I've already had to rename these a few years ago because I used a comma and that caused issues with the summoning macro. I have a few thousand tokens and I really can't take time to do this manually one by one all over again.

And for future reference, which characters should I avoid using so that I will never have to worry about them interfering with programming mechanics? It seems a little odd to me that I can't use a comma in a name, but whatever. I can deal with it.

The reason I name tokens like that rather than just putting the creature type name in the label or DM field is because I prefer to have the tokens sorted in the Map Explorer window by creature type and then name.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: How to batch rename token names (not file name) outside

Post by aliasmask »

Oryan77 wrote:And for future reference, which characters should I avoid using so that I will never have to worry about them interfering with programming mechanics? It seems a little odd to me that I can't use a comma in a name, but whatever. I can deal with it.
Generally speaking names should only have letters and numbers, but _ and - are usually okay. Most of the coding problems with special characters stems from coding shortcuts. Using lists or string properties have character limitations like , and ;.

As for using a batch to change all the names with in zip, inside the xml file would be a pain but can be done. It'll probably be less work to manually rename and less time consuming. It would have to be a custom program using java or something and I don't think there are any tools out there that would open a zip file, search an xml file inside and change a value. But that's what you would need.

Here's a bit of code I use for players to let them know about a player name warning, but these should apply to token names as well. Essentially, don't use gm, self, all for names and use these characters: "a-zA-Z0-9._ -".

Code: Select all

[H: nameWarning = if(json.contains(json.append("","gm","self","all"),lower(playerName)),1,0)]
[H, if(! nameWarning): nameWarning = if(getFindCount(strfind(playerName,"[^a-zA-Z0-9._ -]")),1,0)]
[H, if(nameWarning): am.play.output(am.play.errorMessage("Name Warning",getMacroName(),playerName),json.append("",playerName,"gm","server"))]

badsequel
Giant
Posts: 115
Joined: Thu May 31, 2012 3:13 am

Re: How to batch rename token names (not file name) outside

Post by badsequel »

Oryan77 wrote:I'm hoping there is a way to change the token name data without needing to drag/drop every file in MT and then save them back out (again...uhg). I've already had to rename these a few years ago because I used a comma and that caused issues with the summoning macro. I have a few thousand tokens and I really can't take time to do this manually one by one all over again.
Got inspired and wrote a Python3 program to do a little string replacement in <name> tag inside the .rptok file. Should be a link to a .zip file in my signature.

For instance it changes this:

Code: Select all

<name>Hostile;RabidGoblin</name>
To this:

Code: Select all

<name>Hostile_RabidGoblin</name>
The program grabs ALL .rptok files in a folder and goes looking for strings to replace in the name. On the other end of processing new .rptok files pop out - which can be drag'n dropped into MT without any problems(atleast on my end).

Might be what you are looking for. Was fun writing nevertheless :D

There is a config file that allows you to tweak what strings to replace.
Ps:
  • If "Python" and "pip install lxml" has no meaning for you - ignore this post.
    Read the readme.txt file in the .zip
    Check out the config.ini also
    Make sure to test on some copies of tokens you care about to begin with. I might have missed something in the code..
My shared rpg stuff:
(exe)TokenNameChanger3: https://www.dropbox.com/s/cqsof54v3dl2k ... 3.zip?dl=0
Tiddly Spell Wiki: https://www.dropbox.com/s/53pya4k68mnvc ... ddly01.htm

Stuff to check out:
Mote Kickstarter(back it): https://www.kickstarter.com/projects/74 ... abletop-ev

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: How to batch rename token names (not file name) outside

Post by aliasmask »

What if you made an .exe file for him using py2exe? Would he still need python installed? Could he still edit a config file outside of the exe or update the code to ask for config settings. I don't know python, but looking at the code is seems pretty straight forward. You extract the file, change the name and repack it to specified output folder.

badsequel
Giant
Posts: 115
Joined: Thu May 31, 2012 3:13 am

Re: How to batch rename token names (not file name) outside

Post by badsequel »

aliasmask wrote:What if you made an .exe file for him using py2exe? Would he still need python installed? Could he still edit a config file outside of the exe or update the code to ask for config settings. I don't know python, but looking at the code is seems pretty straight forward. You extract the file, change the name and repack it to specified output folder.
I've never touched py2exe, but I can take a look at it. Good tip
My shared rpg stuff:
(exe)TokenNameChanger3: https://www.dropbox.com/s/cqsof54v3dl2k ... 3.zip?dl=0
Tiddly Spell Wiki: https://www.dropbox.com/s/53pya4k68mnvc ... ddly01.htm

Stuff to check out:
Mote Kickstarter(back it): https://www.kickstarter.com/projects/74 ... abletop-ev

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: How to batch rename token names (not file name) outside

Post by aliasmask »

If you're open to suggestions, I would only extract the content.xml file then merge it in to same zip (if python can do) overwriting content.xml. It'll save time on clean up and you won't have all those image files to contend with. Also, either ask for a folder or use location of exe file for folder, create an output folder from that location to put all the renamed tokens. Check the name to see if it was changed, if not then don't create a copy. Another option would be to rewrite original file and not create an output directory.

User avatar
Oryan77
Dragon
Posts: 452
Joined: Sun Feb 20, 2011 3:14 pm

Re: How to batch rename token names (not file name) outside

Post by Oryan77 »

Should I wait for this exe file to be created or is this something nobody is actually going to do? Sounds helpful since I'm not a programmer type and I have no clue what you guys mean by python or py2exe. :P

badsequel
Giant
Posts: 115
Joined: Thu May 31, 2012 3:13 am

Re: How to batch rename token names (not file name) outside

Post by badsequel »

Oryan77 wrote:Should I wait for this exe file to be created or is this something nobody is actually going to do? Sounds helpful since I'm not a programmer type and I have no clue what you guys mean by python or py2exe. :P
First things first: any day is a good day to start learning Python! ;)

That aside I have tried py2exe and it works. \O/
Have some cleaning up to do in the code and I will try to implement some of AM's suggestions.

I will post a working .exe in a day or two.
My shared rpg stuff:
(exe)TokenNameChanger3: https://www.dropbox.com/s/cqsof54v3dl2k ... 3.zip?dl=0
Tiddly Spell Wiki: https://www.dropbox.com/s/53pya4k68mnvc ... ddly01.htm

Stuff to check out:
Mote Kickstarter(back it): https://www.kickstarter.com/projects/74 ... abletop-ev

badsequel
Giant
Posts: 115
Joined: Thu May 31, 2012 3:13 am

Re: How to batch rename token names (not file name) outside

Post by badsequel »

Sig updated with link to .exe variant.

Haven't figured out why py2exe insists on zipping 15mb of stuff with such a simple program.. But as long as it runs, right? :D

Anyway..
Unzip somewhere, check config.ini and put some values in there that works for you.
The program will ask you to confirm the values before running(run it from a terminal).
The program is fairly dumb so please run on copies of tokens first!

Sadly the program cannot extract, modify and reinsert the content.xml file only in a zip archive(rptok) due to the lack of that functionality in the zipfile python library, so every rptok has to be unpacked, checked for strings and repacked if modified. Until I find some better alternative, the program will remain rather inefficient in this regard.

I haven't found any really nasty bugs using it. Still, you are solely responsible for all results from running this.
Should you be really paranoid, make a ramdrive, unzip, config and run everything from there.
My shared rpg stuff:
(exe)TokenNameChanger3: https://www.dropbox.com/s/cqsof54v3dl2k ... 3.zip?dl=0
Tiddly Spell Wiki: https://www.dropbox.com/s/53pya4k68mnvc ... ddly01.htm

Stuff to check out:
Mote Kickstarter(back it): https://www.kickstarter.com/projects/74 ... abletop-ev

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: How to batch rename token names (not file name) outside

Post by aliasmask »

It's probably packing all your library files. But I agree, 15mb is alot for what is essentially just text.


badsequel
Giant
Posts: 115
Joined: Thu May 31, 2012 3:13 am

Re: How to batch rename token names (not file name) outside

Post by badsequel »

wolph42 wrote:Great! Thanks for the effort
Thank you
My shared rpg stuff:
(exe)TokenNameChanger3: https://www.dropbox.com/s/cqsof54v3dl2k ... 3.zip?dl=0
Tiddly Spell Wiki: https://www.dropbox.com/s/53pya4k68mnvc ... ddly01.htm

Stuff to check out:
Mote Kickstarter(back it): https://www.kickstarter.com/projects/74 ... abletop-ev

User avatar
Oryan77
Dragon
Posts: 452
Joined: Sun Feb 20, 2011 3:14 pm

Re: How to batch rename token names (not file name) outside

Post by Oryan77 »

Ok, so I have 3 comments to make...

1. Great job and thank you so much for creating this. It seems to work perfectly fine.

2. Is there a way I can get it to change a bad character and the space that follows it? My token names look like this: "Baatezu; Pit Fiend 1". This file will change the ; into a _ but if I choose to use the _, I would like to remove the space that follows the ; so that the name looks like: "Baatezu_Pit Fiend 1". I tried editing the cfg to change ; with a space following it, but it doesn't seem to recognize that command and the name keeps the space after the _ so it still looks like: "Baatezu_ Pit Fiend 1".

3. Wolph mentioned that tokens can have bad ID names. He has a macro in his Bag of Tricks that will check maps for this issue and automatically change the ID. Is there a way to do this outside of Maptool similar to the file you just created? Maybe have this file do both operations at once?

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: How to batch rename token names (not file name) outside

Post by wolph42 »

I can answer no 3: no. Id's are created inside the campaign file, eg if you copy paste a token it gets a new Id and in some cases cut and paste as well.

badsequel
Giant
Posts: 115
Joined: Thu May 31, 2012 3:13 am

Re: How to batch rename token names (not file name) outside

Post by badsequel »

Oryan77 wrote:Ok, so I have 3 comments to make...

1. Great job and thank you so much for creating this. It seems to work perfectly fine.

2. Is there a way I can get it to change a bad character and the space that follows it? My token names look like this: "Baatezu; Pit Fiend 1". This file will change the ; into a _ but if I choose to use the _, I would like to remove the space that follows the ; so that the name looks like: "Baatezu_Pit Fiend 1". I tried editing the cfg to change ; with a space following it, but it doesn't seem to recognize that command and the name keeps the space after the _ so it still looks like: "Baatezu_ Pit Fiend 1".

3. Wolph mentioned that tokens can have bad ID names. He has a macro in his Bag of Tricks that will check maps for this issue and automatically change the ID. Is there a way to do this outside of Maptool similar to the file you just created? Maybe have this file do both operations at once?
1 - you are welcome. It was fun making the program. :D
2 - hmm. That needs some research. I do not know if the python library configParser can preserve leading and trailing whitespace.
3 - no. Recreating an algorithm that creates whatever passes as mt ids is out of scope, I feel. If the next iteration on Mt has an integration point where I could ask to have an id created for me I would considerit. JamzTheMan might have some thoughts on that as he has a http enabled variant of Mt.
My shared rpg stuff:
(exe)TokenNameChanger3: https://www.dropbox.com/s/cqsof54v3dl2k ... 3.zip?dl=0
Tiddly Spell Wiki: https://www.dropbox.com/s/53pya4k68mnvc ... ddly01.htm

Stuff to check out:
Mote Kickstarter(back it): https://www.kickstarter.com/projects/74 ... abletop-ev

Post Reply

Return to “MapTool”