3 Replies Latest reply on Dec 3, 2009 3:03 PM by [Jongware]

    Deleting graphics and layers.

    lanejd

      Okay, I am really a n00b, but I can't figure out how to delete graphic items (graphic lines, rectangles, etc) using JS. I also can't figure out how to delete a layer. I am really new to JS and coding in general. I am still struggling a little with syntax and hierarchies, but I am getting there.

       

      Here is my situation:

       

      I am using a script to create a number of documents. I am able to create new layers and graphics in these documents, but, for some reason, I can't remove them after I have created them (I know, the easy answer is just not to create items in the first place if you are just going to delete them later, but that is a topic for another discussion).

       

      I am sure most of you are reading this and saying, "DURH," so, please bear with me.

       

      Thanks

        • 1. Re: Deleting graphics and layers.
          [Jongware] Most Valuable Participant

          The function is called remove() for both graphics and layers. (Presumable because "delete" is already taken.)

          1 person found this helpful
          • 2. Re: Deleting graphics and layers.
            lanejd Level 1

            Please bear with me, I am trying to understand how this works.

             

            So how would you code that if I am trying to delete the layer that was created when the document was first created. How do I identify a layer when I didn't create it? I know that it's name will be 'Layer 1' so how can I use that to delete that layer.

             

            On a similar note, I wouldn't have to delete the layer if I could just rename it, but I still have the problem of identifying a layer I didn't create. This has been a difficult concept for me to understand, and I have read through the documentation a couple of times.

             

            Any help would be great.

            • 3. Re: Deleting graphics and layers.
              [Jongware] Most Valuable Participant

              app.activeDocument.layers.item("Layer 1") returns a pointer to any named item (similar to "swatches.item("Red")", fonts.item("Arial"), etc.).

               

              Perhaps it is easiest to save a reference to your layer when creating it:

               

              myLayer = app.activeDocument.layers.add();

              myLayer.name = "I will be removed shortly";

              myLayer.layerColor = UIColors.GOLD;

              myLayer.visible = false// hides it

              myLayer.locked = true// locks it


              .. more stuff here ..


              myLayer.remove(); // gets rid of it