skip to Main Content

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


  1. This should do it:

    function deletepaths() {
        if (!documents.length) return;
        var doc = activeDocument;
        for (var a = doc.pathItems.length - 1; a > -1; a--) {
            if (!doc.pathItems[a].name.match(/^Ex[1-3]$/)) {
                doc.pathItems.splice(a,1);
            }
        }
    }
    
    //Testing
    var documents = [{}, {}],
        activeDocument = {"pathItems": [{name: "Ex1"}, {name: "Ex2"}, {name: "Someothername"}, {name: "Exasd1"}, {name: "asdEx1"}, {name: "Ex2"}, {name: "Ex3"}, {name: "Ex2"}, {name: "Exasd1"}, {name: "Exasd1"}, ]};
    deletepaths();
                                        
    var body = document.getElementsByTagName("body")[0];
    activeDocument.pathItems.forEach(function(a,b,c){
        body.innerHTML += a.name + "<br>";    
    });
    Login or Signup to reply.
  2. 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 your documents variable doesn’t exist) so that the function feeds something useful back to you, instead of returning undefined.

    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:

    function deletepaths()
    {
      if (!documents.length) return false;
    
      var doc = activeDocument;
      var save = ["Ex1", "Ex2", "Ex3"];
    
      for (var x = 0; x < doc.pathItems.length; x++)
      {
        if (save.indexOf(doc.pathItems[x].name) == -1)
        {
          doc.pathItems[x].remove();
        }
      }
    }
    

    Working Example:

    http://codepen.io/bddenhartog/pen/VLReKa
    Note that the code in the pen has been modified to accommodate the example.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search