Help with Photoshop script: how to iterate through all open documents?
Hello,
I'm very new to Javascript, and scripting in Photoshop especially. I'm trying to create a script that does the following:
- Iterates through all open PSDs in Photoshop
- Checks for paths.
- Checks path at first state -- [0].
- 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.
- 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.
- 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";
};
};
};