Skip to main content
Known Participant
May 29, 2009
Answered

place anchor image by finding the text...

  • May 29, 2009
  • 1 reply
  • 6108 views

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

This topic has been closed for replies.
Correct answer Peter Kahrel

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.contents[1])

      {

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

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

      }

   }

var Obj=myFound.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


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.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.contents).placeAsset (myFound.insertionPoints[-1])[0];
      myIcon.appliedObjectStyle = myObjectStyle;
      myFound.remove ();
      }
   }
catch (e) {alert (e.message)}

1 reply

dhishokAuthor
Known Participant
May 30, 2009

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;
            var Para=Stry.contents;
            for(var k=0;k<Para.length;k++)
            {
                try{
                    if(Para==myText)
                    {
                        app.selection=1851876449;
                        Para.select();
                        app.selection=1851876449;
                        var y=app.libraries[0].assets;
                        var Chi=Lb1;
                        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.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

Peter Kahrel
Community Expert
Community Expert
May 30, 2009

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.contents[1])
      {
      case "P": myFound.insertionPoints[0]. //insert P icon
      case "G": myFound.insertionPoints[0]. //insert G icon
      }
   }

Peter

dhishokAuthor
Known Participant
May 30, 2009

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.contents[1])

      {

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

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

      }

   }

var Obj=myFound.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