Lazy JavaScript function needs to be run several times for simple layer op

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: xbytor, DavideBarranca, Paul MR

Post Reply
plasmax
Posts: 1
Joined: Wed Mar 01, 2017 6:06 pm

Lazy JavaScript function needs to be run several times for simple layer op

Post by plasmax » Wed Mar 01, 2017 7:36 pm

I'm trying to create a reusable javascript function to organise layers within a selected group. So far so good, but a couple of things are puzzling me:

1) I seem to have to name my function parameter 'myArray' in order to pass it correctly. Is this a JavaScript thing? I'm from Python.
2) I have to run the script multiple times to achieve the desired effect. Could something be blocking it in the DOM/Action Manager?
3) It's incredibly slow. I have been told to expect this, but there must be ways to optimise - creating an array of layer objects and then using them is even worse.

Code: Select all


var myArray = ['abc','def','ghi'];
organiseSelGrpSubLayersByName(myArray);

function organiseSelGrpSubLayersByName(myArrray)
{
var doc = app.activeDocument;
var grp = doc.activeLayer;
var lyrs = grp.artLayers;
var sets = grp.layerSets;
for (var item = 0; item < myArray.length; item++)
{
var subGrp = grp.layerSets.getByName(myArray[item]);
if (subGrp === null)
{
var subGrp = grp.layerSets.add();
subGrp.name = myArray[item];
}
for (var i = 0; i < lyrs.length; i++)
{
var myLayer = lyrs[i];
if ((myLayer.name).indexOf(myArray[item]) != -1)
{
myLayer.move(subGrp, ElementPlacement.INSIDE);
}
}
}
return; // Only in here to prevent annoying 'Result: undefined' message.
}

pixxxelschubser
Posts: 26
Joined: Mon Aug 01, 2016 8:59 pm

Re: Lazy JavaScript function needs to be run several times for simple layer op

Post by pixxxelschubser » Sat Mar 04, 2017 12:58 pm

Your script fails if active layer is not a layer set.
Your script fails if layer set "myArray[item]" not exists.
Your script fails if active layer was moved in his new group. (The order is wrong now)

Try this for the beginning:

Code: Select all

//var myArray = ['abc','def','ghi'];
var myArray = ['1','2','3'];
organiseSelGrpSubLayersByName(myArray);

function organiseSelGrpSubLayersByName(myArrray)
{
var doc = app.activeDocument;
var grp = doc.activeLayer;
if (grp.typename !='LayerSet') { alert("active layer is not a layer set"); return; } // check active layer
var lyrs = grp.artLayers;
var sets = grp.layerSets;
for (var item = 0; item < myArray.length; item++)
{
try // better check if objekt exists
{
var subGrp = grp.layerSets.getByName(myArray[item]);
subGrp.name = myArray[item];
}
catch (e)
{
var subGrp = grp.layerSets.add();
subGrp.name = myArray[item];
}
for (var i = lyrs.length-1; i >=0 ; i--) // loop backwards
{
var myLayer = lyrs[i];
if ((myLayer.name).indexOf(myArray[item]) != -1)
{
myLayer.move(subGrp, ElementPlacement.INSIDE);
}
}
}
return;
}
Perhaps it helps a little.
:)

Post Reply