I’m not quite sure how to phrase the syntax for this. Ultimately I want to delete all path in a document that don’t have the name Ex1 or Ex2 or Ex3.
This is what I have so far
deletepaths();
function deletepaths() {
if (!documents.length) return;
var doc = activeDocument;
for (var a = doc.pathItems.length - 1; a > -1; a--) {
if (doc.pathItems[a].name == "Ex1") {
return true;
}
if
else(doc.pathItems[a].name == "Ex2") {
return true;
} else doc.pathItems[a].remove();
}
}
Here is another script I was using prior that works, but doesn’t allow me to exlude multiple “path” names
#target Photoshop
main();
function main(){
if(!documents.length) return;
var doc = activeDocument;
for(var a = doc.pathItems.length-1;a>-1;a--){
if(doc.pathItems[a].name != "Ex1") doc.pathItems[a].remove();
}
}
2
Answers
This should do it:
Sometimes, formatting your code in a more readable fashion can help diagnose the issue.
This function approaches the problem in a slightly different manner — we keep an array of names that we want to save, and if the path’s name isn’t in that array, then we call
.remove()
on it.I’m also returning a
boolean
(in the event that the element tied to yourdocuments
variable doesn’t exist) so that the function feeds something useful back to you, instead of returningundefined
.I’m not familiar with the library you’re using, but try this out. I’m happy to work with you to get a solution, just leave a comment on this answer.
Use this function:
Working Example:
http://codepen.io/bddenhartog/pen/VLReKa
Note that the code in the pen has been modified to accommodate the example.