Skip to main content
Mohamed Hameed21513110
Inspiring
November 23, 2021
Answered

Sort photoshop text layer by index or position

  • November 23, 2021
  • 3 replies
  • 4374 views

Hi ..

I have file with multi text layer

i need to sort selected text layer by index or position

for example :

from photo 0001  to photo 0002

i hope any one help me ..

thanks

This topic has been closed for replies.
Correct answer Kukurykus

Thank you very much

Is it possible to have the full code please?


(lrs = (aD = activeDocument).artLayers)
.add().move(lrs[0], ElementPlacement.PLACEBEFORE)
lngth = (lrs1 = [].slice.call(aD.artLayers)).length, lrs2 = [].slice.call(lrs1)
function txt(v) {return v.kind == 'LayerKind.TEXT'} function bnds(v) {return v.bounds[1]}
lrs1.sort(function(v1, v2){return txt(v1) && txt(v2) && bnds(v1) - bnds(v2)}); while
(lrs2.length) (shft = lrs2.shift()).kind == 'LayerKind.TEXT' && (lrs1.shift().move
	(aD.artLayers[lngth - lrs2.length], ElementPlacement.PLACEBEFORE))
aD.activeLayer.remove()

3 replies

jonj82928671
Known Participant
November 7, 2023
#target photoshop

(function(app) {
    if (isDocumentOpen()) {
        main();
    } else {
        alert("Please open a document to run this script.");
    }

    /**
     * @Desc Entry point of the program. Retrieves the active document, checks if layers exist, and sorts all visible and unlocked ArtLayer and LayerSet objects.
     */
    function main() {
        var activeDoc = app.activeDocument;

        if (activeDoc.layers.length > 0) {
            sortVisibleUnlockedLayersByBottomPosition(activeDoc.layers);
        }
    }

    /**
     * @Desc Sorts visible and unlocked layers within the current LayerSet by their Y position relative to the bottom of the layer.
     * @9397041 {Layers} layers Collection of ArtLayer and LayerSet objects in the current scope.
     */
    function sortVisibleUnlockedLayersByBottomPosition(layers) {
        var layerBuffer = new Array(),
            index = 0;
       
        // Iterate through the layers and gather visible and unlocked ones.
        for (index = 0; index < layers.length; index++) {
            var currentLayer = layers[index];
            if (!currentLayer.isBackgroundLayer && currentLayer.visible && !currentLayer.allLocked) {
                layerBuffer.push(currentLayer);
               
                // Check if we need to sort groups within the current LayerSet.
                if (currentLayer.typename == "LayerSet") {
                    sortVisibleUnlockedLayersByBottomPosition(currentLayer.layers);
                }
            }
        }
       
        // Sort the buffer array using the built-in natural sort comparer.
        layerBuffer.sort(compareByBottomPosition);
       
        // Move each layer accordingly.
        for (index = 0; index < layerBuffer.length; index++) {
            layerBuffer[index].move(layers[index], ElementPlacement.PLACEBEFORE);
        }      
    }
   
    /**
     * @Desc Comparator to sort layers by their Y position relative to the bottom of the layer.
     * @9397041 {ArtLayer} a The first layer to compare.
     * @9397041 {ArtLayer} b The second layer to compare.
     * @Returns {Number}
     */
    function compareByBottomPosition(b,a) {
        return a.bounds[3] - b.bounds[3];
    }

    /**
     * @Desc Checks if there is a document open.
     * @Returns {Boolean}
     */
    function isDocumentOpen() {
        return app.documents.length > 0;    
    }
}(app));
Kukurykus
Legend
November 23, 2021

 

lngth = (lrs1 = [].slice.call((aD = activeDocument).artLayers)).length, lrs2 = [].slice.call(lrs1)
function bnds(v) {return v.bounds[1]} function txt(v) {return v.kind == 'LayerKind.TEXT'}
lrs1.sort(function(v1, v2){return txt(v1) && txt(v2) && bnds(v1) - bnds(v2)}); while
(lrs2.length) (shft = lrs2.shift()).kind == 'LayerKind.TEXT' && (lrs1.shift().move
	(aD.artLayers[lngth - lrs2.length], ElementPlacement.PLACEBEFORE))

 

c.pfaffenbichler
Community Expert
Community Expert
November 23, 2021

Very economic! 

Kukurykus
Legend
November 23, 2021

I was too fast - that version wasn't good, try new one 😉

c.pfaffenbichler
Community Expert
Community Expert
November 23, 2021

So this is strictly about the y-coordinate (the vertical one)? 

Mohamed Hameed21513110
Inspiring
November 23, 2021

yes , but how i do it ?

c.pfaffenbichler
Community Expert
Community Expert
November 23, 2021

This function gets the bounds and IDs etc. of Type Layers; you can then use the pertinent value to process the Layers further: 

// 2021, use it at your own risk;
if (app.documents.length > 0) {
    var theLayers = collectTypeLayersBounds ();
    alert ("type layers\n"+theLayers.join("\n"));
};
////////////////////////////////////
////// collect layers //////
function collectTypeLayersBounds () {
    // the file;
    var myDocument = app.activeDocument;
    // get number of layers;
    var ref = new ActionReference();
    ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
    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 type layer collect values;
    if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true && layerDesc.hasKey(stringIDToTypeID("textKey")) == true) {
    var theName = layerDesc.getString(stringIDToTypeID('name'));
    var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
    var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
    var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
    var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
    theLayers.push([theName, theIndex, theID, theseBounds])
    };
    }
    catch (e) {};
    };
    return theLayers
    };