Skip to main content
Inspiring
June 30, 2023
Answered

Flip only selected layers one by one

  • June 30, 2023
  • 4 replies
  • 1143 views

With following script we can flip all layers one by one: 

 

 

//Script to flip all layers horizontally
//Just collect all layers in an array and then flip each one by looping
//if background layer is present it has to be transformed into a normal layer, then flipped, and then turned back into a
//Not really recomended for multiple frame animations.

//get the document
doc = app.activeDocument;

//get the array with all the layers
allLayers = [];
allLayers = collectAllLayers(doc, allLayers);

//loop and flip
for (var i = 0; i < allLayers.length; i++) {
    //flip all layers that aren't backgorund
    if (allLayers[i].isBackgroundLayer == false) {
        
        var vis = allLayers[i].visible;                                         //save whether the layer was visible or not

        allLayers[i].visible = true;                                            //set to visible so the layer is editable
        allLayers[i].resize(-100, undefined, AnchorPosition.MIDDLECENTER);      //flip the layer
        allLayers[i].visible = vis;                                             //set the visibility back to what it was before editing
    }

    else {
    //flip the backgorund layer (if existant)
        allLayers[i].visible = true;                                            //set to visible so the layer is editable
        allLayers[i].isBackgroundLayer = false;                                 //transform background layer into normal layer
        allLayers[i].resize(-100, undefined, AnchorPosition.MIDDLECENTER);      //flip it
        allLayers[i].isBackgroundLayer = true;                                  //turn back into backgorund layer
        allLayers[i].visible = vis;                                             //set the visibility back to what it was before editing
    }
}

function collectAllLayers (doc, allLayers){
    //returns array containing all posible layers.
    for (var m = 0; m < doc.layers.length; m++){
        var theLayer = doc.layers[m];
        if (theLayer.typename === "ArtLayer"){
            allLayers.push(theLayer);
        }else{
            collectAllLayers(theLayer, allLayers);
        }
    }
    return allLayers;
}

 

 

 

Now if i want to flip only selected layers one by one, what changes i must apply to above script? 

 

 

Please do not revise your posts in this way in future.

[ This thread is now closed by moderator. ]

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

The following script can be used as a framework to process only selected layers:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-run-any-script-on-selected-layers-only-not-all-layers/m-p/13890599

 

Here it is modified for only the selected layers:

 

#target photoshop

/* Start 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);
/* End Process selected layers - from jazz-y */

// Your code here:
    if (activeDocument.activeLayer.isBackgroundLayer == false) {
        var vis = activeDocument.activeLayer.visible;                                         //save whether the layer was visible or not
        activeDocument.activeLayer.visible = true;                                            //set to visible so the layer is editable
        activeDocument.activeLayer.resize(-100, undefined, AnchorPosition.MIDDLECENTER);      //flip the layer
        activeDocument.activeLayer.visible = vis;                                             //set the visibility back to what it was before editing
    }
    else {
        activeDocument.activeLayer.visible = true;                                            //set to visible so the layer is editable
        activeDocument.activeLayer.isBackgroundLayer = false;                                 //transform background layer into normal layer
        activeDocument.activeLayer.resize(-100, undefined, AnchorPosition.MIDDLECENTER);      //flip it
        activeDocument.activeLayer.isBackgroundLayer = true;                                  //turn back into backgorund layer
    }
}

 

 

4 replies

pixxxelschubser
Community Expert
Community Expert
July 2, 2023

@Stephen Marsh 

thanks for the correct solution.

 

@abolfazl29032603daba 

subsequent editing of posts is a useful function to remove spelling mistakes or make necessary additions. However, if, as in this case, the opening post is changed so that the question afterwards is a completely different one, answers already given become pointless at best. Or at worst, even inappropriate.

 

I am glad that a solution has been found for you.

 

@abolfazl29032603daba 

Please do not revise your posts in this way in future.

[ This thread is now closed by moderator. ]

 

 

--------------------------------------------------------------------------------------------------------------------------------------------------

Here is the original post with the original pictures inserted.

quote

Sometimes i have selection like following that i must flip from top to down one by one: 

 


 

now i want to flip only selected areas without separate layers like following: 

 


please note that i don't want to separate selected area to multiple layers. If this is not possible without separate selection, please provide code with separate selection to multiple layers. 

Preference is given to without separate selection.  

 

I have following script to flip all layers: 

//Script to flip all layers horizontally
//Just collect all layers in an array and then flip each one by looping
//if background layer is present it has to be transformed into a normal layer, then flipped, and then turned back into a
//Not really recomended for multiple frame animations.

//get the document
doc = app.activeDocument;

//get the array with all the layers
allLayers = [];
allLayers = collectAllLayers(doc, allLayers);

//loop and flip
for (var i = 0; i < allLayers.length; i++) {
    //flip all layers that aren't backgorund
    if (allLayers[i].isBackgroundLayer == false) {
        
        var vis = allLayers[i].visible;                                         //save whether the layer was visible or not

        allLayers[i].visible = true;                                            //set to visible so the layer is editable
        allLayers[i].resize(-100, undefined, AnchorPosition.MIDDLECENTER);      //flip the layer
        allLayers[i].visible = vis;                                             //set the visibility back to what it was before editing
    }

    else {
    //flip the backgorund layer (if existant)
        allLayers[i].visible = true;                                            //set to visible so the layer is editable
        allLayers[i].isBackgroundLayer = false;                                 //transform background layer into normal layer
        allLayers[i].resize(-100, undefined, AnchorPosition.MIDDLECENTER);      //flip it
        allLayers[i].isBackgroundLayer = true;                                  //turn back into backgorund layer
        allLayers[i].visible = vis;                                             //set the visibility back to what it was before editing
    }
}

function collectAllLayers (doc, allLayers){
    //returns array containing all posible layers.
    for (var m = 0; m < doc.layers.length; m++){
        var theLayer = doc.layers[m];
        if (theLayer.typename === "ArtLayer"){
            allLayers.push(theLayer);
        }else{
            collectAllLayers(theLayer, allLayers);
        }
    }
    return allLayers;
}

 

But the above code has the following two problems: 

  1. it flip all layers (i want to flip only selected areas). 
  2. If the selected areas are not divided into dedicated layers, the above script will only flip the main layer. 

If it possible help to solve following two problems. 

 

PNG sample for test attached to post. 

THEMEN
Aktionen und Skripte
 

By @abolfazl29032603daba

 



pixxxelschubser
Community Expert
Community Expert
July 1, 2023
quote

… Sometimes i have selection like following that i must flip from top to down one by one …

 

… But the above code has the following two problems: 

  1. it flip all layers (i want to flip only selected areas). 
  2. If the selected areas are not divided into dedicated layers, the above script will only flip the main layer …

By @abolfazl29032603daba

 

Sorry.

Unfortunately, you are subject to a fundamental error.


It is not the script that has the two problems. You are trying to use a script that was not written for the purpose you intended.

 

You are trying to mirror multiple non-transparent parts of a single layer that have been bounded by a selection. But that's not the problem either. In addition, you still want to align the individual non-transparent parts within the same selection (each separately) to the right.

 

This can only work if you distribute the individual parts on separate layers and then go through the layers in a loop and mirror them and align them to the right.

 

No longer applicable, as the original post has been completely revised several times and the question is now a completely different one.

 

[ edited by pixxxelschubser ]

 

Inspiring
July 2, 2023

my script flip all layers, if i want to flip only selected layers then what changes i must apply to my script to do it?

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
July 2, 2023

The following script can be used as a framework to process only selected layers:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-run-any-script-on-selected-layers-only-not-all-layers/m-p/13890599

 

Here it is modified for only the selected layers:

 

#target photoshop

/* Start 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);
/* End Process selected layers - from jazz-y */

// Your code here:
    if (activeDocument.activeLayer.isBackgroundLayer == false) {
        var vis = activeDocument.activeLayer.visible;                                         //save whether the layer was visible or not
        activeDocument.activeLayer.visible = true;                                            //set to visible so the layer is editable
        activeDocument.activeLayer.resize(-100, undefined, AnchorPosition.MIDDLECENTER);      //flip the layer
        activeDocument.activeLayer.visible = vis;                                             //set the visibility back to what it was before editing
    }
    else {
        activeDocument.activeLayer.visible = true;                                            //set to visible so the layer is editable
        activeDocument.activeLayer.isBackgroundLayer = false;                                 //transform background layer into normal layer
        activeDocument.activeLayer.resize(-100, undefined, AnchorPosition.MIDDLECENTER);      //flip it
        activeDocument.activeLayer.isBackgroundLayer = true;                                  //turn back into backgorund layer
    }
}

 

 

Inspiring
July 1, 2023

@c.pfaffenbichler @Stephen Marsh @Chuck Uebele  

If have any idea to modify that script plz provide here.

Inspiring
June 30, 2023

Nobody known? maybe this is my last request in this forum. this is very important.