17 Replies Latest reply on Oct 1, 2016 4:59 AM by RicSum

    Save Selection Script?

    RicSum Level 1

      Hi,

      I am trying to create a script which simply pops open the "Select > Save Selection" window.

      I need this to work in any document and on any layer. After researching basic scripting, I came across the script below, however, it throws an error, stating that a semicolon was expected?

       

      Is additional code needed?

      Any help would be greatly appreciated.

       

      Thank you all in advance.

       

      var docRef = app.activeDocument;

      var chanRef:Channel = docRef.channels.add();

      chanRef.name = "New Selection";

      chanRef.kind = ChannelType.SELECTEDAREA;

      docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND );

        • 1. Re: Save Selection Script?
          JJMack Most Valuable Participant

          To do the most likely you would need to use action manager code that shows the dialog.

           

          I save selection in one of my scripts without  the dialog showing here the action manager code you would need to turn on the dialog

           

          function  saveAlpha(alphaName){

            var idDplc = charIDToTypeID( "Dplc" );

               var desc27 = new ActionDescriptor();

              var idnull = charIDToTypeID( "null" );

                   var ref11 = new ActionReference();

                   var idChnl = charIDToTypeID( "Chnl" );

                   var idfsel = charIDToTypeID( "fsel" );

                   ref11.putProperty( idChnl, idfsel );

               desc27.putReference( idnull, ref11 );

               var idNm = charIDToTypeID( "Nm  " );

               desc27.putString( idNm, alphaName );

            try {

            executeAction( idDplc, desc27, DialogModes.NO );

            }

            catch(e) {}

          }

          • 2. Re: Save Selection Script?
            Tomas Sinkunas Level 3

            What about this?

             

            var docRef = app.activeDocument;
            var chanRef = docRef.channels.add();
            chanRef.name = "New Selection";
            chanRef.kind = ChannelType.SELECTEDAREA;
            docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND );
            
            • 3. Re: Save Selection Script?
              RicSum Level 1

              Thanks gentlemen, I will look into your answers as soon as I get a spare 5 minutes.

              Very much appreciated!

              • 4. Re: Save Selection Script?
                RicSum Level 1

                OK - I just tried both versions:

                 

                JJMack - the code you kindly posted did not work (no channel created).

                 

                Tomas - the modified code which you provided creates a channel, but for some strange reason it is inverted (the selection in the channel is black on a white background - I would have expected the selection to appear white on a black background).

                 

                Any idea how to invert the selection?

                 

                Thanks.

                • 5. Re: Save Selection Script?
                  Tomas Sinkunas Level 3

                  OK, let's try inverted then.

                   

                  var docRef = app.activeDocument;
                    docRef.selection.invert();
                  var chanRef = docRef.channels.add();
                    chanRef.name = "New Selection";
                    chanRef.kind = ChannelType.SELECTEDAREA;
                  docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.REPLACE );
                  
                  • 6. Re: Save Selection Script?
                    RicSum Level 1

                    The code is acting erratic.

                     

                    I tried the original code which Tomas modified (without the invert addition) and although it seems to create the alpha channel inverted in the channels panel (black selection on a white background) - it works as expected when the channel is loaded.

                     

                    However - the only problem now is that it deselects all the other channels, so that ONLY the newly created channel is visible?

                     

                    Very strange.

                    • 7. Re: Save Selection Script?
                      Tomas Sinkunas Level 3

                      Hi Ric. I am sorry I dont understand what you need then.

                      Can you explain more clearly or paste a few screenshots?

                      • 8. Re: Save Selection Script?
                        RicSum Level 1

                        OK.......

                        This is the code I currently have:

                         

                        var docRef = app.activeDocument;

                        var chanRef = docRef.channels.add();

                        chanRef.name = "New Selection";

                        chanRef.kind = ChannelType.SELECTEDAREA;

                        docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND );

                         

                        This code saves my selection as an alpha channel, but with 2 strange side effects:

                         

                        1) The channel in the channels panel looks inverted (black selection on white background - the opposite as expected) BUT, when the selection is loaded - it works as expected.

                         

                        2) The visibility of the other 4 default channels (RGB, R, G, B) all get hidden / deactivated (eye icon turned off).

                         

                        Hope that makes it clearer

                         

                        Thanks.

                        • 9. Re: Save Selection Script?
                          Tomas Sinkunas Level 3

                          1. Well, second code produces same results as the first one, except alpha channel is inverted, correcT?

                          2. Also what do you mean by "BUT, when the selection is loaded - it works as expected."? I don't follow.

                          3. Referring to your #2 - you want RGB channels to be selected, except the newly created channel?

                          Please post a screenshot of expected result.

                          • 10. Re: Save Selection Script?
                            RicSum Level 1

                            Tomas,

                             

                            yes - I would need the 4 default channels to be selected, and the newly created channel deselected, (the same as what happens when you do this via "Select > Save Selection" from the menubar).

                             

                            Regarding the first point...

                            The newly created channel in the channels panel seems to be created inverted (black selection on white background),but when loaded it works correctly (not inverted).

                             

                            This is fine, - it works as expected when loaded - therefore, there is no problem.

                            It is just strange that the newly created channel in the channels panel looks inverted (but isn't)

                            • 11. Re: Save Selection Script?
                              Tomas Sinkunas Level 3

                              Give me ~2 hours and I'll be back on this issue - my Photoshop is busy doing some work now.

                              • 12. Re: Save Selection Script?
                                RicSum Level 1

                                Cool. Thank you for helping me with this issue!

                                • 13. Re: Save Selection Script?
                                  RicSum Level 1

                                  Tomas,

                                  I posted a similar question in another thread, and the code which JJMack posted does exactly what I need - here is the code:

                                   

                                  function  saveAlpha(alphaName){

                                    var idDplc = charIDToTypeID( "Dplc" );

                                       var desc27 = new ActionDescriptor();

                                      var idnull = charIDToTypeID( "null" );

                                           var ref11 = new ActionReference();

                                           var idChnl = charIDToTypeID( "Chnl" );

                                           var idfsel = charIDToTypeID( "fsel" );

                                           ref11.putProperty( idChnl, idfsel );

                                       desc27.putReference( idnull, ref11 );

                                       var idNm = charIDToTypeID( "Nm  " );

                                       desc27.putString( idNm, alphaName );

                                    try {

                                    executeAction( idDplc, desc27, DialogModes.ALL );

                                    }

                                    catch(e) {}

                                  }

                                   

                                  saveAlpha("Change This Name");

                                   

                                  Thank you so much for all your help - I appreciate it.

                                  • 14. Re: Save Selection Script?
                                    Tomas Sinkunas Level 3

                                    There are many ways to skin a cat:)

                                    As long as you find a suitable tool for your task - go with it:)

                                    • 15. Re: Save Selection Script?
                                      natrev Level 3

                                      Hi RicSum,

                                       

                                      Try this code..

                                       

                                      #target photoshop
                                      $.level=0;
                                      if(documents.length>0){main();}
                                      
                                      function main(){
                                          var docRef = app.activeDocument;
                                          if(hasSelection()){
                                              var chanRef = docRef.channels.add(); 
                                              chanRef.name = "New Selection"; 
                                              chanRef.kind = ChannelType.MASKEDAREA; 
                                              docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND ); 
                                              docRef.selection.deselect();
                                              selectComponentChannel();
                                          }
                                          else{alert("Document doen't have a selection!");}
                                      }
                                      function selectComponentChannel() { 
                                          try{ 
                                              var map = {} 
                                              map[DocumentMode.GRAYSCALE] = charIDToTypeID('Blck'); 
                                              map[DocumentMode.RGB] = charIDToTypeID('RGB '); 
                                              map[DocumentMode.CMYK] = charIDToTypeID('CMYK'); 
                                              map[DocumentMode.LAB] = charIDToTypeID('Lab '); 
                                              var desc = new ActionDescriptor(); 
                                                  var ref = new ActionReference(); 
                                                  ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), map[app.activeDocument.mode] ); 
                                              desc.putReference( charIDToTypeID('null'), ref ); 
                                              executeAction( charIDToTypeID('slct'), desc, DialogModes.NO ); 
                                          }catch(e){} 
                                      }; 
                                      function hasSelection() {
                                          var doc = app.activeDocument;
                                          try{doc.selection.copy(); return true;}
                                          catch(e){ return false;}
                                      }
                                      
                                      1 person found this helpful
                                      • 16. Re: Save Selection Script?
                                        dominique filiol Level 2

                                        Bonjour

                                         

                                         

                                        more simply :

                                         

                                        "

                                        var docRef = app.activeDocument;

                                        var channelRef = docRef.activeChannels;

                                        var chanRef = docRef.channels.add();

                                         

                                        chanRef.name = "New Selection";

                                        chanRef.kind = ChannelType.SELECTEDAREA;

                                        docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND );

                                         

                                        docRef.activeChannels = channelRef;

                                        docRef.selection.deselect();

                                         

                                        "

                                        • 17. Re: Save Selection Script?
                                          RicSum Level 1

                                          Thank you everyone for all the varying solutions - I appreciate all your help