8 Replies Latest reply on Feb 10, 2016 4:23 AM by LeoMari

    Copy anchored object with specific object style to new layer

    Axenza

      Hi there

       

      i'm new to scripting and been trying to adjust the script below, but without result.

       

      This script i found on Indisnip:

       

      var myItems = app.activeDocument.allPageItems;

      var myDoc = app.activeDocument;

      var foundObjects = Array();

       

      for(var i = 0; i < myItems.length; i++){

          if(myItems[i].parent instanceof Character){foundObjects.push(myItems[i]);}

      }

       

      if(foundObjects.length > 0){

          try{var myDestLayer = myDoc.layers.add({name:"Anchor/Inline Duplicates"});}

          catch(_){myDestLayer = myDoc.layers.item("Anchor/Inline Duplicates");}

          for(var i = 0; i < foundObjects.length; i++){

              var newDuplicate = foundObjects[i].duplicate(myDestLayer);

              newDuplicate.geometricBounds = foundObjects[i].geometricBounds;

              try{newDuplicate.graphics[0].geometricBounds = foundObjects[i].graphics[0].geometricBounds;}catch(_){}

          }

      }

       

      It copy's all the anchored items to a new layer 'Anchor/Inline Duplicates'.

      But i only want the anchored objects with a specific object style to be copied. Something like 'appliedObjectStyles.name == "OS-name".

      Can someone help me with that?

       

      thx

      Arne

        • 1. Re: Copy anchored object with specific object style to new layer
          [Jongware] Most Valuable Participant

          (untested!) Change the lines that iterate over found objects and add inline objects, starting at line #5, to the following

           

          for(var i = 0; i < myItems.length; i++){
          if(myItems[i].appliedObjectStyle.name == "OS-name" &&
          myItems[i].parent instanceof Character){foundObjects.push(myItems[i]);}
          }
          

           

          -- the if-clause has been changed here; the second line is added.

           

          To highlight code in a post, you can use the "Advanced" editor, so you can apply Syntax hi-lighting. It makes Javascript ever so slightly more readable

          • 2. Re: Copy anchored object with specific object style to new layer
            Axenza Level 1

            Hi Jongware

             

            thx for this quick reply!! Wow, that was fast

            The script works fine!

             

            many thanx

            • 3. Re: Copy anchored object with specific object style to new layer
              Axenza Level 1

              Hmmm,

               

              strange things are happening here.

              In my testfile, wich is a 4 page spread document with +/- 15 anchors everything works fine.

               

              When i run the script in a larger document (+50pag's) with lots of anchors, all the anchors are copied to a new layer, but the anchors from the left page are copied to the right page. The y-coördinates are correct, but the x-coördinates should be -210mm (for an A4 design).

              The anchors from the right page are OK.

               

              I also ran into this error in another document.

              When i deleted one specific layer, the error disappeared.

              Schermafbeelding 2012-08-21 om 08.15.27.png

              Any idea what's happening here?

              • 4. Re: Copy anchored object with specific object style to new layer
                [Jongware] Most Valuable Participant

                Axenza wrote:

                 

                Any idea what's happening here?

                 

                I have an idea that could cause this: "appliedObjectStyle.name" fails when there is no object style applied. Try this:

                 

                alert (app.selection[0].appliedObjectStyle.name);

                 

                on a named and on an unnamed object style -- the latter will show your error.

                 

                You can probably circumvent it by adding another test in the initial loop:

                 

                for(var i = 0; i < myItems.length; i++){
                if(myItems[i].appliedObjectStyle && myItems[i].appliedObjectStyle.name == "OS-name" &&
                myItems[i].parent instanceof Character){foundObjects.push(myItems[i]);}
                }
                

                 

                (still untested )

                • 5. Re: Copy anchored object with specific object style to new layer
                  Axenza Level 1

                  Sorry, nothing changes.

                  The anchored items from the left page are still copied to the right page.

                   

                  My code is now:

                   

                  var myDoc = app.activeDocument;
                  var myItems = app.activeDocument.allPageItems;
                  var foundObjects = Array();
                  
                  for(var i = 0; i < myItems.length; i++){
                  if(myItems[i].appliedObjectStyle && myItems[i].appliedObjectStyle.name == "TF-oplossingen" &&
                  myItems[i].parent instanceof Character){foundObjects.push(myItems[i]);}
                  }
                  
                  if(foundObjects.length > 0){
                      try{var myDestLayer = myDoc.layers.add({name:"oplossingen"});}
                      catch(_){myDestLayer = myDoc.layers.item("oplossingen");}
                      for(var i = 0; i < foundObjects.length; i++){
                          var newDuplicate = foundObjects[i].duplicate(myDestLayer);
                          newDuplicate.geometricBounds = foundObjects[i].geometricBounds;
                          try{foundObjects[i].remove(); newDuplicate.graphics[0].geometricBounds = foundObjects[i].graphics[0].geometricBounds;}catch(_){}
                      }
                  }
                  

                   

                  I also tried this:

                  I opened a indd doc with 328 pages with only styled text, nothing more.

                  With a quick find/change i've added 1300 anchored objects with a specific Objectstyle name.

                  And this error shows up:

                  Schermafbeelding 2012-08-21 om 13.55.54.png

                  Maybe this is happening because there are to much objects?

                  • 6. Re: Copy anchored object with specific object style to new layer
                    [Jongware] Most Valuable Participant

                    Axenza wrote:

                     

                    Sorry, nothing changes.

                    The anchored items from the left page are still copied to the right page.

                     

                    (Echh! Really busy with other things, so only the second part of your post registered!)

                     

                    Maybe your ruler settings are different for those documents? Coordinates are measured per active ruler settings, so a script that works on a per-page base, where the top left of the page is always at [0,0], might fail on a two-page spread view where the top left of the left page is actually on [-210,0]. Try if things change for the better with a bit of fiddling around with the ruler settings.

                    • 7. Re: Copy anchored object with specific object style to new layer
                      Axenza Level 1

                      No problem.

                      Put the rules unit in my indd document on spread and that did it

                       

                      Thx for your time!