• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Flip only selected layers one by one

Engaged ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

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. ]

TOPICS
Actions and scripting

Views

594

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 01, 2023 Jul 01, 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('ordin
...

Votes

Translate

Translate
Adobe
Engaged ,
Jun 30, 2023 Jun 30, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 30, 2023 Jun 30, 2023

Copy link to clipboard

Copied

@c.pfaffenbichler @Stephen_A_Marsh @Chuck Uebele  

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 01, 2023 Jul 01, 2023

Copy link to clipboard

Copied

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 ]

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 01, 2023 Jul 01, 2023

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 01, 2023 Jul 01, 2023

Copy link to clipboard

Copied

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-lay...

 

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
    }
}

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 01, 2023 Jul 01, 2023

Copy link to clipboard

Copied


@Stephen_A_Marsh wrote:

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-lay...

 

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
    }
}

 

 

 


can you tell me why you not added following to your code? 

 

//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

 

and

 

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

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 01, 2023 Jul 01, 2023

Copy link to clipboard

Copied

quote

can you tell me why you not added following to your code? 

 

//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

 

and

 

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

 

 


By @abolfazl29032603daba

 

 

Does the code work as expected?

 

I only copied and modified the code that I believed was applicable to work on selected layers.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 01, 2023 Jul 01, 2023

Copy link to clipboard

Copied

quoteDoes the code work as expected?

 

I only copied the code that I believed was applicable to work on selected layers.

 


By @Stephen_A_Marsh

yes it worked :))

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 01, 2023 Jul 01, 2023

Copy link to clipboard

Copied

quote
quoteDoes the code work as expected?

 

I only copied the code that I believed was applicable to work on selected layers.

 


By @Stephen_A_Marsh

yes it worked :))


By @abolfazl29032603daba

 

Then all is good! :]

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 02, 2023 Jul 02, 2023

Copy link to clipboard

Copied

LATEST

@Stephen_A_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: 

pixxxelschubser_4-1688323156562.jpeg

 


 

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

pixxxelschubser_5-1688323225378.jpeg

 


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

 



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines