Copy link to clipboard
Copied
Hello,
Based on the following script found on the net (don't remember where exactly because I made lots of searches), is there a way to add more infos to the selected layers? I would like to know if a layer is a LayerSet or not, and also if it is "movable" (not locked and not background layer).
Please look at the "???" at the end of the script
function getSelectedLayersInfo() {
var lyrs = [];
var lyr;
var ref = new ActionReference();
var desc;
var tempIndex = 0;
var ref2;
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
ref.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
var targetLayers = executeActionGet(ref).getList(stringIDToTypeID("targetLayers"));
for (var i = 0; i < targetLayers.count; i++) {
ref2 = new ActionReference();
// if there's a background layer in the document, AM indices start with 1, without it from 0
try {
activeDocument.backgroundLayer;
ref2.putIndex(charIDToTypeID('Lyr '), targetLayers.getReference(i).getIndex());
desc = executeActionGet(ref2);
tempIndex = desc.getInteger(stringIDToTypeID("itemIndex")) - 1;
} catch (o) {
ref2.putIndex(charIDToTypeID('Lyr '), targetLayers.getReference(i).getIndex() + 1);
desc = executeActionGet(ref2);
tempIndex = desc.getInteger(stringIDToTypeID("itemIndex"));
}
lyr = {};
lyr.index = tempIndex;
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
lyr.name = desc.getString(charIDToTypeID("Nm "));
///// lyr.isLayerSet = ???????
///// lyr.isLocked = ???????
///// lyr.isBackground = ???????
lyrs.push(lyr);
}
return lyrs;
}
This script works fine on Photoshop CS6, but doesn't return enough informations about selected layers for my use.
Thank you.
lyr.isBackgrounLayer = desc.getBoolean(stringIDToTypeID('background'))
lyr.positionLocked = desc.getObjectValue(stringIDToTypeID('layerLocking')).getBoolean(stringIDToTypeID('protectPosition'))
lyr.typename = ({'true': 'LayerSet', 'false': 'ArtLayer'})[!(typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('layerSection'))).split('Content').length-1)]
Copy link to clipboard
Copied
I sometimes get TRUE, and sometimes FALSE... and results are right 🙂
Copy link to clipboard
Copied
That's right, from now on it should be okey since originally I made script for no background case. When I realised I missed it I updated the code, but then again I didn't notice yet there's no changed background value to boolean result, for which I had finally to edit my post of 😉
Copy link to clipboard
Copied
this thread has been both educational and helpful.
@Kukurykus when you say code is updated it sounds like it is posted somewhere.
True? if yes, could you please point to that location.
just this week I was thinking a Layer Info Report would be most useful to see what updated other people made in an [Invite to Edit]. More complete much simpler than clicking on all the layers and associated layer styles to reveal the updates.
thanks Roy
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Concerning the other posted scripts, they all seem to work with a passed layer reference... And I still don't know how to have a reference to a layer with my bloody original script 😞
By @frmorel
In the case of the script that I created, it is used to plan out a script, the code is just an inspector so that I know what is what before I try to "do something" in the script that I am creating.
The code assumes that the current targeted layer is the one that is being inspected.
var doc = app.activeDocument;
var docLayer = doc.activeLayer;
or
var actLay = app.activeDocument.activeLayer;
Copy link to clipboard
Copied
@Stephen Marsh: yes, I understand the use of your script.
Sorry if I was not very clear: my problem was how to have a layer reference (like the one you use with app.activeDocument.activeLayer) from this deep and weird code that look like it was created by the ScriptListener.
By the way, I realised the original script I use doesn't work if only one layer is active, due to this line:
var targetLayers = executeActionGet(ref).getList(stringIDToTypeID("targetLayers"));
Copy link to clipboard
Copied
With your help, and some script found on the net, I wrote two functions to "save" the actual state of selected layer(s), and restore the saved state. As I am very far to be a JSX or AM expert, these scripts could certainly be optimized. At least, they work with Photoshop CS6.
Usage sample:
var arrSavedLayersIDs = saveSelectedLayersIds();
//... perform your actions
restoreSelectedLayersIds(arrSavedLayersIDs);
function saveSelectedLayersIds() {
var cTID = charIDToTypeID; // shortcut
var sTID = stringIDToTypeID; // shortcut
var arrSavedLayersIDs = [];
var thisLayerDesc;
var ref = new ActionReference();
ref.putProperty(sTID("property"), sTID("targetLayers"));
ref.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt'));
if (executeActionGet(ref).count > 0) {
// ---------------------------------------------- several active layers
var layersDesc = executeActionGet(ref).getList(sTID("targetLayers"));
// -------------------------- selected layers loop
var ref2;
for (var i = 0; i < layersDesc.count; i++) {
ref2 = new ActionReference();
// if there's a background layer in the document, AM indices start with 1, without it from 0
try {
activeDocument.backgroundLayer;
ref2.putIndex(cTID('Lyr '), layersDesc.getReference(i).getIndex());
} catch (e) {
ref2.putIndex(cTID('Lyr '), layersDesc.getReference(i).getIndex() + 1);
}
thisLayerDesc = executeActionGet(ref2);
arrSavedLayersIDs.push(thisLayerDesc.getInteger(sTID("layerID")));
}
}else{
// ---------------------------------------------- only one layer active
ref = new ActionReference();
ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt')); // reference is active layer
thisLayerDesc = executeActionGet(ref);
arrSavedLayersIDs.push(thisLayerDesc.getInteger(sTID("layerID")));
}
return arrSavedLayersIDs;
}
function restoreSelectedLayersIds(arrSavedLayersIDs) {
if (Object.prototype.toString.call(arrSavedLayersIDs) === '[object Array]') {
selectLayerByID(arrSavedLayersIDs[0], false);
for (i = 1; i < arrSavedLayersIDs.length; i++) {
selectLayerByID(arrSavedLayersIDs[i], true);
}
return '';
}else{
return "ERROR: arrSavedLayersIDs is not an array.";
}
}
function selectLayerByID(idLayer, addToSelection) {
var addToSelection = addToSelection || false;
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putIdentifier(charIDToTypeID('Lyr '), idLayer);
desc1.putReference(charIDToTypeID('null'), ref1);
if (addToSelection) desc1.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
try {
executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
return true;
} catch (e) {
return false;
}
}