I am writing some custom scripts, and I make heavy use of msg.selected to perform actions on all selected tokens. This generally works great, but I have discovered that if an api invocation includes "@{target|...|token_id}", msg.selected is empty in the on('chat:message') handler. Notably, in the UI the selection highlight around the selected tokens disappears during targeting, and does not reappear until after the chat message is processed. Presumably this is the source of the bug. function command1(selected) { ... }
function command2(selected, target) { ... }
// Example 1
// Calling "!my_script"
// executes command1
// on each selected token.
on('chat:message', msg => {
msg.selected.map(x => getObj(x._type, x._id)).forEach(command1);
});
// Example 2
// Calling "!my_script @{target|MyTarget|token_id}"
// should execute command2
// on each selected token
// with the chosen target.
on('chat:message', msg => {
let args = msg.content.split(' ');
let target = getObj('graphic', args[1]);
msg.selected.map(x => getObj(x._type, x._id)).forEach(selected => command2(selected, target));
}); Example1 above works as expected, but Example2 fails because msg.selected is empty. I see this issue was reported here 4 years ago ( old thread ), but there was no response at all. Has anyone found a workaround or a fix for this in the intervening time?