having problems with a little javascript. Pixate is UI design tool that allows you to build custom actions in JS. I am trying to build a little JS action with a few lines of code, but obviously i am overseeing something evident (sorry, i am not a real coder). Here is my script so far:
var layers = getSelectedLayers();
var erg = "";
for (var elem in layers) {
erg += layers[elem] + ", ";
}
alert(erg);
var group = createLayer("MyGroup");
group.y = group.x = 0;
group.width = Screen.width;;
group.height = Screen.height;;
group.backgroundColor = 'transparent';
nestLayers(group, erg);
The commands "getSelectedLayers" "createLayers" ans "nestLayers" are offered by Pixate. If i try to run my code, the "getSelectedLayers" seems to be working (the alert function gives back an array of the selected layers). Creating the new layer works also. The problem is, that after starting the "nestLayers" function, after nesting the first selected layer the script stops with an error on my last line "undefined is not a function"...
Any help is greatly appreciated – Thanks!
There are two issues with your nestLayers
call: it expects all the layers as parameters (not an array), and you were passing an array of strings, not layer objects (see the docs here: http://www.pixate.com/docs/actions/#nestlayer).
The correct action should look like this:
var layers = getSelectedLayers();
var group = createLayer("MyGroup");
group.y = group.x = 0;
group.width = Screen.width;
group.height = Screen.height;
group.backgroundColor = 'transparent';
nestLayers.apply(this, [].concat(group, layers));
apply
calls a function with the supplied array as parameters (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply), and I'm just concatenating the group layer and the rest of the layers into a single array.