Skip to main content
Participant
September 19, 2023
Répondu

is there a way to use "new smart object via copy" in bulk?

as title suggest id like to use new smart object via copy but copy a lot of them at the same time. please let me know!

Ce sujet a été fermé aux réponses.
Meilleure réponse par Stephen Marsh

Here is an updated 1.1 version of my previous script which will alert on every layer that is linked (one by one, I couldn't be bothered logging them all at the end in a single alert or log file).

 

/*
Selected Layers to New Smart Object via Copy.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-there-a-way-to-use-quot-new-smart-object-via-copy-quot-in-bulk/td-p/14096358
v1.1, 20th September 2023, Stephen Marsh
*/

#target photoshop

function main() {

    // Save the current dialog display settings
    var savedDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var i = 0; i < lrs.count; i++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        // Linked SO conditional check courtesy of r-bin
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var d = executeActionGet(r);
        if (d.hasKey(stringIDToTypeID("smartObject"))) {
            d = d.getObjectValue(stringIDToTypeID("smartObject"));
            if (d.hasKey(stringIDToTypeID("link"))) {
                alert('The smart object layer "' + activeDocument.activeLayer.name + '" is linked, not embedded!');
            } else {
                var embeddedSOlayer = activeDocument.activeLayer.name;
                // Call the new smart object via copy function: courtesy fo c.pfaffenbichler
                newSmartObjectViaCopy();
                //alert('The embedded smart object layer "' + embeddedSOlayer + '" has been copied!');
            }
        }
    }
    // Finish the loop

    // Restore the dialogs
    app.displayDialogs = savedDisplayDialogs;
}


function newSmartObjectViaCopy() {
    try {
        var idplacedLayerMakeCopy = stringIDToTypeID("placedLayerMakeCopy");
        executeAction(idplacedLayerMakeCopy, undefined, DialogModes.NO);
        return app.activeDocument.activeLayer
    } catch (e) {
        alert(e)
    }
}

// Single history stage undo
activeDocument.suspendHistory("Selected Layers to New Smart Object via Copy.jsx", "main()");

 

 

EDIT: This version logs the linked layers that were skipped to a text file on the desktop, rather than alerting each layer:

 

/*
Selected Layers to New Smart Object via Copy.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-there-a-way-to-use-quot-new-smart-object-via-copy-quot-in-bulk/td-p/14096358
v1.2, 27th December 2023, Stephen Marsh
*/

#target photoshop

function main() {

    // Save the current dialog display settings
    var savedDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Log file setup
    var logFile = new File("~/Desktop/Selected Layers to New Smart Object via Copy LOG.txt");
    if (logFile.exists);
    logFile.remove();
    var os = $.os.toLowerCase().indexOf("mac") >= 0 ? "mac" : "windows";
    if (os === "mac") {
        logFileLF = "Unix";
    } else {
        logFileLF = "Windows";
    }
    var dateTime = new Date().toLocaleString();
    logFile.open("w");
    logFile.encoding = "UTF-8";
    logFile.lineFeed = logFileLF;
    logFile.write(dateTime + "\r" + "Document name: " + activeDocument.name + "\r" + "The following layers are linked and therefore skipped:" + "\r\r");
    logFile.close();

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var i = 0; i < lrs.count; i++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        // Linked SO conditional check courtesy of r-bin
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var d = executeActionGet(r);
        if (d.hasKey(stringIDToTypeID("smartObject"))) {
            d = d.getObjectValue(stringIDToTypeID("smartObject"));
            if (d.hasKey(stringIDToTypeID("link"))) {
                // Log the linked layers which were skipped
                writeToLog();
            } else {
                var embeddedSOlayer = activeDocument.activeLayer.name;
                // Call the new smart object via copy function: courtesy fo c.pfaffenbichler
                newSmartObjectViaCopy();
            }
        }
    }
    // Finish the loop

    // Restore the dialogs
    app.displayDialogs = savedDisplayDialogs;

    // Open the log file
    logFile.execute();


    ///// FUNCTIONS /////

    function writeToLog() {
        logFile.open("a");
        logFile.encoding = "UTF-8";
        logFile.lineFeed = logFileLF;
        logFile.write(activeDocument.activeLayer.name + "\r");
        logFile.close();
    }

    function newSmartObjectViaCopy() {
        try {
            var idplacedLayerMakeCopy = stringIDToTypeID("placedLayerMakeCopy");
            executeAction(idplacedLayerMakeCopy, undefined, DialogModes.NO);
            return app.activeDocument.activeLayer;
        } catch (e) {
            alert(e);
        }
    }

}

// Single history stage undo
activeDocument.suspendHistory("Selected Layers to New Smart Object via Copy.jsx", "main()");

 

 

6 commentaires

Stephen Marsh
Community Expert
Community Expert
September 20, 2023

Here is an updated 1.1 version of my previous script which will alert on every layer that is linked (one by one, I couldn't be bothered logging them all at the end in a single alert or log file).

 

/*
Selected Layers to New Smart Object via Copy.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-there-a-way-to-use-quot-new-smart-object-via-copy-quot-in-bulk/td-p/14096358
v1.1, 20th September 2023, Stephen Marsh
*/

#target photoshop

function main() {

    // Save the current dialog display settings
    var savedDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var i = 0; i < lrs.count; i++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        // Linked SO conditional check courtesy of r-bin
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var d = executeActionGet(r);
        if (d.hasKey(stringIDToTypeID("smartObject"))) {
            d = d.getObjectValue(stringIDToTypeID("smartObject"));
            if (d.hasKey(stringIDToTypeID("link"))) {
                alert('The smart object layer "' + activeDocument.activeLayer.name + '" is linked, not embedded!');
            } else {
                var embeddedSOlayer = activeDocument.activeLayer.name;
                // Call the new smart object via copy function: courtesy fo c.pfaffenbichler
                newSmartObjectViaCopy();
                //alert('The embedded smart object layer "' + embeddedSOlayer + '" has been copied!');
            }
        }
    }
    // Finish the loop

    // Restore the dialogs
    app.displayDialogs = savedDisplayDialogs;
}


function newSmartObjectViaCopy() {
    try {
        var idplacedLayerMakeCopy = stringIDToTypeID("placedLayerMakeCopy");
        executeAction(idplacedLayerMakeCopy, undefined, DialogModes.NO);
        return app.activeDocument.activeLayer
    } catch (e) {
        alert(e)
    }
}

// Single history stage undo
activeDocument.suspendHistory("Selected Layers to New Smart Object via Copy.jsx", "main()");

 

 

EDIT: This version logs the linked layers that were skipped to a text file on the desktop, rather than alerting each layer:

 

/*
Selected Layers to New Smart Object via Copy.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-there-a-way-to-use-quot-new-smart-object-via-copy-quot-in-bulk/td-p/14096358
v1.2, 27th December 2023, Stephen Marsh
*/

#target photoshop

function main() {

    // Save the current dialog display settings
    var savedDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Log file setup
    var logFile = new File("~/Desktop/Selected Layers to New Smart Object via Copy LOG.txt");
    if (logFile.exists);
    logFile.remove();
    var os = $.os.toLowerCase().indexOf("mac") >= 0 ? "mac" : "windows";
    if (os === "mac") {
        logFileLF = "Unix";
    } else {
        logFileLF = "Windows";
    }
    var dateTime = new Date().toLocaleString();
    logFile.open("w");
    logFile.encoding = "UTF-8";
    logFile.lineFeed = logFileLF;
    logFile.write(dateTime + "\r" + "Document name: " + activeDocument.name + "\r" + "The following layers are linked and therefore skipped:" + "\r\r");
    logFile.close();

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var i = 0; i < lrs.count; i++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        // Linked SO conditional check courtesy of r-bin
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var d = executeActionGet(r);
        if (d.hasKey(stringIDToTypeID("smartObject"))) {
            d = d.getObjectValue(stringIDToTypeID("smartObject"));
            if (d.hasKey(stringIDToTypeID("link"))) {
                // Log the linked layers which were skipped
                writeToLog();
            } else {
                var embeddedSOlayer = activeDocument.activeLayer.name;
                // Call the new smart object via copy function: courtesy fo c.pfaffenbichler
                newSmartObjectViaCopy();
            }
        }
    }
    // Finish the loop

    // Restore the dialogs
    app.displayDialogs = savedDisplayDialogs;

    // Open the log file
    logFile.execute();


    ///// FUNCTIONS /////

    function writeToLog() {
        logFile.open("a");
        logFile.encoding = "UTF-8";
        logFile.lineFeed = logFileLF;
        logFile.write(activeDocument.activeLayer.name + "\r");
        logFile.close();
    }

    function newSmartObjectViaCopy() {
        try {
            var idplacedLayerMakeCopy = stringIDToTypeID("placedLayerMakeCopy");
            executeAction(idplacedLayerMakeCopy, undefined, DialogModes.NO);
            return app.activeDocument.activeLayer;
        } catch (e) {
            alert(e);
        }
    }

}

// Single history stage undo
activeDocument.suspendHistory("Selected Layers to New Smart Object via Copy.jsx", "main()");

 

 

c.pfaffenbichler
Community Expert
Community Expert
September 20, 2023

Very industrious! 

 

Now it would be interesting to know what the original poster actually intended. 

Stephen Marsh
Community Expert
Community Expert
September 20, 2023
quote

Very industrious! 

 

Now it would be interesting to know what the original poster actually intended. 


By @c.pfaffenbichler


I had fun anyway!

Stephen Marsh
Community Expert
Community Expert
September 20, 2023

Just in case this is useful to somebody... The following script will only process one or more selected smart object layers (as opposed to every smart object layer).

 

/*
Selected Layers to New Smart Object via Copy.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-there-a-way-to-use-quot-new-smart-object-via-copy-quot-in-bulk/td-p/14096358
v1.0, 20th September 2023, Stephen Marsh
*/

#target photoshop

function main() {

    // Save the current dialog display settings
    var savedDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var i = 0; i < lrs.count; i++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        if (activeDocument.activeLayer.typename === "ArtLayer" && activeDocument.activeLayer.kind == "LayerKind.SMARTOBJECT") {
            // Call the new smart object via copy function: courtesy fo c.pfaffenbichler
            newSmartObjectViaCopy();
        }
    }
    // Finish the loop

    // Restore the dialogs
    app.displayDialogs = savedDisplayDialogs;
}


function newSmartObjectViaCopy() {
    try {
        var idplacedLayerMakeCopy = stringIDToTypeID("placedLayerMakeCopy");
        executeAction(idplacedLayerMakeCopy, undefined, DialogModes.NO);
        return app.activeDocument.activeLayer
    } catch (e) {
        alert(e)
    }
}

// Single history stage undo
activeDocument.suspendHistory("Selected Layers to New Smart Object via Copy.jsx", "main()");
c.pfaffenbichler
Community Expert
Community Expert
September 20, 2023

I forgot to filter out Linked Smart Objects anyway … 

Stephen Marsh
Community Expert
Community Expert
September 20, 2023
quote

I forgot to filter out Linked Smart Objects anyway … 


By @c.pfaffenbichler

 

Yeah, I was only performing basic testing for SO in general, not differentiating between linked vs. embedded. Apart from the exercise, should I try to do better, will it matter in practice?

Legend
September 19, 2023

del

c.pfaffenbichler
Community Expert
Community Expert
September 19, 2023

Layer > Smart Objects > New Smart Object Via Copy

creates a new Smart Object, a copy of the selected Smart Object, that can then be edited independently from the original Smart Object. 

c.pfaffenbichler
Community Expert
Community Expert
September 19, 2023
// new smart object via copy for all smart objects;
// 2023, use it at your own risk;
if (app.documents.length > 0) {
var theSmartObjects = collectSmartObjects2017();
for (var m = 0; m < theSmartObjects.length; m++) {
selectLayerByID(theSmartObjects[m][1], false);
newSmartObjectViaCopy ()
}
};
////////////////////////////////////
////// collect smart objects, probably based on code by paul, mike or x //////
function collectSmartObjects2017 () {
// the file;
var myDocument = app.activeDocument;
// 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'))) {theLayers.push([theName, theID])}
};
}
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); 
}
};
////// new so via copy //////
function newSmartObjectViaCopy () {
// ======================================================= new so via copy;
try {
var idplacedLayerMakeCopy = stringIDToTypeID( "placedLayerMakeCopy" );
executeAction( idplacedLayerMakeCopy, undefined, DialogModes.NO );
return app.activeDocument.activeLayer
} catch (e) {alert (e)}
};
c.pfaffenbichler
Community Expert
Community Expert
September 19, 2023

Please post meaningful screenshots including the pertinent Panels and explain what you actually mean – all SOs in the image, the selected ones, …? 

A Script could be used to automate the task. 

Earth Oliver
Legend
September 19, 2023

not possible.