The token ID is, apparently, 32 randomly assigned alpha-numeric characters. The bug occurs when it happens that all of those characters are numeric, so the ID from, say, getTokens, is treated as a number instead of a string. Since token IDs always seem to start with eight zeros, those get trimmed, leaving a 24 character number. (Is that correct? Does the bug always manifest in the form of trimming leading zeros? If so, this should work very consistently.)
The following uses getTokens to get list of all tokens on the Token layer. It then scrolls through them with foreach, checking the length of the ID (because I'm not 100% certain that it will always be eight leading zeros, and there's no reason not to make this work for any arbitrary number), and does a for() to construct a pad. However, the pad can't be constructed with zeros, because it will only, ever, be a single zero, so it constructs the pad using the letter "a" instead. When that's the proper length, it then ads a non-numeric character at the end (exclamation point, in this case), then replaces all the a's with zeros. So it constructs a pad that looks like this: "00000000!", forcing it to be treated as a string.
That pad, in a substring to remove the "!", is then used directly in the switchToken command. This works.
The output of the whole thing is a table showing token.name (from switchToken, so that I know it's working), the reported token ID, and the corrected 32 character token ID, in a table.
Code: Select all
[h:TokenList = "<TABLE BORDER=1><TR><TD>Token Name</TD><TD>Reported ID</TD><TD>Corrected ID</TD></TR>"]
[h: ids = getTokens(",",'{layer:["TOKEN"]}'))]
[h,foreach(id, ids,""), code:
{
[h:Pad = ""]
[if(length(id) != 32), code:
{
[h:PadLength=32 - length(id)]
[for(i,0,PadLength), h:Pad = add(Pad,"a")]
[h:Pad = Pad + "!"]
[h:Pad = replace(Pad,"a","0")]
};
{
[h:PadLength=0]
}]
[switchToken(substring(Pad,0,PadLength) + id)]
[TokenList = TokenList + "<TR><TD>" + token.name + "</TD><TD>" + id + "</TD><TD>" + substring(Pad,0,PadLength) + id + "</TD></TR>"]
}]
[h:TokenList = TokenList + "</TABLE>"]
[r:TokenList]
I've only used this in getTokens, but it should work with any other function that uses token IDs. I strongly suspect it could be made more elegant, starting, perhaps, with the pad starting off as just the exclamation point and adding zeros in front of it, thus eliminating the repace() step), but this works for me.
I think it should be possible to put this whole thing into a user defined function, and use the output of that in switchToken, but that process may trim off leading zeros, too. I haven't tried yet. I'm fairly certain the complete, corrected token ID with a ! at the end could be stuffed into a JSON array, and the json.get inside a substring could be used. But I haven't tested that, either.
Does any of this make sense to anyone who actually knows what they're doing?