12 Replies Latest reply on Apr 20, 2018 3:50 AM by silvestarb50963821

    Select path using script

    Voahyeah

      Hi,

       

      Every now and than I have lots of images to save for the web (1-2k images in one time). All the images are .psd and have or path or transparent background. I would like to select the path and erease backgrount to make triming possible. The paths are named different so it is another problem because actions in photoshop select only specific name of the path. So is it possible to select path regardless of its name? Or how to write script for selecting several names of paths (ie "path 1", "path 2", "selection 1"...) I hope you understand what i want to do...

       

      I have another script which resizing images to corect size and save it for web. So when I have jpg's with white backgroound first I run action in photoshop to trim all images and then I run script to resize all images because script check if image is wider or higher...

       

      If you need more information just ask... I have CS5 version Photoshop

       

      regards,

      voahyeah

       

      Edit:

       

      For now I wrote this :

      app.activeDocument.pathItems.getByName('Path 2').makeSelection();

      app.activeDocument.selection.invert();

      app.activeDocument.selection.clear();

       

      and it select only if the name of the path is 'Path 2' else it stops...

      So when the namme is Path 2 everything is fine but I have 10-15 different names.... so it would be great to avoid name of the path. If it is possible to select the first parh (if there is more than one)

      or if it is not possible to avoid path name how to wrote if els if block for this to check all path names...?

      ... thanks

        • 1. Re: Select path using script
          yajiv_viki Level 2

          Hi Voahyeah,

           

          Can you tell me Step by step procedure you followed.

           

          I will help you.

           

           

          Regards,

          -yajiv

          • 2. Re: Select path using script
            Voahyeah Level 1

            Hi yajiv_viki

             

            I tried something like this

             

            if (app.activeDocument.pathItems.getByName('Path 1'))

            {

            app.activeDocument.pathItems.getByName('Path 1').makeSelection();

            app.activeDocument.selection.invert();

            app.activeDocument.selection.clear();

            }

             

             

            else if (app.activeDocument.pathItems.getByName('Path 2'))

            {

            app.activeDocument.pathItems.getByName('Path 2').makeSelection();

            app.activeDocument.selection.invert();

            app.activeDocument.selection.clear();

            }

             

            But it isnt working. If you want I can post whole script... but it is important to me to make selection of path (regardles of its name) invert selection and clear to make triming possible..

             

            regards

            • 3. Re: Select path using script
              yajiv_viki Level 2

              Hi Voahyeah,

               

              Here is the Code........

               

              if (app.documents.length > 0) {

                  var docRef = app.activeDocument;

                  var n = docRef.pathItems.length;

                      if((n>0)&&(docRef.pathItems[0].name!="Work path" ))  {

                           docRef.pathItems[0].makeSelection();

                           docRef.selection.invert();

                           docRef.selection.clear();

                           docRef.trim(TrimType.TOPLEFT);

                           docRef.selection.deselect();

                         }

              };

               

              Regards,

              - yajiv

              • 4. Re: Select path using script
                Voahyeah Level 1

                Hi,

                 

                Thats it

                 

                Thank you so much!! if I will have some other problems I'll ask again

                 

                regards,

                Voah

                • 5. Re: Select path using script
                  yajiv_viki Level 2

                  Hi Voah,

                   

                  Sure!

                  You are most welcome when i'm free.....

                   

                  Regards,

                  - yajiv

                  • 6. Re: Select path using script
                    silvestarb50963821

                    Hi yajiv,

                     

                    Here I'm after many years I found this topic.. Are you still on forum? I would like to add something to my script but don't know how. In section where Photoshop clears background, Photoshop stops if path is through edge od image so PS doesn't have anything to delete (no pixels are selected).. how to avoid clearing  when there is nothing to clear?

                    if (app.documents.length > 0) {

                        var docRef = app.activeDocument;

                        var n = docRef.pathItems.length;

                            if((n>0)&&(docRef.pathItems[0].name!="Work path" ))  {

                                 docRef.pathItems[0].makeSelection();

                                 docRef.selection.invert();

                                 docRef.selection.clear();

                                 docRef.trim(TrimType.TOPLEFT);

                                 docRef.selection.deselect();

                               }

                    };

                     

                    Thank you,

                    Silvestar

                    • 7. Re: Select path using script
                      r-bin Level 5

                      ...

                       

                      if (app.documents.length > 0) {
                          var docRef = app.activeDocument;
                          var n = docRef.pathItems.length;
                      
                              if((n>0)&&(docRef.pathItems[0].name!="Work path" ))  {
                                   docRef.pathItems[0].makeSelection();
                                   docRef.selection.invert();
                      
                                   try { docRef.selection.bounds; }
                                   finally {
                                       docRef.selection.clear();
                                       docRef.trim(TrimType.TOPLEFT);
                                       docRef.selection.deselect();
                                       }
                                 }
                      };
                      
                      • 8. Re: Select path using script
                        silvestarb50963821 Level 1

                        Hi r-bin,

                         

                        Thank you for answering. I tried it but Photoshop still doesn't go on when comes to image with path on edge of image (or empty path).

                         

                        Silvestar

                        • 9. Re: Select path using script
                          r-bin Level 5

                          It is not clear what it means "on the edge."

                          Could you provide a screenshot.

                           

                          Try this variant.

                          You can uncomment the catch(e) states.

                           

                          // deselect path
                          
                              var r = new ActionReference();
                              r.putClass( charIDToTypeID( "Path" ) );
                              var d = new ActionDescriptor();
                              d.putReference( charIDToTypeID( "null" ), r );
                              executeAction( charIDToTypeID( "Dslc" ), d, DialogModes.NO );
                          
                          
                          if (app.documents.length > 0) {  
                              var docRef = app.activeDocument;  
                              var n = docRef.pathItems.length;  
                            
                                  if((n>0)&&(docRef.pathItems[0].name!="Work path" ))  {  
                                       try { docRef.pathItems[0].makeSelection(); }
                                       // catch(e) { alert("Bad path: " + docRef.pathItems[0].name); }
                                       finally {  
                                               try { docRef.selection.bounds; }  
                                               // catch(e) { alert("Empty selection from path: " + docRef.pathItems[0].name); }
                                               finally {  
                                                   docRef.selection.invert();  
                                                   docRef.selection.clear();  
                                                   docRef.trim(TrimType.TOPLEFT);  
                                                   docRef.selection.deselect();  
                                                   }  
                                             }  
                                      }
                          };  
                          
                          • 10. Re: Select path using script
                            silvestarb50963821 Level 1

                            path.jpg

                            Thank you for helping me. English is not native language, so I probbably use a wrong word. Whan I mean by "on edge" is showed on image. So when you make selection with path and inverse selection to erease background you get warning "no pixels are selected". I tried this code bit it stops again. In history I see

                            Open

                            Selection Change

                            Select Inverse

                             

                            and than Photoshop stops.. because it has nothing to delete (background).

                             

                            Silvestar

                            • 11. Re: Select path using script
                              r-bin Level 5

                              OK

                               

                              // deselect path
                              
                                  var r = new ActionReference();
                                  r.putClass( charIDToTypeID( "Path" ) );
                                  var d = new ActionDescriptor();
                                  d.putReference( charIDToTypeID( "null" ), r );
                                  executeAction( charIDToTypeID( "Dslc" ), d, DialogModes.NO );
                              
                              
                              if (app.documents.length > 0) {  
                                  var docRef = app.activeDocument;  
                                  var n = docRef.pathItems.length;  
                                
                                      if((n>0)&&(docRef.pathItems[0].name!="Work path" ))  {  
                                           try { docRef.pathItems[0].makeSelection(); }
                                           // catch(e) { alert("Bad path: " + docRef.pathItems[0].name); }
                                           finally {  
                                                   try { docRef.selection.bounds; }  
                                                   // catch(e) { alert("Empty selection from path: " + docRef.pathItems[0].name); }
                                                   finally {  
                                                       docRef.selection.invert();  
                              
                                                       try { docRef.selection.bounds; }  
                                                       // catch(e) { alert("Nothing to delete with path: " + docRef.pathItems[0].name); }
                                                       finally {  
                                                           docRef.selection.clear();  
                                                           docRef.trim(TrimType.TOPLEFT);  
                                                           docRef.selection.deselect();  
                                                           }   
                                                       }  
                                                 }  
                                          }
                              };  
                              

                               

                              UPD.

                               

                              OR

                               

                               

                              // deselect path
                              
                                  var r = new ActionReference();
                                  r.putClass( charIDToTypeID( "Path" ) );
                                  var d = new ActionDescriptor();
                                  d.putReference( charIDToTypeID( "null" ), r );
                                  executeAction( charIDToTypeID( "Dslc" ), d, DialogModes.NO );
                              
                              
                              if (app.documents.length > 0) {  
                                  var docRef = app.activeDocument;  
                                  var n = docRef.pathItems.length;  
                                
                                      if((n>0)&&(docRef.pathItems[0].name!="Work path" ))  {  
                                           try { docRef.pathItems[0].makeSelection(); }
                                           // catch(e) { alert("Bad path: " + docRef.pathItems[0].name); }
                                           finally {  
                                                   try { docRef.selection.bounds; }  
                                                   // catch(e) { alert("Empty selection from path: " + docRef.pathItems[0].name); }
                                                   finally {  
                                                       docRef.selection.invert();  
                              
                                                       try { docRef.selection.bounds; }  
                                                       // catch(e) { alert("Nothing to delete with path: " + docRef.pathItems[0].name); }
                                                       finally {  
                                                           docRef.selection.clear();  
                                                           }   
                                                       }  
                                                 }  
                              
                                          docRef.trim(TrimType.TOPLEFT);  
                                          docRef.selection.deselect();  
                                          }
                              };  
                              
                              1 person found this helpful
                              • 12. Re: Select path using script
                                silvestarb50963821 Level 1

                                Hi r-bin,

                                 

                                That's it Thank you very much!

                                 

                                Now Photoshop goes on.. and I'll check later what images had "prolems"..

                                 

                                regards,

                                Silvestar