Hey simene49649793, these 2 functions should get you half way there.
getDocumentInfo() - Retrieves the document information. Super cool function that I found in one of these forums. This is similar to Generator's getDocumentInfo. It has some optional params to retrieve more specific data.
This AM function will return the document object as a string, because ExtendScript is very limited unfortunately. You will have to parse it using a JSON polyfill. I use this one: https://github.com/JavierAroche/descriptor-info/blob/master/example/helpers/JSON.jsx
You can then grab the document.layers key which includes all the layers in the document.
function getDocumentInfo() {
// Build Action Descriptor
var ref = new ActionReference();
var desc = new ActionDescriptor();
var psJSON = stringIDToTypeID("json");
ref.putProperty(charIDToTypeID('Prpr'), psJSON);
ref.putEnumerated(stringIDToTypeID("document"), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc.putReference(charIDToTypeID('null'), ref);
// desc.putBoolean(stringIDToTypeID("expandSmartObjects"), true);
// desc.putBoolean(stringIDToTypeID("selectedLayers"), true);
// desc.putBoolean(stringIDToTypeID("getTextStyles"), true);
// desc.putBoolean(stringIDToTypeID("getFullTextStyles"), true);
// desc.putBoolean(stringIDToTypeID("getDefaultLayerFX"), true);
// desc.putBoolean(stringIDToTypeID("getPathData"), true);
return executeAction(charIDToTypeID( "getd" ), desc, DialogModes.NO).getString(psJSON);
}
getSelectedLayerIDs() - Retrieves the selected layer IDs as an array.
function getSelectedLayerIDs() {
var selectedLayerIDs = [];
// Filter selected layers
var docRef = new ActionReference();
docRef.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var docDesc = executeActionGet(docRef);
// Get target layer ids
if( docDesc.hasKey(stringIDToTypeID('targetLayersIDs'))) {
targetLayersIDsDesc = docDesc.getList( stringIDToTypeID('targetLayersIDs'));
for(var ii = 0; ii < targetLayersIDsDesc.count; ii++) {
selectedLayerIDs.push(Number(targetLayersIDsDesc.getReference( ii ).getIdentifier()));
}
}
return selectedLayerIDs;
}
Once you have the document layers as an object and the selected layer IDs, you can simply iterate through all the layers (recursive function) until you find the one you're looking for. If you're only targeting one group layer, it will be a lot easier.
- Is it possible to retrieve the parent of a specific layer? |
This one is tricky, but if you're comfortable with recursive functions, you should be able to get the parent layer of the any layer in a document.
collapseAllGroups() - Collapses all document groups.
function collapseAllGroups() {
try {
var idcollapseAllGroupsEvent = stringIDToTypeID( "collapseAllGroupsEvent" );
var desc13 = new ActionDescriptor();
return executeAction( idcollapseAllGroupsEvent, desc13, DialogModes.NO );
} catch(err) {}
}
How do you guys attack these kinds of challenges? I have googled and searched github for these questions, but I haven't found anything of help. I feel like there's just a few of you that knows AM code, how did you plow your way into learning AM coding? I mean, the basic operations that can be recorded by the listener is doable even for me, but approaching challenges like the one's I have now, I don't even know where to start? |
It takes a lot of practice, patience, googling and spending lots of hours in forums
DBarranca is authoring a Photoshop Scripting book. That will be great for people trying to learn all of this.
http://www.davidebarranca.com/2016/10/new-course-photoshop-scripting/
Some resources that have helped me a lot:
I made this little module to better understand Action Descriptors.
https://github.com/JavierAroche/descriptor-info
Hope it helps!