Skip to main content
CottonandTwirls
Participant
May 22, 2022
Answered

Can you select multiple layers in a document and have them all duplicate to unique documents

  • May 22, 2022
  • 3 replies
  • 493 views

Say I have multiple layers in a document. I right click a layer, go to duplicate, select new document. Is there a way to select multiple layers, and then duplicate them to *unique* documents? As in, I select 4 layers, and I duplicate them all at once to 4 new documents?

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

@CottonandTwirls – As I think about this, an action might be able to do so, however, it would be messy... Here is a script that should achieve what you are looking for:

 

/* 
https://community.adobe.com/t5/photoshop-ecosystem-discussions/can-you-select-multiple-layers-in-a-document-and-have-them-all-duplicate-to-unique-documents/td-p/12957700
Dupe Selected Layers to New Docs.jsx
v1.0 - Stephen Marsh, 22nd May 2022
Note: No checks are performed for the selected layer kind, ensure that the correct layers are selected!
*/

#target photoshop

function main() {

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

            // Set the parent doc
            var origDoc = activeDocument;

            // RegEx remove illegal filename characters from document name
            var docName = activeDocument.activeLayer.name.replace(/[:\/\\*\?\"\<\>\|\\\r\\\n.]/g, ""); // "/\:*?"<>|\r\n" -> "-";

            // Layer name
            var layerName = activeDocument.activeLayer.name;

            // Show only the current layer
            toggleActiveLayerVisibility(true);

            // Duplicate the current layer to a new doc
            dupeLayer();

            // Return to the parent doc
            activeDocument = origDoc;

        }


        /* Functions */

        // Dupe active layer to new doc
        function dupeLayer() {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();
            var reference2 = new ActionReference();
            reference.putClass(s2t("document"));
            descriptor.putReference(s2t("null"), reference);
            // Duped document name
            descriptor.putString(s2t("name"), docName);
            reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
            descriptor.putReference(s2t("using"), reference2);
            // Duped layer name
            descriptor.putString(s2t("layerName"), layerName);
            descriptor.putInteger(s2t("version"), 0);
            executeAction(s2t("make"), descriptor, DialogModes.NO);
        }

        // Toggle active layer visibility
        function toggleActiveLayerVisibility(toggleOptionsPalette) {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            var list = new ActionList();
            var reference = new ActionReference();
            reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
            list.putReference(reference);
            descriptor.putList(s2t("null"), list);
            descriptor.putBoolean(s2t("toggleOptionsPalette"), toggleOptionsPalette);
            executeAction(s2t("show"), descriptor, DialogModes.NO);
        }
}

app.activeDocument.suspendHistory("Dupe Selected Layers to New Docs.jsx", "main()");

 

Saving & Installing Scripts:

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save the text file as .txt
  5. Rename the file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

 

More here:

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html#Photoshop

 

3 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
May 22, 2022

@CottonandTwirls – As I think about this, an action might be able to do so, however, it would be messy... Here is a script that should achieve what you are looking for:

 

/* 
https://community.adobe.com/t5/photoshop-ecosystem-discussions/can-you-select-multiple-layers-in-a-document-and-have-them-all-duplicate-to-unique-documents/td-p/12957700
Dupe Selected Layers to New Docs.jsx
v1.0 - Stephen Marsh, 22nd May 2022
Note: No checks are performed for the selected layer kind, ensure that the correct layers are selected!
*/

#target photoshop

function main() {

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

            // Set the parent doc
            var origDoc = activeDocument;

            // RegEx remove illegal filename characters from document name
            var docName = activeDocument.activeLayer.name.replace(/[:\/\\*\?\"\<\>\|\\\r\\\n.]/g, ""); // "/\:*?"<>|\r\n" -> "-";

            // Layer name
            var layerName = activeDocument.activeLayer.name;

            // Show only the current layer
            toggleActiveLayerVisibility(true);

            // Duplicate the current layer to a new doc
            dupeLayer();

            // Return to the parent doc
            activeDocument = origDoc;

        }


        /* Functions */

        // Dupe active layer to new doc
        function dupeLayer() {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();
            var reference2 = new ActionReference();
            reference.putClass(s2t("document"));
            descriptor.putReference(s2t("null"), reference);
            // Duped document name
            descriptor.putString(s2t("name"), docName);
            reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
            descriptor.putReference(s2t("using"), reference2);
            // Duped layer name
            descriptor.putString(s2t("layerName"), layerName);
            descriptor.putInteger(s2t("version"), 0);
            executeAction(s2t("make"), descriptor, DialogModes.NO);
        }

        // Toggle active layer visibility
        function toggleActiveLayerVisibility(toggleOptionsPalette) {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            var list = new ActionList();
            var reference = new ActionReference();
            reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
            list.putReference(reference);
            descriptor.putList(s2t("null"), list);
            descriptor.putBoolean(s2t("toggleOptionsPalette"), toggleOptionsPalette);
            executeAction(s2t("show"), descriptor, DialogModes.NO);
        }
}

app.activeDocument.suspendHistory("Dupe Selected Layers to New Docs.jsx", "main()");

 

Saving & Installing Scripts:

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save the text file as .txt
  5. Rename the file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

 

More here:

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html#Photoshop

 

CottonandTwirls
Participant
May 22, 2022

Stephen I cannot express my gratitute enough to you for the various scripts you've written that have saved me literally HOURS of work. I just started using your rename-multiple-layers-at-once script last week and I went around and told every single person I know who uses Photoshop where to find it lol. Thank you, once again. This works perfectly. 

Stephen Marsh
Community Expert
Community Expert
May 22, 2022

@CottonandTwirls – You're welcome, I'd like to think that I am helping to pay back to the community in the same spirit in which others have helped me.

Bojan Živković11378569
Community Expert
Community Expert
May 22, 2022

Only with script as suggested by @Stephen Marsh, there isn't built in functionality as far as I know.

lambiloon
Community Expert
Community Expert
May 22, 2022

Hi, you can use the move tool to do this use the auto-select option then you can select all layers by selecting them from the main working area then control + j to duplicate them at once...regards

Ali Sajjad / Graphic Design Trainer / Freelancer / Adobe Certified Professional
Stephen Marsh
Community Expert
Community Expert
May 22, 2022

@CottonandTwirls - yes, this is possible with scripting.

 

@lambiloon - how does this dupe to separate individual documents?