1 2 Previous Next 41 Replies Latest reply on Jul 9, 2016 7:40 PM by JJMack

    change opacity levels individually

    Level 1

      Good year

      I'm new to scripting and speak little English I hope to be clear.

      I would like to create a panel that I change the opacity levels individually

      as in this example

      second and you can make it happen?

       

      Schermata 2016-01-01 alle 12.11.44.png

        • 1. Re: change opacity levels individually
          c.pfaffenbichler Level 9

          // change layers’ opacity via scrollbars;

          // 2015, use it at your own risk;

          #target photoshop

          if (app.documents.length > 0) {

          app.activeDocument.suspendHistory("opacity", "theDialog ()");

          };

          function theDialog () {

          //////////////////////////////////////////

          var theLayers = theLayerNames ();

          ////// create dialog for user-entry //////

          var dlg = new Window('dialog', "layeropacity", undefined);

          //////////////////////////////

          dlg.layer = dlg.add('panel', undefined, "opacity");

          dlg.layer.orientation = "column";

          dlg.layer.preferredSize = [300, 50];

          // populate panel;

          for (var m = 0; m < theLayers.length; m++) {

          dlg.layer.add('statictext', undefined, theLayers[m][0]);

          dlg.layer.add('scrollbar', undefined, theLayers[m][2], 0, 255);

          dlg.layer.children[m*2+1].preferredSize = [255,20];

          //////////////////////////////

          // if scrolled;

          dlg.layer.children[m*2+1].onChange = function () {

          var theCh = dlg.layer.children.length;

          for (var n = 0; n < theCh; n++) {

            if (dlg.layer.children[n] == this) {var theNumber = n}

            };

          setOpacityByID (theLayers[(theNumber-1)/2][1], Math.round(this.value));

          app.refresh();

          };

          };

          //////////////////////////////

          // build- and cancel-button;

          dlg.cancelOk = dlg.add('panel', [12,495,308,550], "");

          dlg.cancelOk.buildBtn = dlg.cancelOk.add('button', [10,10,141,40], 'OK', {name:'ok'});

          dlg.cancelOk.cancelBtn = dlg.cancelOk.add('button', [149,10,280,40], 'Cancel', {name:'cancel'});

          dlg.center();

          var myReturn = dlg.show ();

          //////////////////////////////

          // reset if canceled;

          if (myReturn != 1) {

          for (var a = 0; a < theLayers.length; a++) {

          setOpacityByID (theLayers[a][1], theLayers[a][2])

          }

          };

          $.gc();

          };

          ////////////////////////////////////

          function theLayerNames () {

          // the file;

          var myDocument = app.activeDocument;

          // get number of layers;

          var ref = new ActionReference();

          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 not layer group collect values;

          if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {

          var theName = layerDesc.getString(stringIDToTypeID('name'));

          var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

          var theOpacity = layerDesc.getInteger(stringIDToTypeID('opacity'));

          theLayers.push([theName, theID, theOpacity])

          };

          }

          catch (e) {};

          };

          return theLayers

          };

          ////// set opacity //////

          function setOpacityByID (theID, theOpacity) {

          // =======================================================

          var idsetd = charIDToTypeID( "setd" );

              var desc2 = new ActionDescriptor();

              var idnull = charIDToTypeID( "null" );

              var idLyr = charIDToTypeID( "Lyr " );

                  var ref1 = new ActionReference();

                  ref1.putIdentifier( idLyr, theID );

              desc2.putReference( idnull, ref1 );

              var idT = charIDToTypeID( "T   " );

                  var desc3 = new ActionDescriptor();

                  desc3.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), Math.round(theOpacity/2.55) );

              desc2.putObject( idT, idLyr, desc3 );

          executeAction( idsetd, desc2, DialogModes.NO );

          };

          • 2. Re: change opacity levels individually
            Level 1

            Big

            Thanks just what I needed

            friendly

            happy 2016

            • 3. Re: change opacity levels individually
              c.pfaffenbichler Level 9

              It may be a dubious improvement, but you could replace the function theDialog with this, it saves a few lines:

              function theDialog () {

              //////////////////////////////////////////

              var theLayers = theLayerNames ();

              ////// create dialog for user-entry //////

              var dlg = new Window('dialog', "layeropacity", undefined);

              //////////////////////////////

              dlg.layer = dlg.add('panel', undefined, "opacity");

              dlg.layer.orientation = "column";

              dlg.layer.preferredSize = [300, 50];

              // populate panel;

              for (var m = 0; m < theLayers.length; m++) {

              dlg.layer.add('statictext', undefined, theLayers[m][0]);

              dlg.layer.add('scrollbar', undefined, theLayers[m][2], 0, 255);

              dlg.layer.children[m*2+1].preferredSize = [256,20];

              dlg.layer.children[m*2+1].helpTip = String(m);

              //////////////////////////////

              // if scrolled;

              dlg.layer.children[m*2+1].onChange = function () {

              setOpacityByID (theLayers[Number(this.helpTip)][1], Math.round(this.value));

              app.refresh();

              };

              };

              //////////////////////////////

              // build- and cancel-button;

              dlg.cancelOk = dlg.add('panel', [12,495,308,550], "");

              dlg.cancelOk.buildBtn = dlg.cancelOk.add('button', [10,10,141,40], 'OK', {name:'ok'});

              dlg.cancelOk.cancelBtn = dlg.cancelOk.add('button', [149,10,280,40], 'Cancel', {name:'cancel'});

              dlg.center();

              var myReturn = dlg.show ();

              //////////////////////////////

              // reset if canceled;

              if (myReturn != 1) {

              for (var a = 0; a < theLayers.length; a++) {

              setOpacityByID (theLayers[a][1], theLayers[a][2])

              }

              };

              $.gc();

              };

              • 4. Re: change opacity levels individually
                Level 1

                Good morning and thank you,

                I noticed that the levels are proposed to me on the contrary

                you can modify them to see properly.

                Schermata 2016-01-03 alle 11.45.38.png

                 

                the second script does not go on my mac.

                • 5. Re: change opacity levels individually
                  c.pfaffenbichler Level 9

                  Post 3 did not contain the complete Script but only the one function.

                   

                  I noticed that the levels are proposed to me on the contrary

                  What do you mean?

                  Which Photoshop version do you use?

                  For me the dialog looks like this and the left of the scrollbar represents 0%, the right 100%:

                  layerOpacityScr.jpg

                  • 6. Re: change opacity levels individually
                    Level 1

                    Viewing screenshot

                    you too are on the contrary

                     

                     

                    use photoshop cc2014

                     

                     

                    excuse the panel used by you and much more 'nice

                    can I have it.

                     

                    layerOpacityScr copia.jpg

                    • 7. Re: change opacity levels individually
                      JJMack Most Valuable Participant

                      Whatever you do do not try the script with CC 2015 I found playing with a slider cause CC 2015 to loop be not responsive had to terminate CC 2015 with Windows Task Manager.

                      Capture.jpg

                      • 8. Re: change opacity levels individually
                        c.pfaffenbichler Level 9
                        I found playing with a slider cause CC 2015 to loop be not responsive had to terminate CC 2015 with Windows Task Manager

                        I use Photoshop CC 2015.1.1 on Mac OS X 10.9.5 and it seems to work fine.

                        Did you use the original one or did you replace the function theDialog with the amended one?

                         

                        you too are on the contrary

                        Once again: What do you mean?

                        • 9. Re: change opacity levels individually
                          Level 1

                          c.pfaffenbichler

                          Thanks for the time you gave me

                          I use cc2014 on mac and the slider works perfectly

                           

                           

                          how to make buttons cancel and ok like your.

                          • 10. Re: change opacity levels individually
                            c.pfaffenbichler Level 9
                            how to make buttons cancel and ok like your.

                            I’m afraid the appearance is a Script UI matter and dependent on the version of Photoshop/ExtendScript one uses, they just look like that in the most current version.

                            It might be possible to simulate the appearance in older versions by using graphics in the dialog but it does not seem worth the trouble.

                            • 12. Re: change opacity levels individually
                              c.pfaffenbichler Level 9
                              you too are on the contrary

                              Now I understand … changing the line

                              for (var m = 0; m < theLayers.length; m++) {

                              to

                              for (var m = theLayers.length-1; m >= 0; m--) {

                              should invert the order of the layers in the dialog.

                              • 13. Re: change opacity levels individually
                                JJMack Most Valuable Participant

                                No the produced an undefined error.

                                 

                                However changing

                                 

                                for (var m = 0; m <= theNumber; m++) {


                                to

                                 

                                for (var m = theNumber; m >= 0; m--) {

                                 

                                Works

                                • 14. Re: change opacity levels individually
                                  Level 1

                                  I put the string you sent me it by always error

                                  Schermata 2016-01-04 alle 14.55.44.png

                                  • 15. Re: change opacity levels individually
                                    c.pfaffenbichler Level 9

                                    // change layers’ opacity via scrollbars;

                                    // 2015, use it at your own risk;

                                    #target photoshop

                                    if (app.documents.length > 0) {

                                    app.activeDocument.suspendHistory("opacity", "theDialog ()");

                                    };

                                    function theDialog () {

                                    //////////////////////////////////////////

                                    var theLayers = theLayerNames ();

                                    ////// create dialog for user-entry //////

                                    var dlg = new Window('dialog', "layeropacity", undefined);

                                    //////////////////////////////

                                    dlg.layer = dlg.add('panel', undefined, "opacity");

                                    dlg.layer.orientation = "column";

                                    dlg.layer.preferredSize = [300, 50];

                                    // populate panel;

                                    for (var m = 0; m < theLayers.length; m++) {

                                    dlg.layer.add('statictext', undefined, theLayers[m][0]);

                                    dlg.layer.add('scrollbar', undefined, theLayers[m][2], 0, 255);

                                    var thisIndex = dlg.layer.children.length - 1;

                                    dlg.layer.children[thisIndex].preferredSize = [256,20];

                                    dlg.layer.children[thisIndex].helpTip = String(m);

                                    //////////////////////////////

                                    // if scrolled;

                                    dlg.layer.children[thisIndex].onChange = function () {

                                    setOpacityByID (theLayers[Number(this.helpTip)][1], Math.round(this.value));

                                    app.refresh();

                                    };

                                    };

                                    //////////////////////////////

                                    // build- and cancel-button;

                                    dlg.cancelOk = dlg.add('panel', [12,495,308,550], "");

                                    dlg.cancelOk.buildBtn = dlg.cancelOk.add('button', [10,10,141,40], 'OK', {name:'ok'});

                                    dlg.cancelOk.cancelBtn = dlg.cancelOk.add('button', [149,10,280,40], 'Cancel', {name:'cancel'});

                                    dlg.center();

                                    var myReturn = dlg.show ();

                                    //////////////////////////////

                                    // reset if canceled;

                                    if (myReturn != 1) {

                                    for (var a = 0; a < theLayers.length; a++) {

                                    setOpacityByID (theLayers[a][1], theLayers[a][2])

                                    }

                                    };

                                    $.gc();

                                    };

                                    ////////////////////////////////////

                                    function theLayerNames () {

                                    // the file;

                                    var myDocument = app.activeDocument;

                                    // get number of layers;

                                    var ref = new ActionReference();

                                    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 not layer group collect values;

                                    if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {

                                    var theName = layerDesc.getString(stringIDToTypeID('name'));

                                    var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

                                    var theOpacity = layerDesc.getInteger(stringIDToTypeID('opacity'));

                                    theLayers.push([theName, theID, theOpacity])

                                    };

                                    }

                                    catch (e) {};

                                    };

                                    return theLayers

                                    };

                                    ////// set opacity //////

                                    function setOpacityByID (theID, theOpacity) {

                                    // =======================================================

                                    var idsetd = charIDToTypeID( "setd" );

                                        var desc2 = new ActionDescriptor();

                                        var idnull = charIDToTypeID( "null" );

                                        var idLyr = charIDToTypeID( "Lyr " );

                                            var ref1 = new ActionReference();

                                            ref1.putIdentifier( idLyr, theID );

                                        desc2.putReference( idnull, ref1 );

                                        var idT = charIDToTypeID( "T   " );

                                            var desc3 = new ActionDescriptor();

                                            desc3.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), Math.round(theOpacity/2.55) );

                                        desc2.putObject( idT, idLyr, desc3 );

                                    executeAction( idsetd, desc2, DialogModes.NO );

                                    };

                                    • 16. Re: change opacity levels individually
                                      JJMack Most Valuable Participant

                                      Yes in that version the mod you posted works.  I must be using the first or second script posted.

                                       

                                      CC 2015 ScriptUI has problems and will loop and hang Photoshop. When you play with this scripts sliders too much.

                                      • 17. Re: change opacity levels individually
                                        JJMack Most Valuable Participant

                                        Here is his latest Script with his mod to  change the order of the sliders to match the layers palette..

                                         

                                        http://www.mouseprints.net/old/dpr/LayerOpacityScrollBars.jsx

                                         

                                         

                                        CC 2015 ScriptUI hang on my Windows 10 system with this script when you play with the sliders I use CC 2014.

                                        • 18. Re: change opacity levels individually
                                          c.pfaffenbichler Level 9

                                          I had a hang (in 2015.1.1) with the Script, too, recently.

                                          I wonder if $.gc() in the inChange function might help …

                                          • 19. Re: change opacity levels individually
                                            JJMack Most Valuable Participant

                                            As I only hack at scripting  I do not even know what  $.gc() is about. Garbish Collection?? Could you shed some light I was intending to look at documentation.

                                            • 20. Re: change opacity levels individually
                                              c.pfaffenbichler Level 9

                                              You are right, that’s garbage collection.

                                              • 21. Re: change opacity levels individually
                                                Level 1

                                                I downloaded the script of Mr. JJ Mack

                                                and it works well on cc2014mac

                                                I cancellalto this $ .gc (); and the script works well

                                                I sincerely thank you both for the help.

                                                • 22. Re: change opacity levels individually
                                                  JJMack Most Valuable Participant

                                                  Its a good script to experiment with. I can hang cc 2015 at will with it.

                                                  • 23. Re: change opacity levels individually
                                                    c.pfaffenbichler Level 9
                                                    I can hang cc 2015 at will with it.

                                                    Which step/s cause the hang?

                                                    • 24. Re: change opacity levels individually
                                                      JJMack Most Valuable Participant

                                                      ALL I need to do is drag a slider about  without letting the mouse bottom go. Soon both arrows on the scroll bar are gone soon the task manager shows Photoshop is not responding. Photoshop exe is still getting cpu but has to be terminated using the task manager

                                                      • 25. Re: change opacity levels individually
                                                        c.pfaffenbichler Level 9

                                                        JJMack, could you try with this dialog-function?

                                                        function theDialog () {

                                                        //////////////////////////////////////////

                                                        var theLayers = theLayerNames ();

                                                        ////// create dialog for user-entry //////

                                                        var dlg = new Window('dialog', "layeropacity", undefined);

                                                        //////////////////////////////

                                                        dlg.layer = dlg.add('panel', undefined, "opacity");

                                                        dlg.layer.orientation = "column";

                                                        dlg.layer.preferredSize = [300, 50];

                                                        // populate panel;

                                                        //for (var m = 0; m < theLayers.length; m++) {

                                                        for (var m = theLayers.length-1; m >= 0; m--) {

                                                        dlg.layer.add('statictext', undefined, theLayers[m][0]);

                                                        dlg.layer.add('scrollbar', undefined, theLayers[m][2], 0, 255);

                                                        var thisIndex = dlg.layer.children.length - 1;

                                                        dlg.layer.children[thisIndex].preferredSize = [256,20];

                                                        dlg.layer.children[thisIndex].helpTip = String(m);

                                                        //////////////////////////////

                                                        // if scrolled;

                                                        dlg.layer.children[thisIndex].onChange = function () {

                                                        setOpacityByID (theLayers[Number(this.helpTip)][1], Math.round(this.value));

                                                        app.refresh();

                                                        $.gc();

                                                        };

                                                        };

                                                        //////////////////////////////

                                                        // build- and cancel-button;

                                                        dlg.cancelOk = dlg.add('panel', [12,495,308,550], "");

                                                        dlg.cancelOk.buildBtn = dlg.cancelOk.add('button', [10,10,141,40], 'OK', {name:'ok'});

                                                        dlg.cancelOk.cancelBtn = dlg.cancelOk.add('button', [149,10,280,40], 'Cancel', {name:'cancel'});

                                                        dlg.center();

                                                        var myReturn = dlg.show ();

                                                        //////////////////////////////

                                                        // reset if canceled;

                                                        if (myReturn != 1) {

                                                        for (var a = 0; a < theLayers.length; a++) {

                                                        setOpacityByID (theLayers[a][1], theLayers[a][2])

                                                        }

                                                        };

                                                        $.gc();

                                                        };

                                                        The difference is the $.gc() in the onChange function – not sure it makes a difference, but so far I have not been able to create a hang with this.

                                                        • 26. Re: change opacity levels individually
                                                          JJMack Most Valuable Participant

                                                          I had tries putting garbage collection in the onChange function when you made the other append. It made no difference the arrows go away Photoshop loops and does not respond to user action has to be terminated using Task Manager.

                                                          Capture.jpg

                                                          • 27. Re: change opacity levels individually
                                                            c.pfaffenbichler Level 9

                                                            Sorry and thanks for checking (back).

                                                            I’m out of ideas on this one so far …

                                                            • 28. Re: change opacity levels individually
                                                              JJMack Most Valuable Participant

                                                              I think its just an other bug in the new version of ScriptUI in CC 2015. The are many issues in CC 2015 I do not use it.  Perhaps Adobe will do better in 2016 happy new year...

                                                              • 29. Re: change opacity levels individually
                                                                c.pfaffenbichler Level 9

                                                                And a Happy New Year to you, too!

                                                                • 30. Re: change opacity levels individually
                                                                  Level 1

                                                                  Sorry if I take advantage of your kindness

                                                                  you could insert a checkbox before e after to see the changes.

                                                                  • 31. Re: change opacity levels individually
                                                                    c.pfaffenbichler Level 9

                                                                    I suppose there are (at least) two possible approaches

                                                                    • collect the current settings to be able to, if the preview is turned off, re-apply the original ones and then, if the preview is turned on, re-apply the edited settings

                                                                    • create a "Snapshot" and insert/remove that as a Layer

                                                                    The actual History Snapshot will not work out currently as the History is suspended, not suspending the History would cause every change in the dialog to register in the History.

                                                                     

                                                                    Maybe you should try to implement the changes when you have a little more experience with Photoshop Scripting.

                                                                    • 32. Re: change opacity levels individually
                                                                      Level 1

                                                                      I realized with scripting'm having a little 'difficulty is why I ask for help, reading your code much I learn things from books and much more' difficult to understand,

                                                                      I thought about doing something like that was more 'easy, but you gave me to understand that it is very difficult.

                                                                       

                                                                       

                                                                      I hope someone reads the discussion and help me in this.

                                                                       

                                                                       

                                                                      Again thank you for everything

                                                                      • 33. Re: change opacity levels individually
                                                                        c.pfaffenbichler Level 9
                                                                        but you gave me to understand that it is very difficult.

                                                                        No, it is feasible but as this is just a Script and no proper plugin the methods to achieve this are a little roundabout.

                                                                        One approach would be to use something like is currently being used if the dialog gets cancelled

                                                                        if (myReturn != 1) {

                                                                        for (var a = 0; a < theLayers.length; a++) {

                                                                        setOpacityByID (theLayers[a][1], theLayers[a][2])

                                                                        }

                                                                        };

                                                                        to reset the original opacity values for the Layers on the click of a checkbox and do the "reverse" if it is unchecked.

                                                                        • 34. Re: change opacity levels individually
                                                                          Level 1

                                                                          I understood this thing and  difficult

                                                                          even if you create a level snapshot with a checkbox that enables and disables this can be done.

                                                                          • 35. Re: change opacity levels individually
                                                                            c.pfaffenbichler Level 9

                                                                            If you have a specific question maybe you should start a new thread.

                                                                            This one is a not of a nuisance to me because all my recent posts here seem to have triggered moderation …

                                                                            • 36. Re: change opacity levels individually
                                                                              JJMack Most Valuable Participant

                                                                              I actually do not see why you would even want this script.  For you have scrubby slider in the layers palette for layers opacity in the first place..

                                                                              The only benefit I see the script provides is you can make many opacity changes and not add history states because while the script is in control it turns off history state recording. However by the same token when the script is running all you can do is change layers opacity all other Photoshop features are not avaible for use.

                                                                              • 37. Re: change opacity levels individually
                                                                                Level 1

                                                                                Thank you again

                                                                                if I have any questions I will open 'another topic

                                                                                I  thank sincerely them both for as long as I have dedicated.

                                                                                • 38. Re: change opacity levels individually
                                                                                  JJMack Most Valuable Participant

                                                                                  I was looking at te examples in the ExtendScript Toolkit SDK ColorPicker.jsx and ColorSelector.jsx the ScriptUI sliders they use work fine in CC 2015 however the controls in ColorSelector.jsx Dialog do not work. So you may want to change scroll bars to sliders.

                                                                                  Capture.jpg

                                                                                  • 39. Re: change opacity levels individually
                                                                                    JJMack Most Valuable Participant

                                                                                    I still find if you quickly move a scrollbar's slider side to side the script  scrollbar's end arrows will vanish using Photoshop CC 2015 and CC 2015.5  and Photoshop will stop responding.  The Windows Task manager will show the Photoshop is not responding but is using 5% of my machine CPU power. The Task Manager must be used to terminate Photoshop.  The Script work well in Photoshop versions prior the CC 2015.

                                                                                     

                                                                                    Capture.jpg

                                                                                    1 2 Previous Next