Need a macro to change a single character in a token name

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

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

Need a macro to change a single character in a token name

Post by Oryan77 »

I know the BoT has a macro that can fix bad alphanumeric characters in token names, but I have 3000 tokens that have a ; in the token name and the BoT is not currently configured to find and replace that particular character. I have asked Wolph42 for a possible feature to make that fix.

I don't know when/if Wolph42 will be able to provide that feature and I was hoping to fix my token names today since I don't have much spare time lately to fiddle with my MT stuff.

I'm hoping someone could provide me with a simple macro that will change any token names using a ; into a _ for any tokens I have selected on my token layer map. It would actually need to also remove the space after the ; but not any other spaces in the name. For example, "Human; Guy 1" to "Human_Guy 1".

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Need a macro to change a single character in a token nam

Post by aliasmask »

Do you want to replace only selected tokens are all tokens on token layer?

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

Re: Need a macro to change a single character in a token nam

Post by Oryan77 »

aliasmask wrote:Do you want to replace only selected tokens are all tokens on token layer?
I suppose all tokens on the token layer would be better in case I missed some.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Need a macro to change a single character in a token nam

Post by aliasmask »

Code: Select all

[H: allTokens = getTokens("json",json.set("{}","layer","TOKEN"))]
[H: numTokens = json.length(allTokens)]
[H: tokenSetSize = 1000]
[H: tokenSets = ceiling(numTokens / tokenSetSize)]
[H: counter = 0]
[H, c(tokenSets), code: {
   [H: startSet = roll.count * tokenSetSize]
   [H: theseTokens = json.get(allTokens,startSet,min(startSet+tokenSetSize,numTokens)-1)]
   [H, foreach(id,theseTokens), code: {
      [H: switchToken(id)]
      [H, if(indexOf(token.name,";")+1): token.name = replace(token.name,"; *","_"); counter = counter + 1]
   }]
}]
[H: counter = numTokens - counter]
[R: strformat("There were %{counter} tokens updated out of %{numTokens}.")] 
I have not tested this code, so you may want to save campaign before you do this.

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

Re: Need a macro to change a single character in a token nam

Post by wolph42 »

Oryan77 wrote:I don't know when/if Wolph42 will be able to provide that feature
As usual, not as fast as AM. But a response time of 6 hours (after getting the request at 3am in the morning aint bad...

I've just provided the new feature. And no its not so simple, there are many many factors to be taken into account.

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

Re: Need a macro to change a single character in a token nam

Post by Oryan77 »

Yes, very fast response Wolph42. Sorry, I know you are on the other side of the world from me, so I usually assume you won't be able to work on anything until I'm sound asleep. :P

BTW aliasmask, I tried your code but get this message:

Code: Select all

Invalid end index "0" for array (size of 299) in function "json.end".

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Need a macro to change a single character in a token nam

Post by aliasmask »

I have to put a -1 with the json get because I want to get array places of 0 to 999 (or size of array), but I used the higher value of 1000 just like the way you get a substring. Code above updated.

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

Re: Need a macro to change a single character in a token nam

Post by wolph42 »

There is a function avaliable to change the loop limit!
And IRC, the loop limit had changed:
Just created the wiki pages. Note that the limit is now (=1.4.0.2) 10,000
http://lmwcs.com/rptools/wiki/Category: ... ion_limits

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

Re: Need a macro to change a single character in a token nam

Post by wolph42 »

so with the loop limit out of the way you get:

Code: Select all

[H: allTokens = getTokens("json",json.set("{}","layer","TOKEN"))]
<!-- OR !! if you only want selected use this line-->
[H: allTokens = getSelected("json")]

[H, foreach(id,allTokens), code: {
  [switchToken(id)]
  [if(indexOf(token.name,";")+1): token.name = replace(token.name,"; *","_")]
}]

 

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

Re: Need a macro to change a single character in a token nam

Post by Oryan77 »

Thanks. This worked perfectly and all of my tokens appear to have working names now.

Post Reply

Return to “Macros”