Skip to main content
jonathankemp
Inspiring
June 3, 2020
Answered

Scripting : get number of layers with Action Manager. : Is this a bug ?

  • June 3, 2020
  • 3 replies
  • 1268 views

So... consider the following function.

 

function getNumberOfLayers() {
// For debugging purposes
  var start = new Date().getTime();

  var ref4 = new ActionReference();
		ref4.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
	var docDesc = executeActionGet(ref4);

// For debugging purposes
  var end = new Date().getTime();
  alert(end-start);

	return docDesc.getInteger(stringIDToTypeID("numberOfLayers"));
}

 

This returns the number of layers in a document.   Notice I added a few debugging lines to get the time it takes to execute.

 

When the function is called while a layer's RGB channel is selected (the "ArtLayer"), it takes much more time to execute, than when the layer's mask is selected.  It really depends on the document, but I've seen numbers like this : 

 

Time to execute (in milliseconds) : 

- With the layer's RGB channel selected : 250ms

- With the layer's mask selected : 10ms 

 

Of course, 250ms isn't great at all, and it makes a noticeable difference... but I'm wondering what might be going on here... it just doesn't make sense to me.

 

Also... I had trouble with another similar function (a function detecting if there was a background layer or not), and stumbled on this page.  It looks like accessing the "Document" through action manager code might not be that efficient... 

 

So... 

 

1 - Anyone has any clue about what is going on ?

 

2- Anyone has a better, faster way to get the number of layers in a document ?

 

Many, many thanks folks.

 

J

This topic has been closed for replies.
Correct answer Kukurykus

Change 5th line to:

 

(ref4 = new ActionReference()).putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'))

 

numberOfLayers / for backround layer use: 'hasBackgroundLayer'

3 replies

Legend
June 3, 2020

It is better to take the layer property "count".
The document property "numberOfLayers" does not take into account the Background layer.

jonathankemp
Inspiring
June 3, 2020

Did the trick.

 

Am I understanding this right ?  I was retrieving the whole "Document", which took more time, and now we are only retrieving the property we are actually looking for, which is "numberOfLayers".  Is that it ?

 

Also... any clue about why it wasn't behaving the same way with the RGB or the mask selected ?

Kukurykus
Legend
June 3, 2020

"Obtaining the value takes a noticeable time - this is due to the fact that we first get the entire handle of the object, and then select one property from it. This can be slightly optimized if, on the way to the object, you immediately indicate which one we need. Now the code works almost instantly."

 

That's said by jazz-y, who I teached it, while I learnt it from Jarda_Bereza. Look for 'hasBackgroundLayer' and 'numberOfLayers' in a thread code I linked you to in Action Manager Scripting

 

Regarding RGB / Mask, post a screenshot of your Layers panel.

Kukurykus
KukurykusCorrect answer
Legend
June 3, 2020

Change 5th line to:

 

(ref4 = new ActionReference()).putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'))

 

numberOfLayers / for backround layer use: 'hasBackgroundLayer'