3 Replies Latest reply on Jul 28, 2013 3:19 AM by Muppet Mark

    Place/Import file to visible Layer

    toeknee0506

      I have been working on a script to place a psd file onto only one visible layer (document has multiple hidden layers).

       

      This java code has worked so far:

       

      var doc = app.activeDocument;

      var onFile = File("C:/Users/Desktop/Desktop/ExportedArtboard-01.psd");

      var mydoc = doc.placedItems.add();

      mydoc.file = onFile;

      mydoc.embed();

       

      however this works with only the top layer visible (eye turned on in Ai). if top most layer isn't visible i get "target layer cannot be modified" error.

      I've searched a lot in the forums and cant seem to figure this last code out if some can please help me make the code import the psd file to a layer that is currently visible (closests to top layer) would be great.

      For instance say i have 1,2,3 layers. layer 1 (top most) and 3 (bottom most) would be hidden, layer 2 (middle layer) would be visible and then running the script would place the psd file on to layer 2.

       

      Thank you!!!

        • 1. Re: Place/Import file to visible Layer
          Muppet Mark Level 5

          You can change the documents Active Layer… Probably more useful if you wanted to paste something…

           

          Anyhows why don't you place the psd using the layer as the target object…

           

           

          #target illustrator
          
          var doc = app.activeDocument;
          
          var psdFile = File( Folder.desktop + '/Testing.psd' );
          
          var lay = doc.layers.getByName( 'Layer 2' );
          
          //doc.activeLayer = lay; // If you were to paste…
          
          var myPsd = lay.placedItems.add();
          
          myPsd.file = psdFile;
          
          myPsd.embed();
          

           

          Here you see I just got the layer by name and used it for placement…

           

          You will need to loop the layers to find the top most thats visible…

           

          BTW Check for both locked and visible to change the contents…

          • 2. Re: Place/Import file to visible Layer
            toeknee0506 Level 1

            thank you so much! your code worked for me!

            you are the best!

            • 3. Re: Place/Import file to visible Layer
              Muppet Mark Level 5

              A simple forward looking loop testing for both properties should find you the first useable unknown layer if there is one…

               

              #target illustrator
              
              var doc = app.activeDocument;
              
              var psdFile = File( Folder.desktop + '/Testing.psd' );
              
              var count = doc.layers.length;
              
              for ( var i = 0; i < count; i++ ) {
                
                        if ( ! doc.layers[i].locked && doc.layers[i].visible ) {
                
                                  doc.activeLayer = doc.layers[i];
              
                                  var myPsd = doc.layers[i].placedItems.add();
                                   
                                  myPsd.file = psdFile;
                                   
                                  myPsd.embed();
                
                                  break;
                
                        };
              
              };
              

               

              You could add an else to this to warn if there was none…