Get Layer Type generates error if doc has background only

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: Tom, Kukurykus

bdeshazer

Get Layer Type generates error if doc has background only

Post by bdeshazer »

(Last question of the day, I promise!)

Ok, I lifted this code:

Code: Select all       var ref = new ActionReference();
       ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID("layerSection"));
       ref.putIndex(charIDToTypeID( "Lyr " ), idx);
        return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID('layerSection')));


from Mike Hale and this thread:

bb/viewtopic.php?p=7089

a while back, and it's been working fine for what I've used it for.

However, just today I noticed that if you run this code (setting the 'idx' variable to '0') on a document that only has a background layer I get the following error:

'The object “ <unknown> of layer 0” is not currently available.'

Adding an additional layer of any kind allows the script to run without generating this error, whether the Background layer or the other layer is selected and the appropriate value is set for the idx variable. Likewise, converting the Background layer of a single-layer document to a 'regular' layer also prevents this error from occurring.

I suppose the answer is to just test for this special case (single layer, active layer index=0) and modify the code behavior accordingly as needed? Any other suggestions for dealing with this?

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Paul MR

Get Layer Type generates error if doc has background only

Post by Paul MR »

Interesting, a check might be useful...

Code: Select allalert(getlayerSection(0));
function getlayerSection(idx){
var ref = new ActionReference();
if(!activeDocument.activeLayer.isBackgroundLayer){
ref.putIndex(charIDToTypeID( "Lyr " ), idx);
}else{
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
}
var desc = executeActionGet(ref);
return typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('layerSection')));
}
bdeshazer

Get Layer Type generates error if doc has background only

Post by bdeshazer »

Thanks Paul, probably a more elegant solution than the check that I came up with, but I did manage to work around it (numlayers = 0 and document.hasBackgroundLayer = true then do manual workaround...)

Once I finish my current project in the next day or so I'll test your code out and see if I can figure out how it works.