Copy link to clipboard
Copied
Need Help getting a list of duplicated smart objects Photoshop Script...
I am new to Javascript and Photoshop Scripting... I already have a script that reads all smart objects and opens each one and does some changes inside it... I have lots of duplicates of the same smart object layer... So whenever I run the script... It opens each duplicated smart object... But I don't want it that way... If there is any duplicated smart object layer I want to skip it... Is it possible...?
A version that gets an Array with each SO represented only once.
// determine duplicated smart objects;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var theThings = collectSmartObjectsWithIDs();
alert (theThings.length+" smart objects:\n"+theThings.join("\n"));
};
////////////////////////////////////
////// collect smart objects, probably based on code by paul, mike or x //////
function collectSmartObjectsWithIDs () {
// get number of layers;
var ref = new ActionReference(
...
Copy link to clipboard
Copied
In AM-code check the ID under smartObjectMore.
Copy link to clipboard
Copied
This gets you an array in which the ID of the SOs is given; same ID means instances of the same Smart Object.
By that property you can filter out the duplicates, either in the function or later from the array.
// determine duplicated smart objects;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var theThings = collectSmartObjectsWithIDs();
alert ("smart objects:\n"+theThings.join("\n"));
};
////////////////////////////////////
////// collect smart objects, probably based on code by paul, mike or x //////
function collectSmartObjectsWithIDs () {
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {
var soDesc = layerDesc.getObjectValue(stringIDToTypeID('smartObject'));
var soDescMore = layerDesc.getObjectValue(stringIDToTypeID('smartObjectMore'));
var theFileRef = soDesc.getString(stringIDToTypeID('fileReference'));
var theDocID = soDesc.getString(stringIDToTypeID('documentID'));
var soID = soDescMore.getString(stringIDToTypeID('ID'));
theLayers.push([theName, theID, soID, theFileRef, theDocID])
}
}
}
catch (e) {};
};
return theLayers
};
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// based on code by michael l hale //////
function checkDesc2 (theDesc, theAlert) {
var c = theDesc.count;
var str = '';
for(var i=0;i<c;i++){ //enumerate descriptor's keys
str = str + 'Key '+i+' = '+typeIDToStringID(theDesc.getKey(i))+': '+theDesc.getType(theDesc.getKey(i))+'\n'+getValues (theDesc, i)+'\n';
};
if (theAlert == true) {alert("desc\n\n"+str);
return};
return str
};
Copy link to clipboard
Copied
A version that gets an Array with each SO represented only once.
// determine duplicated smart objects;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var theThings = collectSmartObjectsWithIDs();
alert (theThings.length+" smart objects:\n"+theThings.join("\n"));
};
////////////////////////////////////
////// collect smart objects, probably based on code by paul, mike or x //////
function collectSmartObjectsWithIDs () {
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {
var soDesc = layerDesc.getObjectValue(stringIDToTypeID('smartObject'));
var soDescMore = layerDesc.getObjectValue(stringIDToTypeID('smartObjectMore'));
var theFileRef = soDesc.getString(stringIDToTypeID('fileReference'));
var theDocID = soDesc.getString(stringIDToTypeID('documentID'));
var soID = soDescMore.getString(stringIDToTypeID('ID'));
var theCheck = false;
for (var x = 0; x < theLayers.length; x++) {
if (theLayers[x][2] == soID) {theCheck = true}
};
if (theCheck == false) {theLayers.push([theName, theID, soID, theFileRef, theDocID])}
}
}
}
catch (e) {};
};
return theLayers
};
Copy link to clipboard
Copied
I appreciate your response and the additional information. It has been helpful to me. Despite spending several days searching for a solution, I haven't found one yet. Thank you once again.
Copy link to clipboard
Copied
What is the problem that needs a solution?
Edit: The code I posted provides an Array of Arrays that contain, amongst others, the IDs of one instance of each Smart Object, so one can select and open them based on that.
Copy link to clipboard
Copied
The Code is working perfect for me.