9 Replies Latest reply on Jul 7, 2009 2:04 AM by Murugan_vdm

    place anchor image by finding the text...

    dhishok Level 1

      Hi scripting Gurus & everyone,

       

      We are working with InDesign CS3 files. And we need to place two different icons as anchor objects in the margins by finding respective callouts. If the callout is "[Pair-Icon]" means the P-icon (i.e. image) that has to be placed as anchored object. If the callout is "[Group-Icon]" means the G-icon (i.e. image) that has to be placed as anchored object. The icons have approximately 3 pica height and width. It will always comes in left side of the margin in both verso and recto pages. Doing automation for anchor object placements is new to us.

       

      Can any one help me for this.

       

      Thanks in advance.

      Thiyagu

        • 1. Re: place image as anchor object by finding the text...
          dhishok Level 1

          Hi scripting gurus,

           

          I have tried a little bit to place the "Pair_Icon" art as anchor object by replacing the text "[P-ICON]" in the document. But nothing happened after running the script. Same like the Group_Icon has to be placed by replacing the text "[G-ICON]. Can anyone look into this help me out.

          Find the script below,

           

          //===================

          if(app.documents.length>0)
          {
              var myDoc=app.activeDocument;
              var myText=["[P-ICON]"];
              var myText1=["[G-ICON]"];
               var Lb1=["Group_Icon"];
              var Lb2=["Pair_Icon"];
              var counter=0;
              var Xoffset=63;
              var Yoffset=4;
              var Dist=12;
              var myNewUnits = myDoc.viewPreferences;
              myNewUnits.verticalMeasurementUnits = MeasurementUnits.points;
              myNewUnits.horizontalMeasurementUnits = MeasurementUnits.points;
              myNewUnits.rulerOrigin=RulerOrigin.pageOrigin;
              myDoc.zeroPoint=[0,0];
             
              for(var j=0;j<myText.length;j++)
              {
                  for(var i=myDoc.stories.length-1;i>=0;i--)
                  {
                      var Stry=myDoc.stories[i];
                      var Para=Stry.contents;
                      for(var k=0;k<Para.length;k++)
                      {
                          try{
                              if(Para[k]==myText)
                              {
                                  app.selection=1851876449;
                                  Para[k].select();
                                  app.selection=1851876449;
                                  var y=app.libraries[0].assets;
                                  var Chi=Lb1[j];
                                  var x=y.item(Chi).placeAsset(myDoc);
                                  Tex=x[0];
                                  Tex.contents="A";
                                  Tex.contents.select();
                                  app.paste();
                                  var RemLen=Tex.characters.length;
                                  Tex.characters[RemLen-1].remove();
                                  Tex.fit(FitOptions.frameToContent);
                             
                                  var Obj=Para[k].pageItems[0];
                 
                                  Obj.anchoredObjectSettings.anchoredPosition = AnchorPosition.anchored;
                                  Obj.anchoredObjectSettings.spineRelative=true;
                                  Obj.anchoredObjectSettings.anchorPoint = AnchorPoint.topLeftAnchor;
                                  Obj.anchoredObjectSettings.horizontalReferencePoint=AnchoredRelativeTo.pageMargins;
                                  Obj.anchoredObjectSettings.anchorXoffset=0;
                                  Obj.anchoredObjectSettings.anchorYoffset=0;
                                  Obj.anchoredObjectSettings.verticalReferencePoint=VerticallyRelativeTo.capheight;
                                                     
                                  counter++;           
                              }
                          }
                          catch(e)
                          {
                              alert(e);
                          }
                      }
                  }
              }
              alert(counter+" DONE");
          }

          //=======================

           

          Thanks in advance

          Thiyagu

          • 2. Re: place image as anchor object by finding the text...
            Peter Kahrel Adobe Community Professional & MVP

            It seems to me that your approach is not correct. First of all, create an object style for the anchored rectangles so that you can fine-tune the items later. More importantly, there's no need to cycle through your stories and scan the text for the two text strings. Use inDesign's findText() method. This gives you all occurrences of your text. You insert the library items at the first insertion point of each found instance. Or better yet, use the findGrep() method so that you need just one pass through the document. Something like this:

             

            // find options to be set
            app.findGrepPreferences = null;
            app.findGrepPreferences.findWhat = "\\[[PG]-ICON\\]";
            var myFound = app.activeDocument.findText (true);
            for (var i = 0; i < myFound.length; i++)
                {
               switch (myFound[i].contents[1])
                  {
                  case "P": myFound[i].insertionPoints[0]. //insert P icon
                  case "G": myFound[i].insertionPoints[0]. //insert G icon
                  }
               }

             

            Peter

            1 person found this helpful
            • 3. Re: place image as anchor object by finding the text...
              dhishok Level 1

              Hi Peter,

               

              Thanks for your kindful suggestions and for your support. I have tried but it throws me a error. Because i got confused and i am sure that i did some mistake in calling the library items. Please help me for this. I am still learning and want to learn more things from you. Find the script below,

               

              if(app.documents.length>0)

              {

              // find options to be set

              app.findGrepPreferences = null;

              app.findGrepPreferences.findWhat = "\\[P-ICON\\]";

              var myFound = app.activeDocument.findGrep (true);

              var counter = i+1;

              var myDoc=app.activeDocument;

              var y=app.libraries[0].assets;

              var Lb1=["Pair_Icon"];

              var Lb2=["Group_Icon"];

              var Chi=Lb1;

              var x=y.item(Chi).placeAsset(myDoc);

               

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

                 {

                 switch (myFound[i].contents[1])

                    {

                    case "P": myFound[i].insertionPoints[0].insert.Lb1;//insert P icon

                    case "G": myFound[i].insertionPoints[0].insert.Lb2; //insert G icon

                    }

                 }

              var Obj=myFound[i].contents[1];

               

              Obj.anchoredObjectSettings.anchoredPosition = AnchorPosition.anchored;

              Obj.anchoredObjectSettings.spineRelative=true;

              Obj.anchoredObjectSettings.anchorPoint = AnchorPoint.topLeftAnchor;

              Obj.anchoredObjectSettings.horizontalReferencePoint=AnchoredRelativeTo.pageMargins;

              Obj.anchoredObjectSettings.anchorXoffset=0;

              Obj.anchoredObjectSettings.anchorYoffset=0;

              Obj.anchoredObjectSettings.verticalReferencePoint=VerticallyRelativeTo.capheight;

               

              counter++;          

              }

              alert(counter+" DONE");

               

              Thanks in advance

              Thiyagu

              • 4. Re: place image as anchor object by finding the text...
                Peter Kahrel Adobe Community Professional & MVP

                Here is the full script. It assumes that your library is open, that it contains the two named assets, and that you have an object style named "icon". You may also want to set the find options (include footnotes, locked stories, etc.)

                 

                The script does this: find all instances of [P-ICON] and  [G-ICON] (which is [[PG]-ICON] in GREP). Cycle through the array of found items (findGrep(true) returns the found items in reverse order), inserting an asset at the last insertion point of each found instance. Each found instance (i.e. each myFound[i].contents) is the name of an asset.

                 

                This marginal stuff is good fun. I've just clobbered together a system for dynamic margin notes (http://www.kahrel.plus.com/indesign/sidenotes.html).

                 

                Peter

                 

                 

                #target indesign
                
                myDoc = app.documents[0];
                myObjectStyle = myDoc.objectStyles.item ("icon");
                myLib = app.libraries[0];
                
                app.findGrepPreferences = null;
                app.findGrepPreferences.findWhat = "\\[[PG]-ICON\\]";
                var myFound = myDoc.findGrep (true);
                try
                   {
                   for (i = 0; i < myFound.length; i++)
                      {
                      myIcon = myLib.assets.item (myFound[i].contents).placeAsset (myFound[i].insertionPoints[-1])[0];
                      myIcon.appliedObjectStyle = myObjectStyle;
                      myFound[i].remove ();
                      }
                   }
                catch (e) {alert (e.message)}
                • 5. Re: place image as anchor object by finding the text...
                  dhishok Level 1

                  Hi Peter,

                   

                  The script is working fine. Thanks for your valuable and ecellent support.
                  I feel very happy now. Also i am going to try this for placing ANNO text with anchor frame.

                   

                  Thanks & Regards
                  Thiyagu

                  • 6. Re: place image as anchor object by finding the text...
                    JonasEttemo

                    Hi Peter and everyone else,

                    I have a similar problem as Thiyagu but I need to pace an anchored frame in font of an xml-tag in the text. I have tried to use the search engine but it hasn’t search and replace support for xml-files.
                    Do you have any clue how to do that?

                    Thanks in advance

                      Jonas

                    • 7. Re: place image as anchor object by finding the text...
                      Peter Kahrel Adobe Community Professional & MVP

                      Jonas,

                       

                      It is indeed the case that you can't find XML tags using Find/Change (though you can specify them in the Change Format pane). Instead, you have to go through your XML structure looking for your tag and insert a frame at each node's insertion pont. Here is a working example. The XPath part of the script (in effect most of it) was posted here a while ago by Dirk Becker -- I hope he doesn't mind me culling it.

                       

                      The script searches for tags named "sidenote" and applies the object style "sidenote". Frames are made 30mm wide. These three things can be changed to different values in the first function call.

                       

                      Peter

                       

                      #target "InDesign"
                      
                      anchor_xml ("sidenote", app.activeDocument.objectStyles.item ("sidenote"), "30mm")
                      
                      function anchor_xml (node_name, object_style, width)
                        {
                        var root  = app.activeDocument.xmlElements[0];
                        var xpath = "//"+node_name;
                        var node  = null;
                        try {
                          var proc  = app.xmlRuleProcessors.add ([xpath]);
                          var match = proc.startProcessingRuleSet (root);
                          while( match!=undefined )
                            {
                            node = match.element;
                            anchored_frame (node, object_style, width);
                            match = proc.findNextMatch();
                            }
                          } catch( ex ) {
                            alert(ex);
                          } finally {
                          proc.endProcessingRuleSet();
                          proc.remove();
                          }
                        }
                      
                      function anchored_frame (xml, ostyle, w)
                        {
                        var anchor = xml.storyOffset.textFrames.add ();
                        anchor.appliedObjectStyle = ostyle;
                        anchor.contents = xml.contents;
                        anchor.geometricBounds = [0, 0, "20mm", w];
                        anchor.fit (FitOptions.frameToContent);
                        }
                      
                      • 8. Re: place image as anchor object by finding the text...
                        JonasEttemo Level 1

                        Hi Peter,


                        The script is working perfectly. Thanks for your support.

                        Thanks! / Jonas

                        • 9. Re: place image as anchor object by finding the text...
                          Murugan_vdm

                          Hi Peter.,

                           

                                 I have tried insert new Anchored Object in a textFrame using Javascript indesign CS2, i couldnt complete. Give me your suggestion for insert Anchored Object in indesign CS2. Thanks in Advance.