Skip to main content
Known Participant
February 24, 2026
Question

Script to Move Selected Text Layers into a Specific Folder (Create If Not Exists)

  • February 24, 2026
  • 3 replies
  • 34 views

Hi everyone,

I’m looking for a Photoshop script that can help organize layers with the following behavior:

- Move the selected layers into a layer group (folder) with a fixed name (for example: “A”)

- If the group does not exist, the script should create it automatically

- If the group already exists, the script should move the selected layers into that existing group

If anyone has an existing script, sample code, or advice on how to implement this in Photoshop scripting, I’d really appreciate it.

Thanks a lot!

    3 replies

    Stephen Marsh
    Community Expert
    Community Expert
    February 26, 2026

    Oops, I missed that the title mentioned text layers!

     

    Both of the scripts currently move any layer, without restricting it to be a layer kind of text.

    Stephen Marsh
    Community Expert
    Community Expert
    February 25, 2026

    This 1.1 version offers a prompt to enter the group name, rather than hard coding it into the script.

     

    /*
    Group or Move Selected Layers to Group.jsx
    Stephen Marsh
    v1.0, 25th February 2026 - Initial release
    v1.1, 25th February 2026 - Added a prompt to enter the group name
    https://community.adobe.com/questions-712/script-to-move-selected-text-layers-into-a-specific-folder-create-if-not-exists-1551320
    */

    #target photoshop

    if (app.documents.length > 0) {
    app.activeDocument.suspendHistory("Group or Move Selected Layers to Group", "main()");
    } else {
    alert("A document must be open to run this script!");
    }

    // Main function
    function main() {

    // Prompt for group name (default = "A")
    var theGroupName = prompt("Enter the group name:", "A");

    // Exit if cancelled or empty
    if (!theGroupName || theGroupName.replace(/\s+/g, "") === "") {
    return;
    }

    if (!groupExists(theGroupName)) {
    selectedLayersToGroup(theGroupName);
    } else {
    moveSelectedLayersToTopOfGroup(theGroupName);
    }
    }


    // Helper Functions

    function moveSelectedLayersToTopOfGroup(groupName) {

    var doc = app.activeDocument;
    var targetGroup = doc.layerSets.getByName(groupName);
    var selectedLayers = getSelectedLayers();

    if (selectedLayers.length === 0) return;

    // Remove the group itself if selected
    for (var i = selectedLayers.length - 1; i >= 0; i--) {
    if (selectedLayers[i] === targetGroup) {
    selectedLayers.splice(i, 1);
    }
    }

    if (selectedLayers.length === 0) return;

    // Move layers into group (reverse order preserves stacking)
    for (var i = selectedLayers.length - 1; i >= 0; i--) {
    selectedLayers[i].move(targetGroup, ElementPlacement.INSIDE);
    }

    // Move them to the top of the group
    for (var j = selectedLayers.length - 1; j >= 0; j--) {
    selectedLayers[j].move(targetGroup.layers[0], ElementPlacement.PLACEBEFORE);
    }
    }

    function selectedLayersToGroup(groupName) {

    var idMk = charIDToTypeID("Mk ");
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass(stringIDToTypeID("layerSection"));
    desc.putReference(charIDToTypeID("null"), ref);

    var refFrom = new ActionReference();
    refFrom.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    desc.putReference(charIDToTypeID("From"), refFrom);

    var descName = new ActionDescriptor();
    descName.putString(charIDToTypeID("Nm "), groupName);
    desc.putObject(charIDToTypeID("Usng"), stringIDToTypeID("layerSection"), descName);
    desc.putString(charIDToTypeID("Nm "), groupName);

    executeAction(idMk, desc, DialogModes.NO);
    }

    function groupExists(groupName) {

    var layers = app.activeDocument.layers;

    for (var i = 0; i < layers.length; i++) {
    if (layers[i].typename === "LayerSet" && layers[i].name === groupName) {
    return true;
    }
    }

    return false;
    }

    // Courtesy of jazz-y
    function getSelectedLayers() {

    var selLayers = [];
    var ref = new ActionReference();
    ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));

    try {
    var desc = executeActionGet(ref);
    if (desc.hasKey(stringIDToTypeID("targetLayers"))) {
    var list = desc.getList(stringIDToTypeID("targetLayers"));
    for (var i = 0; i < list.count; i++) {
    var idx = list.getReference(i).getIndex();
    selLayers.push(app.activeDocument.layers[idx - 1]);
    }
    }
    } catch (e) {
    selLayers.push(app.activeDocument.activeLayer);
    }

    return selLayers;
    }

     

    Stephen Marsh
    Community Expert
    Community Expert
    February 25, 2026

    @KathyDang 

     

    I’m not aware of an existing script to do this, so I wrote one for you!

     

    /*
    Group or Move Selected Layers to Group.jsx
    Stephen Marsh
    v1.0, 25th February 2026
    https://community.adobe.com/questions-712/script-to-move-selected-text-layers-into-a-specific-folder-create-if-not-exists-1551320
    */

    #target photoshop

    if (app.documents.length > 0) {
    app.activeDocument.suspendHistory("Group or Move Selected Layers to Group", "main()");
    } else {
    alert("A document must be open to run this script!");
    }

    // Main function
    function main() {
    var theGroupName = "A"; // The group name
    if (!groupExists(theGroupName)) {
    selectedLayersToGroup(theGroupName);
    } else {
    moveSelectedLayersToTopOfGroup(theGroupName);
    }
    }


    // Helper Functions

    function moveSelectedLayersToTopOfGroup(groupName) {

    var doc = app.activeDocument;
    var targetGroup = doc.layerSets.getByName(groupName);
    var selectedLayers = getSelectedLayers();

    if (selectedLayers.length === 0) return;

    // Remove the group itself if selected
    for (var i = selectedLayers.length - 1; i >= 0; i--) {
    if (selectedLayers[i] === targetGroup) {
    selectedLayers.splice(i, 1);
    }
    }

    if (selectedLayers.length === 0) return;

    // Move layers into group (reverse order preserves stacking)
    for (var i = selectedLayers.length - 1; i >= 0; i--) {
    selectedLayers[i].move(targetGroup, ElementPlacement.INSIDE);
    }

    // Move them to the top of the group
    for (var j = selectedLayers.length - 1; j >= 0; j--) {
    selectedLayers[j].move(targetGroup.layers[0], ElementPlacement.PLACEBEFORE);
    }
    }

    function selectedLayersToGroup(groupName) {

    var idMk = charIDToTypeID("Mk ");
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass(stringIDToTypeID("layerSection"));
    desc.putReference(charIDToTypeID("null"), ref);

    var refFrom = new ActionReference();
    refFrom.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    desc.putReference(charIDToTypeID("From"), refFrom);

    var descName = new ActionDescriptor();
    descName.putString(charIDToTypeID("Nm "), groupName);
    desc.putObject(charIDToTypeID("Usng"), stringIDToTypeID("layerSection"), descName);
    desc.putString(charIDToTypeID("Nm "), groupName);

    executeAction(idMk, desc, DialogModes.NO);
    }

    function groupExists(groupName) {

    var layers = app.activeDocument.layers;

    for (var i = 0; i < layers.length; i++) {
    if (layers[i].typename === "LayerSet" && layers[i].name === groupName) {
    return true;
    }
    }

    return false;
    }

    // Courtesy of jazz-y
    function getSelectedLayers() {

    var selLayers = [];
    var ref = new ActionReference();
    ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));

    try {
    var desc = executeActionGet(ref);
    if (desc.hasKey(stringIDToTypeID("targetLayers"))) {
    var list = desc.getList(stringIDToTypeID("targetLayers"));
    for (var i = 0; i < list.count; i++) {
    var idx = list.getReference(i).getIndex();
    selLayers.push(app.activeDocument.layers[idx - 1]);
    }
    }
    } catch (e) {
    selLayers.push(app.activeDocument.activeLayer);
    }

    return selLayers;
    }

     

    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 as a plain text format file – .txt
    5. Rename the saved file extension from .txt to .jsx
    6. Install or browse to the .jsx file to run (see link below)

    [link removed by moderator]

     

    KathyDangAuthor
    Known Participant
    February 26, 2026

    @KathyDang 

     

    I’m not aware of an existing script to do this, so I wrote one for you!

     

    /*
    Group or Move Selected Layers to Group.jsx
    Stephen Marsh
    v1.0, 25th February 2026
    https://community.adobe.com/questions-712/script-to-move-selected-text-layers-into-a-specific-folder-create-if-not-exists-1551320
    */

    #target photoshop

    if (app.documents.length > 0) {
    app.activeDocument.suspendHistory("Group or Move Selected Layers to Group", "main()");
    } else {
    alert("A document must be open to run this script!");
    }

    // Main function
    function main() {
    var theGroupName = "A"; // The group name
    if (!groupExists(theGroupName)) {
    selectedLayersToGroup(theGroupName);
    } else {
    moveSelectedLayersToTopOfGroup(theGroupName);
    }
    }


    // Helper Functions

    function moveSelectedLayersToTopOfGroup(groupName) {

    var doc = app.activeDocument;
    var targetGroup = doc.layerSets.getByName(groupName);
    var selectedLayers = getSelectedLayers();

    if (selectedLayers.length === 0) return;

    // Remove the group itself if selected
    for (var i = selectedLayers.length - 1; i >= 0; i--) {
    if (selectedLayers[i] === targetGroup) {
    selectedLayers.splice(i, 1);
    }
    }

    if (selectedLayers.length === 0) return;

    // Move layers into group (reverse order preserves stacking)
    for (var i = selectedLayers.length - 1; i >= 0; i--) {
    selectedLayers[i].move(targetGroup, ElementPlacement.INSIDE);
    }

    // Move them to the top of the group
    for (var j = selectedLayers.length - 1; j >= 0; j--) {
    selectedLayers[j].move(targetGroup.layers[0], ElementPlacement.PLACEBEFORE);
    }
    }

    function selectedLayersToGroup(groupName) {

    var idMk = charIDToTypeID("Mk ");
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass(stringIDToTypeID("layerSection"));
    desc.putReference(charIDToTypeID("null"), ref);

    var refFrom = new ActionReference();
    refFrom.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    desc.putReference(charIDToTypeID("From"), refFrom);

    var descName = new ActionDescriptor();
    descName.putString(charIDToTypeID("Nm "), groupName);
    desc.putObject(charIDToTypeID("Usng"), stringIDToTypeID("layerSection"), descName);
    desc.putString(charIDToTypeID("Nm "), groupName);

    executeAction(idMk, desc, DialogModes.NO);
    }

    function groupExists(groupName) {

    var layers = app.activeDocument.layers;

    for (var i = 0; i < layers.length; i++) {
    if (layers[i].typename === "LayerSet" && layers[i].name === groupName) {
    return true;
    }
    }

    return false;
    }

    // Courtesy of jazz-y
    function getSelectedLayers() {

    var selLayers = [];
    var ref = new ActionReference();
    ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));

    try {
    var desc = executeActionGet(ref);
    if (desc.hasKey(stringIDToTypeID("targetLayers"))) {
    var list = desc.getList(stringIDToTypeID("targetLayers"));
    for (var i = 0; i < list.count; i++) {
    var idx = list.getReference(i).getIndex();
    selLayers.push(app.activeDocument.layers[idx - 1]);
    }
    }
    } catch (e) {
    selLayers.push(app.activeDocument.activeLayer);
    }

    return selLayers;
    }

     

    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 as a plain text format file – .txt
    5. Rename the saved file extension from .txt to .jsx
    6. Install or browse to the .jsx file to run (see link below)

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

    "Thank you! The script works, but there are a couple of issues:

    It only works if the layers are not already inside another group. In my workflow, all text layers are inside a main 'Large Group.' When I run the script, it creates the 'Small Folder' inside the large one correctly the first time. But the second time, it creates a duplicate folder instead of merging into the existing one.