5 Replies Latest reply on Mar 24, 2014 7:48 PM by RobertKyle

    Replacing object with library asset help

    Mattias O



      working on a script that is meant to replace some text with an asset from my library. I've got it to almost working correctly.

      It replaces almost all of the text but always skips one or more. Any ideas why?




      //Find/Change to smaller font to avoid overset

      app.findTextPreferences = NothingEnum.nothing;

      app.changeTextPreferences = NothingEnum.nothing;

          app.findTextPreferences.appliedFont = "DIN";

          app.findTextPreferences.fontStyle = "Medium";

          app.findTextPreferences.pointSize = 7.6;

              app.changeTextPreferences.pointSize = 4;

                  app.activeDocument.changeText ();




      myDoc = app.activeDocument;

      myDocItems = app.activeDocument.textFrames;

      myLib = app.libraries.item("LaReSS13.indl");

      myAsset = myLib.assets.item("Skinnsköld");

      myObject = app.activeDocument.textFrames.item("SKINN"); 





      //Set label "SKINN"

      for (i=0; i<myDocItems.length; i++)


              if (myDocItems[i].contents == "SKINN")

                  myFinds=myDocItems[i].label = "SKINN";





      //Replace "SKINN" label with library asset "Skinnsköld"

      for (k=0; k<myFinds.length; k++)









      //Move Inserted Library Assets

      myObject.move (undefined, ["-6 pt", "-11 pt"]);

        • 1. Re: Replacing object with library asset help
          Jump_Over Level 5



          Which version of ID?


          No need to use name, label and contents all together - one of them is good enough.


          Try to notice anything about ommited textFrames (their location, page, layer, contents, anchored status..., any)


          Your code need both cleaning and correction, however more info can be helpful.



          • 2. Re: Replacing object with library asset help
            RobertKyle Level 2

            This is just a guess but try running your loop in reverse:


            for (k=myFinds.length-1; k >= 0; k--)


            Sometimes a loop will change the array it's working on and the result is usually something like "some items get skipped."


            I wonder why one loop to insert labels and a second loop to replace them. Why not find 'em and fix 'em at the same time.


            Also, is it your intent to move only the last inserted asset? The move line seems to be outside the second loop.

            • 3. Re: Replacing object with library asset help
              Mattias O Level 1

              Its ID 4.


              Tried to clean up the code but to no success.

              I'm kinda new at javscripting and I can't seem

              to get it to work if I change anything. I dunno

              why it won't work with just labels, name or

              contents but it won't, atleast not for me

              • 4. Re: Replacing object with library asset help
                Mattias O Level 1

                Reversed the loop, same thing happened.

                The move thing works for all replaced assets.

                • 5. Re: Replacing object with library asset help
                  RobertKyle Level 2

                  first off, app.activeDocument.textFrames seems to miss any text frames that are part of a group. If that's a factor in your document, start with myDoc.allPageItems and test the .constructor.name


                  What I was suggesting might look like this:


                  for (i=0; i<myDocItems.length; i++)


                          if (myDocItems[i].contents == "SKINN")


                              myDocItems[i].move (undefined, ["-6 pt", "-11 pt"]);



                  Just one loop: If the contents of one text frame match "SKINN", it is replaced by the library asset and the text frame is moved. You could assign a label at the same time, if you really need one.


                  Your original code, seems to me might suffer from a number of problems:


                  1) You set the variable myObject to text frames labeled "SKINN" before you actually apply the label. Maybe that works, it just seems counter-intuitive.


                  2) Your second loop is based on the array myFinds, but you use the index from myFinds to grab an object in the array app.selection. Again, it's possible that those indexes are the same, but I would want to do a lot of testing before I relied on it.