Iterating through all layers extremely slow

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

simerus
Posts: 5
Joined: Thu Dec 15, 2016 7:51 pm

Iterating through all layers extremely slow

Post by simerus »

I've attached a snipped that changes antialising on all text layers to "Smooth", on both hidden and unhidden layers.

The problem is that this is extreeemely slow, so I'm wondering if there is any way to make this faster? Also, is there an easy way to be able to see a progressbar when running the function?

Code: Select all

// FUNCTION: APPLY FONT CHANGES
function fnChangeAntiAlias(target)
{
// Get the layers of the active document
var docLayers = target.layers;

// For each layer in the document
for (var i = 0; i < docLayers.length; i++)
{
// If the current layer is a LayerSet
if (docLayers[i].typename == "LayerSet")
{
// Recursive: Re-run function with the current LayerSet as target
fnChangeAntiAlias(docLayers[i]);
}

// Else if the current layer is a text layer
else if (docLayers[i].kind == LayerKind.TEXT)
{
docLayers[i].textItem.antiAliasMethod = AntiAlias.SMOOTH];
};
};
};
// Get the active document
var doc = app.activeDocument;
// Execute the function
fnChangeAntiAlias(doc);
xbytor
Posts: 22
Joined: Thu Jan 01, 1970 12:00 am

Re: Iterating through all layers extremely slow

Post by xbytor »

The simplest fix (assuming you have a lot of layers) is a change like this:

Code: Select all

 // For each layer in the document
for (var i = 0; i < docLayers.length; i++)
{
to this:

Code: Select all

 // For each layer in the document
var len = docLayers.length;
for (var i = 0; i < len ; i++)
{
If that doesn't work, you'll need to descend into ActionManager code.
simerus
Posts: 5
Joined: Thu Dec 15, 2016 7:51 pm

Re: Iterating through all layers extremely slow

Post by simerus »

Thanks for replying xbytor. I haven't tried, but I don't understand how that will increase the speed? The only difference is that you pass the number of layers into a variable before starting the loop.

Regarding ActionManager code, what is that? Could you point me in a direction for learning about that?
pixxxelschubser
Posts: 26
Joined: Mon Aug 01, 2016 8:59 pm

Re: Iterating through all layers extremely slow

Post by pixxxelschubser »

The solution is here in the crossposting
https://forums.adobe.com/thread/2252274
simerus
Posts: 5
Joined: Thu Dec 15, 2016 7:51 pm

Re: Iterating through all layers extremely slow

Post by simerus »

Yeah I posted it there yesterday as the forums showed up when I was googling for ActionScript Code, still haven't found a good reference for learning ActionScript though :?: