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

Script to select token with known id

So my issue is that I have a script that looks for a selected tokens id to pass the rest of the script action. This is fine for most of my usage of the script, but, I have a particular need for a group of tokens to be selected at once and each to execute a different script from the api. I can do it manually by selecting the relevant token and running the script, but for my purposes I need the process to run consecutively and fairly automatic (as there are quite a few tokens to select). These tokens do not change on the board, and they need to be run on the script together for the effect I want to work. What I am looking for, to make a long story short-er, is an api that can mimic the mouse selection of a token (select and deselect) so that I can I run the entire script as a macro. Since I am using the same tokens throughout, their IDs shouldn't change which means they should be able to be located, yes? Any help on this would be greatly appreciated.
1550729621

Edited 1550729753
GiGs
Pro
Sheet Author
API Scripter
It's hard to suggest specific code without seeing your code, but if you have a group of tokens selected, you have all of their ids already in the msg object. For instance something like this should work msg.selected.forEach( function(token) { // loop through selected tokens const id = token.id    // get this token's id // do whatever you want with that token } The trick will be deciding which script should be run on each token. How do you decide that?
Thanks GiGs, there isn't a lot to the script after getting the id, checks if get.obj of selected is graphic and then sends that on to another function. Yeah, I am not sure how to breakdown which id goes where. I am thinking of parsing them and looking for a unique handle, maybe in the tokan_name, not sure. Then direct which handle corresponds to which script. But I am not savvy with java, I think my script might end up looking quite messy. Simple would be best I think. I think something I wish could be done (given that i don't know java very well) is a function that allows you to select a token as you would normally with the mouse, then i could just loop the function for all selected tokens.
It may even make it easier for me, cuz once I have that function I could jut set it up as a macro on all the tokens I want it on and presto.
1550738103
GiGs
Pro
Sheet Author
API Scripter
Michael H. said: I think something I wish could be done (given that i don't know java very well) is a function that allows you to select a token as you would normally with the mouse, then i could just loop the function for all selected tokens. It's javascript, btw, not java. They are two different languages. Unfortunately, there's no way to do that with the API.  You can launch a script which applies to tokens based on a variety of criteria, you just can't have it operate like the mouse, manually selecting them.  What scripts do you need to do? Give as much detail as possible to describe what you need to do (and which tokens you want to affect), and we can tell you the best way to automate it.
Okay, so I have maybe 20 different tokens that are controlled by 5 players. The current script reads certain attributes on each token that are used to define the name of a particular object (out of a set of objects: all draw shapes on the gm layer) which mark the pathway for the tokens to move along. At current, the script can only be run 1 token at a time (very tedious) because if multiple tokens are selected they all follow the same pathway. I assume only the first token's attribute is parsed, which defines which pathway to follow for all other tokens. I don't know how to resolve selecting multiple tokens and allow each attribute to define the pathway object for each token. Oh yeah javascript lol brain fart :D
1550793443
GiGs
Pro
Sheet Author
API Scripter
Your information is too vague to provide a solution. I am certain what you want can be done with an API script, where you click a button and the script runs, analuses the tokens and moves them appropriately. But it can't be written without concrete details.
Sorry GiGs for being a pain. I have been using an API script: Patrols using Waypoints from here . All credit to Matt for writing the API it works like a treat. My issue is that although i can select multiple tokens to move to a path, there isn't an option to have each token move to a different path when selected together. It becomes tedious selecting each and setting the patrol for each. My suspicion is that it comes down to this section of the script (but I don't really know): WPP.SetTokenToPatrol = function(selected, opts) { //check to see if a token has been selected if (!selected || selected.length === 0) { return 'Err: no token selected'; } if (!state.WaypointPatrol['tokens']) { state.WaypointPatrol['tokens'] = {}; } if (!opts || !opts['patrolname'] || opts['patrolname'].trim() === '' ) { return 'Err: patrol name missing'; } opts['patrolname'] = opts['patrolname'].toLowerCase(); var pageid = getObj(selected[0]['_type'], selected[0]['_id']).get('_pageid'); if (!state.WaypointPatrol['paths'] || !state.WaypointPatrol['paths'][pageid] || !state.WaypointPatrol['paths'][pageid][opts['patrolname']]) { return 'Err: patrol name does not exist for this map'; } var points = state.WaypointPatrol['paths'][pageid][opts['patrolname']]; for (i in selected) { if (selected[i]['_type'] === 'graphic') { state.WaypointPatrol['tokens'][selected[i]['_id']] = { 'patrolname' : opts['patrolname'], 'active' : opts['active'] || true, 'patrolmode' : opts['patrolmode'] || 0, 'movementrate' : opts['movementrate'] || getObj('page', pageid).get('scale_number'), 'rotation' : isNaN(opts['rotation']) ? false : opts['rotation'], 'random' : opts['random'] || false, 'phase' : opts['phase'] || 0, }; //move token to closest point on patrol path var obj = getObj('graphic', selected[i]['_id']); WPP.MoveTokenToPath(obj, points); } } return 'Success: patrol added'; } And my thought is that the selected tokens get read as sharing the same patrol path. I have a macro that calls the api command like this: !wp patrol path:s_@{selected|bar1} mode:0 speed:5 rotation:false Where the paths are differentiated by the value of bar1 of the selected tokens. Now that I'm looking at it, the @{selected|bar1} might be the issue if it only reads the first token and applies that to the whole script?  😕 very much not certain. Thanks again GiGs for putting up with my vagary. If you have any insights they are more than welcome.
1550867020
GiGs
Pro
Sheet Author
API Scripter
It does look like the script is set up to move groups of tokens, so yes, that macro you are running is limiting the script to the first one in the selection. The problem is a but earlier than the code you're looking at, in the section starting: on ( 'chat:message' , function ( msg ) { The script grabs all the options (which path to assign, etc) from the macro text and in your case, that means whatever you have in bar1 is going to be assigned to all tokens in the section. That means the script would need a pretty big rewrite to be able to handle your approach. Have you treated manually selecting only  tokens with the same bar1 text, and running your macro? If that works, it would break it up into steps but be quicker than having to do it one at a time.
I have tried doing it that way, and honestly it's only mildly faster than doing them one-by-one. Part of the problem is there are many paths setup for patrol and many tokens that can switch or change paths, and the order they do that in is not sequential so picking out the same tokens is a task n' a half for me. Ahh so it's in the event call! Well I was worried you were going to say it needs a big rewrite.
1550893093
GiGs
Pro
Sheet Author
API Scripter
Unfortunately it would need a pretty significant rewrite. I cant think of a way to solve the problem.
Thanks anyway GiGs. I will probably have to use another script to run it.