function with if then else and opacity

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

getInteger

function with if then else and opacity

Post by getInteger »

Hello everyone.

I would like to know if it is possible to do that and how if possible please:

I launch a script which check my selected layer opacity:
If selected layer opacity = 100%
then set opacity to 0% and set opacity to the below layer to 100% (the selected layer is still the same).
else set opacity to 100% and set opacity to the below layer to 0% (the selected layer is still the same).

i hope i'm making any sense.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

function with if then else and opacity

Post by Mike Hale »

It should be easy to do. Get the parent of the selected layer. Search that parent's artLayers by index until you find the selected layer. Now that you have the selected layer's index you can change the opacity of the selected layer and the one below( index+1 ).

You should put in a check to make sure index+1 is a valid index in case the selected layer is the bottom layer. And this approach will only work with layers at the same level. In other words if the selected layer is at the bottom of a layerset you will not be able to find the layer below( outside the set ) using this method. For that you would need to use scriptlistener code.
getInteger

function with if then else and opacity

Post by getInteger »

I can assume it's easy to do for you because you're expert with coding but i don't know how to code, i just test stuffs with scriptlistener + piece of codes i can find, it's more luck and feeling than knowledge actually.
If you could show me a basic code i will really appreciate
Mike Hale

function with if then else and opacity

Post by Mike Hale »

Code: Select allvar curentLayer = app.activeDocument.activeLayer;
var curentParent = curentLayer.parent;
var currentIndex = getLayerIndex( curentParent, curentLayer );
if(curentLayer.opacity==100){
    curentParent.artLayers[currentIndex+1].opacity=0;
}
if(curentLayer.opacity==0){
    curentParent.artLayers[currentIndex+1].opacity=100;
}
function getLayerIndex( parent, layer ){
    for(var i=0;i<parent.artLayers.length;i++){
        if(parent.artLayers==layer) return i;
    }
};
getInteger

function with if then else and opacity

Post by getInteger »

Thanks you ! it was almost good, i changed it a bit and now i have exactly what i want

Code: Select allvar curentLayer = app.activeDocument.activeLayer;
var curentParent = curentLayer.parent;
var currentIndex = getLayerIndex( curentParent, curentLayer );

if(curentParent.artLayers[currentIndex+1].opacity==100){
   curentParent.artLayers[currentIndex+1].opacity=0;
   curentParent.artLayers[currentIndex].opacity=100;
}

else{
   curentParent.artLayers[currentIndex].opacity=0;
   curentParent.artLayers[currentIndex+1].opacity=100;
}

function getLayerIndex( parent, layer ){
    for(var i=0;i<parent.artLayers.length;i++){
        if(parent.artLayers==layer) return i;
    }
};
What should i do if i want to add the possibility that the top layer is in a group and the below one outside the group or the opposite ?
Mike Hale

function with if then else and opacity

Post by Mike Hale »

You can use the two scriptlistener functions below to move up and down the layer stack. With this method you will need to change the activeLayer but you can always set it back when you are done.

Code: Select allfunction selectLayerAbove(){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Frwr" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
    desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
}
function selectLayerBelow(){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Bckw" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
    desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
}
getInteger

function with if then else and opacity

Post by getInteger »

i would like to be able to launch the script very often and very quickly with no manual movments between.
Isn't there a way to add conditions in the previous code to detect which layer is just behind:



In my example,
if my selected layer is Layer 4 i would like the script to interact between Layer 4 and Layer 5
if my selected layer is Layer 5 i would like the script to interact between Layer 5 and Layer 3
if my selected layer is Layer 3 i would like the script to interact between Layer 3 and Layer 2
if my selected layer is Layer 2 i would like the script to interact between Layer 2 and Layer 1
if my selected layer is Layer 1 i would like the script to interact between Layer 1 and Background

If my selection is not a layer and/or more than 1 layer then return an error.

Is there an easy way to do that ?
EDIT: also is there an easy way to remove the blending options( which are the result of the script use only) from history automatically ?
Mike Hale

function with if then else and opacity

Post by Mike Hale »

The answers to some of your questions depends on which version of Photoshop you are using. With CS4 or CS5 you can use scriptlistener to determine the number of selected layers for the multi layer error. However if there is no layer selected activeLayer returns the top layer so I don't know of a way for the script to know if no layers are selected.

You might be able to use suspendHistory to stop the blendmode changes from showing up in the history.

I guess you could check if the activeLayer index is equal to the parent.layers.length. That would like the script know that the layer is at the bottom of a group. You could then find the index of the parent and use that to select the layer below. You would need to add checks to make sure the layer below is a layer and not a layerset. And the more layers/layerSets your doc has the slower a script that uses the DOM (like the first script) runs.
getInteger

function with if then else and opacity

Post by getInteger »

ok thanks you.
I use CS3.
What you told me seems very complicated to do with my current "skill".
If you have time to waste i will enjoy to have some "help" but the most important for me at the moment is to remove the blending options from the history.
How do you use the suspendHistory function this way please ?
Mike Hale

function with if then else and opacity

Post by Mike Hale »

I may have some time after the holiday.

To use suspendHistory you put your code that you don't want recorded in history in a function. You then call that function with suspendHistory.

Code: Select allapp.activeDocument.suspendHistory('String to place in history to undo the entire function','changeBlendMode()');

Where changeBlendmode is a function that does the work you want to hide.