Script for change of transparency of layers

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

ustinof

Script for change of transparency of layers

Post by ustinof »

Help!
The script for change of transparency of layers is necessary. The button + and -
Thanks in advance!

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

pfaffenbichler

Script for change of transparency of layers

Post by pfaffenbichler »

Please explain more clearly what you want.
Why do the default keyboard shortcuts for setting Opacity and Fill not suffice for your needs?
ustinof

Script for change of transparency of layers

Post by ustinof »

It is necessary to me not with concrete value of transparency of a layer.
It is necessary измененить transparency as a percentage, for example to reduce by 5%.
pfaffenbichler

Script for change of transparency of layers

Post by pfaffenbichler »

For affecting the active Layer DOM code should suffice.
If you want to affect more than one selected Layers AM code would seem necessary.
Code: Select all// 2014, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var theLayer = app.activeDocument.activeLayer;
if (theLayer.isBackgroundLayer == false) {
var theOpacity = theLayer.opacity - 5;
if (theOpacity < 0) {var theOpacity = 100 + theOpacity};
theLayer.opacity = theOpacity;
}
};
ustinof

Script for change of transparency of layers

Post by ustinof »

pfaffenbichler .

You the best! I asked many in Russia and in the world. Alas, anybody, except you couldn't or didn't want to answer correctly! !
Thanks! Good luck and prosperity! ! !
pfaffenbichler

Script for change of transparency of layers

Post by pfaffenbichler »

You’re welcome.
But please keep in mind this works for one selected Layer, for more than one selected Layers a different approach (Action Manager instead of Document Object Model) would be necessary.
ustinof

Script for change of transparency of layers

Post by ustinof »

Thanks! Everything is right. And if with group of layers - what script?
pfaffenbichler

Script for change of transparency of layers

Post by pfaffenbichler »

With one Group selected the Script should work fine, too.

But with more than one Layer (or Group) selected this should work:
Code: Select all// reduce opacity of selected layers by 5%;
// 2014, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
app.activeDocument.suspendHistory ("-5% opacity", "main ()")
};
////////////////////////////////////
function main () {
var theLayers = getSelectedLayersIdx();
// do stuff;
for (var m = 0; m < theLayers.length; m++) {
   var theNumber = Number(theLayers[m]);
   try {
      var ref = new ActionReference();
      //ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      ref.putIndex( charIDToTypeID( "Lyr " ), theNumber);
      var layerDesc = executeActionGet(ref);
      var theName = layerDesc.getString(stringIDToTypeID('name'));
      var theOpacity = Math.round(layerDesc.getInteger(charIDToTypeID( "Opct" )) / 2.55);
      var theNewOpacity = theOpacity - 5;
      if (theNewOpacity < 0) {var theNewOpacity = 100 + theNewOpacity};
// set the opacity;
// =======================================================
var idsetd = charIDToTypeID( "setd" );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
//        ref1.putEnumerated( charIDToTypeID( "Lyr " ), idOrdn = charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
        ref1.putIndex( charIDToTypeID( "Lyr " ), theNumber);
    desc2.putReference( idnull, ref1 );
    var idT = charIDToTypeID( "T   " );
        var desc3 = new ActionDescriptor();
        var idOpct = charIDToTypeID( "Opct" );
        var idPrc = charIDToTypeID( "#Prc" );
        desc3.putUnitDouble( idOpct, idPrc, theNewOpacity );
    var idLyr = charIDToTypeID( "Lyr " );
    desc2.putObject( idT, idLyr, desc3 );
executeAction( idsetd, desc2, DialogModes.NO );

      } catch (e) {};
   };
/*
// reselect layers;
for (var p = 0; p < theLayers.length; p++) {
   selectLayerByIndex(theLayers[p], true)
   };*/
};
////// by paul mr;
function getSelectedLayersIdx(){
      var selectedLayers = new Array;
      var ref = new ActionReference();
      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      var desc = executeActionGet(ref);
      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
         desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
          var c = desc.count
          var selectedLayers = new Array();
          for(var i=0;i<c;i++){
            try{
               activeDocument.backgroundLayer;
               selectedLayers.push(  desc.getReference( i ).getIndex() );
            }catch(e){
               selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
            }
          }
       }else{
         var ref = new ActionReference();
         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
         try{
            activeDocument.backgroundLayer;
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
         }catch(e){
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
         }
      }
      return selectedLayers;
};
// by mike hale, via paul riggott;
// http://forums.adobe.com/message/1944754#1944754
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
      desc.putBoolean( charIDToTypeID( "MkVs" ), false );
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
ustinof

Script for change of transparency of layers

Post by ustinof »

Class! Everything works! ! You excellent programmer! ! !