Put selected layers to array
-
csuebele
Put selected layers to array
How would you put several layers that were selected in PS into an array so that you could go back and preform an operation on each of the selected layers?
-
bdeshazer
Put selected layers to array
csuebele wrote:How would you put several layers that were selected in PS into an array so that you could go back and preform an operation on each of the selected layers?
Looping through the selected layers and pushing the layer ID from the layer descriptor would be how I would do it.
Looping through the selected layers and pushing the layer ID from the layer descriptor would be how I would do it.
-
Mike Hale
Put selected layers to array
It depends on which version of Photoshop you have and if you are willing to use Action Manager code.
Newer versions of Photoshop has a key ( 'targetLayers' ) in the document descriptor for selected layers. That is an actionList of layer indexes.
Newer versions of Photoshop has a key ( 'targetLayers' ) in the document descriptor for selected layers. That is an actionList of layer indexes.
-
csuebele
Put selected layers to array
bdeshazer wrote:csuebele wrote:How would you put several layers that were selected in PS into an array so that you could go back and preform an operation on each of the selected layers?
Looping through the selected layers and pushing the layer ID from the layer descriptor would be how I would do it.
That's what I was thinking of doing, but I'm not sure how to specify which layers were selected.
Mike Hale wrote:It depends on which version of Photoshop you have and if you are willing to use Action Manager code.
Newer versions of Photoshop has a key ( 'targetLayers' ) in the document descriptor for selected layers. That is an actionList of layer indexes.
Mike, I'm still not very good about using Action Manager code. How would you go about that?
Looping through the selected layers and pushing the layer ID from the layer descriptor would be how I would do it.
That's what I was thinking of doing, but I'm not sure how to specify which layers were selected.
Mike Hale wrote:It depends on which version of Photoshop you have and if you are willing to use Action Manager code.
Newer versions of Photoshop has a key ( 'targetLayers' ) in the document descriptor for selected layers. That is an actionList of layer indexes.
Mike, I'm still not very good about using Action Manager code. How would you go about that?
-
Mike Hale
Put selected layers to array
Here are some useful functions. I haven't found a good way to relate AM layer info to a Photoshop Object Model layer object( other than making the layer the activeLayer ).
Code: Select allfunction getSelectedLayersID(){
function hasBackground(){
var res = undefined;
try{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm " ));
ref.putIndex( charIDToTypeID( "Lyr " ), 0 );
executeActionGet(ref).getString(charIDToTypeID( "Nm " ));;
res = true;
}catch(e){ res = false}
return res;
}
function getLayerIDByItemindex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
var res = executeActionGet(ref).getInteger(charIDToTypeID( "LyrI" ));
return res;
}
var selectedLayers = new Array();
var bckg = hasBackground();
var end = Number(bckg);
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count -1;
for(var i = c; i >= 0 ; i--){
var res = (desc.getReference( i ).getIndex()) ;
selectedLayers.push( getLayerIDByItemindex(res)-end );
}
}else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
selectedLayers.push(( executeActionGet(ref).getInteger(charIDToTypeID( "LyrI" )) )- end);
}
return selectedLayers;
}
getSelectedLayersID();
function getLayerNameByLayerID(id) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm " ));
ref.putIdentifier( charIDToTypeID( "Lyr " ), id );
return executeActionGet(ref).getString(charIDToTypeID( "Nm " ));;
};
function makeLayerActiveByLayerID(id){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier( charIDToTypeID( "Lyr " ), id );
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};
Code: Select allfunction getSelectedLayersID(){
function hasBackground(){
var res = undefined;
try{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm " ));
ref.putIndex( charIDToTypeID( "Lyr " ), 0 );
executeActionGet(ref).getString(charIDToTypeID( "Nm " ));;
res = true;
}catch(e){ res = false}
return res;
}
function getLayerIDByItemindex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
var res = executeActionGet(ref).getInteger(charIDToTypeID( "LyrI" ));
return res;
}
var selectedLayers = new Array();
var bckg = hasBackground();
var end = Number(bckg);
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count -1;
for(var i = c; i >= 0 ; i--){
var res = (desc.getReference( i ).getIndex()) ;
selectedLayers.push( getLayerIDByItemindex(res)-end );
}
}else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
selectedLayers.push(( executeActionGet(ref).getInteger(charIDToTypeID( "LyrI" )) )- end);
}
return selectedLayers;
}
getSelectedLayersID();
function getLayerNameByLayerID(id) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm " ));
ref.putIdentifier( charIDToTypeID( "Lyr " ), id );
return executeActionGet(ref).getString(charIDToTypeID( "Nm " ));;
};
function makeLayerActiveByLayerID(id){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier( charIDToTypeID( "Lyr " ), id );
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};
-
bdeshazer
Put selected layers to array
Mike, was this "getSelectedLayersID()" function adapted from one that gets layer indexes? As-is I don't think it's getting the correct ID's for the selected layers because of the subtraction (...-end) that you're doing which AFAIK you don't need or want to do with layer IDs...
Pretty sure this line:
Code: Select allselectedLayers.push( getLayerIDByItemindex(res)-end );
needs to be changed to:
Code: Select allselectedLayers.push( getLayerIDByItemindex(res) );
Brent
Pretty sure this line:
Code: Select allselectedLayers.push( getLayerIDByItemindex(res)-end );
needs to be changed to:
Code: Select allselectedLayers.push( getLayerIDByItemindex(res) );
Brent
-
Mike Hale
Put selected layers to array
It could be that Adobe made some change in newer versions. I think that background layer adjustment was there because at one time the AM index of a layer depended on whether or not the document has a background layer.
-
bdeshazer
Put selected layers to array
Mike Hale wrote:It could be that Adobe made some change in newer versions. I think that background layer adjustment was there because at one time the AM index of a layer depended on whether or not the document has a background layer.
For indexes you are correct, you have to go through the "if background then -1" gymnastics when dealing with layer indexes, at least with CS4-CS6 which is what I'm currently developing for.
However, the code you posted is dealing with LayerIDs, with the "res" variable holding the layer index.... this appears to be getting the layer ID using the index "res", and then subtracting 1 from the returned ID. As far as I know, accessing layers via ID does not need the same subtraction gymnastics that layer indexes require...
When using ref.putIndex you have to use subtract one from the "real" layer index to access the correct layer, but when using ref.putIdentifier you have to use the layers actual ID to properly reference the correct layer.
I think just altering that one line I posted earlier to not do the -end subtraction makes the script correctly return the layer ID's for the selected layers...
For indexes you are correct, you have to go through the "if background then -1" gymnastics when dealing with layer indexes, at least with CS4-CS6 which is what I'm currently developing for.
However, the code you posted is dealing with LayerIDs, with the "res" variable holding the layer index.... this appears to be getting the layer ID using the index "res", and then subtracting 1 from the returned ID. As far as I know, accessing layers via ID does not need the same subtraction gymnastics that layer indexes require...
When using ref.putIndex you have to use subtract one from the "real" layer index to access the correct layer, but when using ref.putIdentifier you have to use the layers actual ID to properly reference the correct layer.
I think just altering that one line I posted earlier to not do the -end subtraction makes the script correctly return the layer ID's for the selected layers...