Skip to main content
New Participant
November 19, 2022
Answered

Converting to *Linked* Smart Object?

  • November 19, 2022
  • 2 replies
  • 2193 views

Hello all,

 

I'm looking for a way to convert layers to a *linked* smart object without having to, first, Convert to Smart Object, and secondly, Convert to Linked... Does anyone know if this is a possibility?

 

Thank you for any help 🙂

Enid

This topic has been closed for replies.
Correct answer Stephen Marsh

This version will create linked smart object layers for all selected "normal" top-level raster layers and layer groups. The linked SO PSB files will be automatically saved to the same path as the parent file (this could be changed to a sub-folder if more convenient for file management). A single undo history step is provided.

 

/*
Selected Layers to Linked Smart Objects.jsx
v1.0, 20th November 2022, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/converting-to-linked-smart-object/td-p/13357539
*/

#target photoshop

function main() {

    ///// Process selected layers - from 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();
    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);

        /////

        // Conditional check for normal or smart object layers, ignoring other layer kinds such as adjustment layers
        if (activeDocument.activeLayer.kind == LayerKind.NORMAL || activeDocument.activeLayer.kind == LayerKind.SMARTOBJECT) {
            newPlacedLayer();
            placedLayerConvertToLinked();
        }


        ///// Functions /////

        // Convert the active layer to an embedded smart object
        function newPlacedLayer() {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            executeAction(s2t("newPlacedLayer"), undefined, DialogModes.NO);
        }

        // Convert the active embedded smart object to a linked smart object
        function placedLayerConvertToLinked() {
            var savePath = activeDocument.path;
            // RegEx remove illegal/invalid filename characters from document name
            var layerName = activeDocument.activeLayer.name.replace(/[:\/\\*\?\"\<\>\|\\\r\\\n.]/g, ""); // "/\:*?"<>|\r\n" -> "-";;
            var idplacedLayerConvertToLinked = stringIDToTypeID("placedLayerConvertToLinked");
            var desc346 = new ActionDescriptor();
            var idnull = stringIDToTypeID("null");
            var ref49 = new ActionReference();
            var idlayer = stringIDToTypeID("layer");
            var idordinal = stringIDToTypeID("ordinal");
            var idtargetEnum = stringIDToTypeID("targetEnum");
            ref49.putEnumerated(idlayer, idordinal, idtargetEnum);
            desc346.putReference(idnull, ref49);
            var idusing = stringIDToTypeID("using");
            desc346.putPath(idusing, new File(savePath + "/" + layerName + ".psb"));
            executeAction(idplacedLayerConvertToLinked, desc346, DialogModes.NO);
        }


    }
}
app.activeDocument.suspendHistory("Selected Layers to Linked Smart Objects", "main()");

 

2 replies

Stephen Marsh
Community Expert
November 19, 2022

@Enid27216724xdpg – Let me see if this can be automated via scripting...

Stephen Marsh
Stephen MarshCorrect answer
Community Expert
November 19, 2022

This version will create linked smart object layers for all selected "normal" top-level raster layers and layer groups. The linked SO PSB files will be automatically saved to the same path as the parent file (this could be changed to a sub-folder if more convenient for file management). A single undo history step is provided.

 

/*
Selected Layers to Linked Smart Objects.jsx
v1.0, 20th November 2022, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/converting-to-linked-smart-object/td-p/13357539
*/

#target photoshop

function main() {

    ///// Process selected layers - from 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();
    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);

        /////

        // Conditional check for normal or smart object layers, ignoring other layer kinds such as adjustment layers
        if (activeDocument.activeLayer.kind == LayerKind.NORMAL || activeDocument.activeLayer.kind == LayerKind.SMARTOBJECT) {
            newPlacedLayer();
            placedLayerConvertToLinked();
        }


        ///// Functions /////

        // Convert the active layer to an embedded smart object
        function newPlacedLayer() {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            executeAction(s2t("newPlacedLayer"), undefined, DialogModes.NO);
        }

        // Convert the active embedded smart object to a linked smart object
        function placedLayerConvertToLinked() {
            var savePath = activeDocument.path;
            // RegEx remove illegal/invalid filename characters from document name
            var layerName = activeDocument.activeLayer.name.replace(/[:\/\\*\?\"\<\>\|\\\r\\\n.]/g, ""); // "/\:*?"<>|\r\n" -> "-";;
            var idplacedLayerConvertToLinked = stringIDToTypeID("placedLayerConvertToLinked");
            var desc346 = new ActionDescriptor();
            var idnull = stringIDToTypeID("null");
            var ref49 = new ActionReference();
            var idlayer = stringIDToTypeID("layer");
            var idordinal = stringIDToTypeID("ordinal");
            var idtargetEnum = stringIDToTypeID("targetEnum");
            ref49.putEnumerated(idlayer, idordinal, idtargetEnum);
            desc346.putReference(idnull, ref49);
            var idusing = stringIDToTypeID("using");
            desc346.putPath(idusing, new File(savePath + "/" + layerName + ".psb"));
            executeAction(idplacedLayerConvertToLinked, desc346, DialogModes.NO);
        }


    }
}
app.activeDocument.suspendHistory("Selected Layers to Linked Smart Objects", "main()");

 

Stephen Marsh
Community Expert
December 21, 2022

This is magical! Saved me so much time, thank you!

I'm running Photoshop 2023, and I got an error saying that the functionality may not be available on this version:

"- Could not convert smart objects to linked because the file was not found.

Line: 58

->              executeAction(idplacedLayerConvertToLinked, desc346, DialogModes.NO);"

But I just deleted that line and it worked like a charm.

Thanks so much!


Ah, yes, there can be differences in AM code between versions, I mostly develop in 2021, but I don't always test in later versions.

 

I'm surprised it worked with that line deleted, my meagre understanding of AM code is that it is required. But if it works for you, it works!

 

davescm
Community Expert
November 19, 2022

No, they are the steps you need to take - with the exception of directly placing an external document as a Linked Smart Object.

 

Dave