• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
5

Need Help for getting list of duplicated smart objects Photoshop Script...

Community Beginner ,
Feb 28, 2024 Feb 28, 2024

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...?

 

 

TOPICS
Actions and scripting , Windows

Views

167

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 02, 2024 Mar 02, 2024

A version that gets an Array with each SO represented only once. 

Screenshot 2024-03-02 at 15.09.59.png

// 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(
...

Votes

Translate

Translate
Adobe
Community Expert ,
Feb 28, 2024 Feb 28, 2024

Copy link to clipboard

Copied

In AM-code check the ID under smartObjectMore

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2024 Mar 01, 2024

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. 

Screenshot 2024-03-01 at 12.55.22.png

 

// 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
};

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2024 Mar 02, 2024

Copy link to clipboard

Copied

A version that gets an Array with each SO represented only once. 

Screenshot 2024-03-02 at 15.09.59.png

// 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
};

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2024 Mar 04, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2024 Mar 04, 2024

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

LATEST

The Code is working perfect for me.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines