Copy link to clipboard
Copied
Hello,
I'm very new to Javascript, and scripting in Photoshop especially. I'm trying to create a script that does the following:
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";
};
};
};
You're not changing what is the active document. Change line 2 to this:
app.activeDocument = app.documents; |
Copy link to clipboard
Copied
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;
//More code here
}
Copy link to clipboard
Copied
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;
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);
};
};
};
Copy link to clipboard
Copied
You're not changing what is the active document. Change line 2 to this:
app.activeDocument = app.documents; |
Copy link to clipboard
Copied
Yesssss, thank you! This is working now! I really appreciate your help, Chuck
Copy link to clipboard
Copied
As the change in forum software mangled previous scripts by removing key items such as variable references from loops, the missing code is [i], as in:
app.activeDocument = app.documents[i];