4 Replies Latest reply on Nov 17, 2017 5:58 AM by mollys36035936

    Help with Photoshop script: how to iterate through all open documents?

    mollys36035936

      Hello,

       

      I'm very new to Javascript, and scripting in Photoshop especially. I'm trying to create a script that does the following:

       

      1. Iterates through all open PSDs in Photoshop
      2. Checks for paths.
      3. Checks path at first state -- [0].
      4. First checks if that path is a work path. If so, renames work path to "Path 1" and creates clipping path set to .2 flatness.
      5. If not work path, then checks if the path is not a clipping path. If it isn't, sets path as clipping path set to .2 and renames it "Path 1" if needed.
      6. If path is a clipping path already, sets it to .2 flatness and renames to "Path 1" if needed.

       

      Basically, the idea here is that any path at the top of the path list in the paths panel, whether a work path or a regular path, needs to become a clipping path set with .2 flatness and renamed to be Path 1.

       

      I've gotten this far (script below), but I would like to now have this script iterate through all open documents. Is there a way I can accomplish this with what I've written so far? Again, I'm very new, and I've written this script in a way that I can follow -- I'm sure there are instances where I can consolidate and remove duplicate code. If there is a way to have this iterate through all open documents, I would really appreciate any advice and help. Thank you!

       

      var docRef = app.activeDocument;
      
      if (app.documents.length > 0) {
          var n = docRef.pathItems.length;
          //If path at state [0] is a "work path," rename as "Path 1" and make into a clipping path set to .2
          if ((n > 0) && (docRef.pathItems[0].kind == PathKind.WORKPATH)) {
              docRef.pathItems.getByName("Work Path").name = "Path 1";
              docRef.pathItems[0].makeClippingPath(.2);
          //If path at state [0] is not a clipping path, make into clipping path set to .2 and rename as "Path 1," if needed.
          //"Work paths" and regular paths needed to be treated differently, so I have it check for work path prior to this step.
          //Also, I kept getting a popup to rename the path if it was already named "Path 1," the second if statement seems to avoid that.
          } else if ((n > 0) && (docRef.pathItems[0].kind != PathKind.CLIPPINGPATH)) {
              docRef.pathItems[0].makeClippingPath(.2);
              if (docRef.pathItems[0].name != "Path 1") {
                  docRef.pathItems[0].name = "Path 1";
              };
          //If path at state [0] is a clipping path, set to clipping path .2 and rename as "Path 1," if needed.
          } else if ((n > 0) && (docRef.pathItems[0].kind == PathKind.CLIPPINGPATH)) {
              docRef.pathItems[0].makeClippingPath(.2);
              if (docRef.pathItems[0].name != "Path 1") {
                  docRef.pathItems[0].name = "Path 1";
              };
          };
      };
      
        • 1. Re: Help with Photoshop script: how to iterate through all open documents?
          Chuck Uebele Adobe Community Professional & MVP

          You'll want to create a loop for going through the open docs:

           

          for(var i=0;i<app.documents.length;i++){
               var actDoc = app.documents[i];
               //More code here
               }
          
          1 person found this helpful
          • 2. Re: Help with Photoshop script: how to iterate through all open documents?
            mollys36035936 Level 1

            Hi Chuck,

             

            Thank you for your response, and sorry for my late one. I incorporated the for loop into my script above, as shown below. I tested it with 3 open PSDs. Whenever I execute, the script only applies to one open document, completes the task, then ends, and it doesn't move through the rest of the open documents. Would you have another suggestion for how this could work? Let me know if the loop has been added to my code incorrectly. Thanks again for the help.

             

            for (var i = 0; i < app.documents.length; i++) {
                var actDoc = app.documents[i];
                var docRef = app.activeDocument;
                var n = docRef.pathItems.length;
                //If path at state [0] is a "work path," rename as "Path 1" and make into a clipping path set to .2
                if ((n > 0) && (docRef.pathItems[0].kind == PathKind.WORKPATH)) {
                    docRef.pathItems.getByName("Work Path").name = "Path 1";
                    docRef.pathItems[0].makeClippingPath(.2);
                //If path at state [0] is not a clipping path, make into clipping path set to .2 and rename as "Path 1," if needed.
                //"Work paths" and regular paths needed to be treated differently, so I have it check for work path prior to this step.
                //Also, I kept getting a popup to rename the path if it was already named "Path 1," this seems to avoid that.
                } else if ((n > 0) && (docRef.pathItems[0].kind != PathKind.CLIPPINGPATH)) {
                    if (docRef.pathItems[0].name != "Path 1") {
                        docRef.pathItems[0].name = "Path 1";
                    docRef.pathItems[0].makeClippingPath(.2);
                    };
                //If path at state [0] is a clipping path, set to clipping path .2 and rename as "Path 1," if needed.
                } else if ((n > 0) && (docRef.pathItems[0].kind == PathKind.CLIPPINGPATH)) {
                    docRef.pathItems[0].makeClippingPath(.2);
                    if (docRef.pathItems[0].name != "Path 1") {
                        docRef.pathItems[0].name = "Path 1";
                    docRef.pathItems[0].makeClippingPath(.2);
                    };
                };
            };
            
            • 3. Re: Help with Photoshop script: how to iterate through all open documents?
              Chuck Uebele Adobe Community Professional & MVP

              You're not changing what is the active document. Change line 2 to this:

               

              app.activeDocument = app.documents[i]; 
              1 person found this helpful
              • 4. Re: Help with Photoshop script: how to iterate through all open documents?
                mollys36035936 Level 1

                Yesssss, thank you! This is working now! I really appreciate your help, Chuck