Skip to content

Instantly share code, notes, and snippets.

@Parmeisan
Created April 2, 2014 18:53
Show Gist options
  • Save Parmeisan/9940642 to your computer and use it in GitHub Desktop.
Save Parmeisan/9940642 to your computer and use it in GitHub Desktop.
Roll20 - Batch Edit
var parm_batch = parm_batch || {};
parm_batch.debugging = false;//true;//
parm_batch.speak_as = "Batch-Edit";
parm_batch.whisper = "";
parm_batch.brush = [];
on("chat:message", function(msg)
{
var ns = parm_batch;
var words = msg.content.split(" ");
var w = 0;
if(msg.type == "api" && words[w++] == "!batch")
{
// Fill in the array of selected objects
var sel = [];
_.each(msg.selected, function(value, key)
{
ns.debug(value);
sel[key] = findObjs(value)[0];
});
ns.debug(sel);
var action = words[w++];
var prop = words[w++];
//sel = _.where(sel, { _type: "graphic" }); // Is this necessary?
if (ns.isnull(sel)) error("Nothing selected.");
else
{
ns.debug(sel);
if (action == "help")
{
// List properties of the selected object
ns.info("Commands: help (this), help props (shows valid properties for one selected item), " + //_
"show [prop-list] (shows stored value), " + //_
"brush [prop-list] (saves stored value for one selected item), " + //_
"paint [prop-list] (paints selected items with stored value)");
if (ns.isnull(prop))
{
if (!ns.isnull(sel))
{
var out = "";
out += "Properties:";
// Show all available property names
_.each(sel[0].attributes, function(value, key)
{
if (key.indexOf("_") != 0) out += " " + key;
});
ns.info(out);
}
}
}
else
{
while (!ns.isnull(prop))
{
// For all the given properties
if (action == "brush")
{
// Sets the brush
ns.brush[prop] = sel[0].get(prop);
ns.info(prop + ": " + ns.brush[prop]);
}
else if (action == "paint")
{
var new_value = ns.brush[prop];
if (ns.isnull(new_value)) ns.error("No property set.")
else
{
ns.debug("Painting " + new_value);
_.each(sel, function(obj, key)
{
ns.debug(obj);
obj.set(prop, new_value);
});
}
}
else if (action == "show")
{
// Show the current value of the given property
ns.info(prop + ": " + nvl(ns.brush[prop], "Not set."));
}
prop = words[w++];
}
}
}
}
});
// My function library
parm_batch.debug = function(s) { if (parm_batch.debugging) log(s); };
parm_batch.error = function(s) { sendChat(parm_batch.speak_as, parm_batch.whisper + "Error: " + s); };
parm_batch.info = function(s) { sendChat(parm_batch.speak_as, parm_batch.whisper + s); };
parm_batch.nvl = function(o, v) { return parm_batch.isnull(o) ? v : o; };
parm_batch.isnull = function(o) { return typeof o == 'undefined' || o == null || o.length == 0; };
parm_batch.replaceAll = function(s, a, b) { return s.split(a).join(b); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment