10 Replies Latest reply on Sep 17, 2012 2:01 AM by purushoth_j

    Find path name

    purushoth_j

      Hello All,

       

      I am new to photoshop scripting and i am learning. I want to write a code to "find the particular path with a alert box whether its is available in the file or not" i was trying and confused. i used "else" method but i couldn't get it right.

       

      #target photoshop

       

       

      var doc = app.activeDocument;

       

      if (app.activeDocumen.pathItems.length > 0) {

      app.activeDocument.pathItems[0].name = "Path 1"

      } else {

       

          alert ("There is no path name: Path 1", title, errorIcon)

           }

       

      also tried this one

       

      #target photoshop

       

       

      var doc = app.activeDocument;

       

      if ( doc.pathItems.length > 0 ) {

       

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

       

       

      } else {

       

          alert ("There is no path name: Path 1", title, errorIcon)

           }

       

      pls help me!

        • 1. Re: Find path name
          Michael L Hale Level 5

          Here is one way to do what I think you are asking.

           

          try{
              var myPath = app.activeDocument.pathItems.getByName("Path 1");
              alert('The pathItem: '+myPath.name+' was found');
          }catch(e){
              alert('Could not find pathItem');
          }
          
          • 2. Re: Find path name
            Muppet Mark Level 5

            Mike, you beat me to this with the same answer… Only when I tried a test with no path of this name it does not catch the error… My mind is fuzzy at best but I thought this did work with the ESTK now or have I lost the plot completely? No such element…

            • 3. Re: Find path name
              Michael L Hale Level 5

              That is strange Mark. For me it works in either ESKT or straight from Photoshop. It even works if there is no open document.

              • 4. Re: Find path name
                Muppet Mark Level 5

                Mike, I recall all be it vaugely now that went I switched from AppleScript to ExtendScript that I could never work out try/catch it just never worked. The code turned out to be fine when run in app but NOT when testing in the ESTK… I could have sworn this was fixed when swapping from CS2 to CS5. But then it just done it again… I really should get some note book for these things… It may work for the OP too but neither yours or mine run here… Anyhows all someCollection.getByName() should be in a try/catch…

                • 5. Re: Find path name
                  Michael L Hale Level 5

                  Mark, if it runs in Photoshop but not ESKT I would bet that you do not have 'Do Not Break on Guarded Exceptions' checked under the Debug menu. With that unchecked ESKT will stop even in a try/catch block.

                  • 6. Re: Find path name
                    Muppet Mark Level 5

                    Mike, I didn't know what that actually meant and yes it's unchecked… I've checked it but don't recall touching it before… Works as expected now… That's why we is a muppet…

                    • 7. Re: Find path name
                      purushoth_j Level 1

                      WOWWWW! i got it! thanks alot mike! its great!

                      • 8. Re: Find path name
                        purushoth_j Level 1

                        hi mike,

                        pls can you explain the difference between "if" & "try" "catch" functions. and y should we use "catch(e)" 

                        • 9. Re: Find path name
                          Michael L Hale Level 5

                          A single if statement would not work for this task. Because pathItem.getByName will thorw an error if that pathItem does not exists you can not use getByName in an if statement. I know that some think try/catch sould not be used and here is a way to test for a path by name without the try/catch.

                           

                          if( app.documents.length>0 && app.activeDocument.pathItems.length>0 ){
                              for(var pathItemIndex = 0;pathItemIndex< app.activeDocument.pathItems.length; pathItemIndex++){
                                  if(app.activeDocument.pathItems[pathItemIndex].name == 'Path 1' ) {
                                      alert('Path found');
                                  }
                              }
                              alert('This document does not have a path with that name');
                          }else{
                              alert('Needs a document with pathItems');
                          }
                          

                           

                          I used the try/catch because I think it is faster. With a try statement you need to have catch to handle the error or finally to run code regardles of an error. You can use both catch and finally if needed.

                           

                          The e part of catch(e) is the error object. If you want more details about the error you could do something like this.

                           

                          try{
                              var myDoc = app.activeDocument;
                              var myPath = myDoc.pathItems.getByName("Path 1");
                              alert('The pathItem: '+myPath.name+' was found');
                          }catch(errorObject){
                              alert(errorObject.message+': '+errorObject.line);
                          }
                          

                           

                          In this case the message would be the same if there is no document or pathItem with that name. But the line number is different. And most of the time the error message is more helpful than 'No such element'.

                           

                          I hope that explains why the difference between using if or try/catch to do something like this. Let me know if you have more questions.

                          • 10. Re: Find path name
                            purushoth_j Level 1

                            thanq mike now i got cleared!