Skip to main content
Participant
Sept. 19, 2023 2:45 PM UTC
Beantwortet

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!

Dieses Thema wurde für Antworten geschlossen.
Stephen Marsh
Community Expert
Sept. 20, 2023 11:16 AM UTC

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()");

 

 

14 Antworten

Stephen Marsh
Community Expert
Community Expert
Sept. 20, 2023 11:16 AM UTC

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()");

 

 

Übersetzen
c.pfaffenbichler
Community Expert
Community Expert
Sept. 20, 2023 11:20 AM UTC

Very industrious! 

 

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

Übersetzen
Stephen Marsh
Community Expert
Community Expert
Sept. 20, 2023 11:53 AM UTC
quote

Very industrious! 

 

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


By @c.pfaffenbichler


I had fun anyway!

Übersetzen
Stephen Marsh
Community Expert
Community Expert
Sept. 20, 2023 1:05 AM UTC

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()");
Übersetzen
c.pfaffenbichler
Community Expert
Community Expert
Sept. 20, 2023 6:23 AM UTC

I forgot to filter out Linked Smart Objects anyway … 

Übersetzen
Stephen Marsh
Community Expert
Community Expert
Sept. 20, 2023 6:53 AM UTC
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?

Übersetzen
Legend
Sept. 19, 2023 3:50 PM UTC

del

Übersetzen
c.pfaffenbichler
Community Expert
Community Expert
Sept. 19, 2023 3:55 PM UTC

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. 

Übersetzen
c.pfaffenbichler
Community Expert
Community Expert
Sept. 19, 2023 3:30 PM UTC
// 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)}
};
Übersetzen
c.pfaffenbichler
Community Expert
Community Expert
Sept. 19, 2023 3:01 PM UTC

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. 

Übersetzen
Earth Oliver
Legend
Sept. 19, 2023 2:55 PM UTC

not possible.

Übersetzen