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

API to Reveal a Handout to Players

1728925767

Edited 1728926501
Hello everyone! I am trying to create an API script that has the following functionality: When I have a token selected, I can click a macro that will run a script to search for handouts with a name similar to the token, and then reveal that handout to all players. Then, provide a feedback message if it was performed properly. I have had success with searching for and identifying handouts based on the token selected, but I cannot figure out how to change the visibility of a handout via API. I have seen some -really- old threads that state that this is not possible; is this still the case? Is there not a way to change the visibility of a handout via API? My current workaround is to match with a similar name, then whisper me a link to the handout which I can then click (I haven't been able to figure out how to have it just open either), and then click to reveal the handout. Below is my current working code. I will say, it does currently output all relevant handout names that it finds which is kind of helpful in the event it finds multiples that are similar.&nbsp; // API Script to Open Handout Based on Similar Selected Token Name (Whisper to GM) on("ready", function() { &nbsp; &nbsp; log("Open Handout by Similar Token Name script loaded."); &nbsp; &nbsp; on("chat:message", function(msg) { &nbsp; &nbsp; &nbsp; &nbsp; // Check if the command is from the API and is the correct command &nbsp; &nbsp; &nbsp; &nbsp; if (msg.type === "api" &amp;&amp; msg.content.startsWith("!openHandout")) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var selectedTokens = getSelected(msg); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (selectedTokens.length === 0) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("System", "Please select a token."); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var tokenName = selectedTokens[0].get("name"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var handoutsFound = []; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Search for handouts with names similar to the token name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findObjs({ type: "handout" }).forEach(function(handout) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Check for a partial match (case insensitive) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (handout.get("name").toLowerCase().includes(tokenName.toLowerCase())) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handoutsFound.push(handout); // Add matching handout to the array &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Notify the GM with clickable links &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (handoutsFound.length &gt; 0) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handoutsFound.forEach(function(handout) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let handoutId = handout.id; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("System", `/w gm You can view the handout here: [${handout.get("name")}](<a href="http://journal.roll20.net/handout/${handoutId})`" rel="nofollow">http://journal.roll20.net/handout/${handoutId})`</a>); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendChat("System", `No handouts found with similar names to "${tokenName}".`); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; }); }); // Helper function to get selected tokens function getSelected(msg) { &nbsp; &nbsp; if (msg.selected) { &nbsp; &nbsp; &nbsp; &nbsp; return msg.selected.map(function(o) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return getObj("graphic", o._id); &nbsp; &nbsp; &nbsp; &nbsp; }).filter(function(obj) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return obj !== undefined; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; } &nbsp; &nbsp; return []; } &nbsp;
1728928870
timmaugh
Pro
API Scripter
If you are worried about visibility impacted the by inplayerjournals or controlledby properties, those are both exposed to the API for you to set. You can use the Inspector script to see. Here is a report where I examine a handout named "UnSanity". So you can set either/both properties to include 'all' and be good.
1728930797
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Further info: You can change those properties, but the API cannot automatically open a handout for all players. The best it can do is send them a link to click on to open it.
Okay, so the field that I need to edit is "inplayerjournals." I see this now, and the script is working as intended. Thank you both so much! !&nbsp;