Skip to main content
Known Participant
July 29, 2015
Answered

how to apply specific operations for anchored image and non-anchored image separately..

  • July 29, 2015
  • 12 replies
  • 1197 views

i want to make some  changes for anchored and non-anchored image separately..

so first i want to find anchored and non-anchored image ...

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

Lines 50 and 69:

if(img.parent.parent.hasOwnProperty("baseline"))

Cant manage what is this loop:

//...

for(var j=0 ; j<3; j++)

    {

//...

created for?

I suggest to remove it (if agree ==> remove last line {"}") as well

Jarek

12 replies

mani4Author
Known Participant
August 1, 2015

thank u so much , i used the above code and its working fine now...

mani4Author
Known Participant
August 1, 2015

end of the script , i tired to release the anchor of all the images but indesign stop working ...

Jump_Over
Legend
August 1, 2015

Hi,

at the end of script:

//...

app.findTextPreferences = NothingEnum.NOTHING;

mImages = mDoc.allGraphics;

while ( cImage = mImages.pop() ) {

     if ( cImage.parent.parent.hasOwnProperty("baseline") )

          cImage.parent.anchoredObjectSettings.releaseAnchoredObject ();

     }

}

Jarek

mani4Author
Known Participant
August 1, 2015

if the image name is in bottom of the page and there is no enough space for image means ,

image will go out of the text frame..this is previous problem ..

but u make the image come inside the text frame ...

now problem for those images , the textwrapper is not working..

image is overlapping on the text..

Jump_Over
Legend
August 1, 2015

Hi,

Since it stay anchored it overlaps the line of text where anchor is set into - wrap values and anchor offset values go into recursive relation.

Solution I can see:

1. Grow "Y" anchor offset (line 63)

2. Release anchoring of each image finally at the end of script

But in case of images placed at bottom of textFrame and forced to keep frame bounds --> only 2nd way can help.

Jarek

mani4Author
Known Participant
August 1, 2015

if suppose user add three or more pages in the same document and those have images like this ...

again they will run this script to place the remaining images in correct location ..

or they will do any changes or editing means , they will run this script again

Jump_Over
Legend
August 1, 2015

Hi,

We started with question about the way and it is growing up, sorry.

To avoid this error script has to make some investigation before :

  • check if image parent is anchored
  • check if image name is found
  • check if its current position is correct or wrong

Each check result and their pair-combinations involves specific part of job.

One could use try-catch routine with line no 43 - but on my side it is... forcing ID to quit...

Jarek

mani4Author
Known Participant
July 31, 2015

hi, second time in the same doc..

mani4Author
Known Participant
July 31, 2015

thanks for your great support ... this above code working fine for first time..

but when i run this script second time , it showing error message like this ,,

Jump_Over
Legend
July 31, 2015

Hi,

Second time in the same doc? Other doc?

Notice you can use "UNDO" before repeated script run.

Jarek

mani4Author
Known Participant
July 31, 2015

And one more thing is, if that picture name text is not found means ,

i dont want to remove that picture ..just ignore that picture..

mani4Author
Known Participant
July 31, 2015

yes , its not working with standalone images..

this if condition got true , and its exit from the script..

if (!mImageChar.hasOwnProperty("baseline")) { 

       alert ("Anchor image named: " + imgname + " somewhere in text"); 

       exit(); 

       }

Jump_Over
Legend
July 31, 2015

Hi,

Standalone images moved as well but if text not found => placed in 'collector' textFrame

OUTPUT ==> all images anchored and - if name found - placed nearby

app.doScript('testImageAlign()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "imageAlign");

function testImageAlign() {

  var

       mDoc = app.activeDocument,

       mStandaloneImgCollector = mDoc.textFrames.add(),

       mImages = mDoc.allGraphics, mImageName,

       cImage,

       cImageRec, cImageChar,

       mFound;

  app.findTextPreferences = NothingEnum.NOTHING;

  while ( cImage = mImages.pop() )

       if (!cImage.parent.parent.hasOwnProperty("baseline")) {

            cImage.parent.select();

            app.cut();

            mStandaloneImgCollector.insertionPoints[-1].contents = "\r";

            mStandaloneImgCollector.insertionPoints[-1].select();

            app.paste();

            }

  mImages = mDoc.allGraphics;

  while ( cImage = mImages.pop() ) {

       cImageRec = cImage.parent;

       cImageChar = cImageRec.parent;

       mImageName = cImage.itemLink.name,

       app.findTextPreferences.findWhat = mImageName;

       mFound = mDoc.findText();

       if (mFound.length)

            cImageChar.move(LocationOptions.BEFORE,mFound[0]);

       else

            alert ("Text: " + imgname + " not found in doc");

       cImageRec = cImage.parent;

       cImageChar = cImageRec.parent;

       with (cImageRec) {

            textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

            textWrapPreferences.textWrapOffset = [1,1,1,1];

            fit(FitOptions.frameToContent);

            with (anchoredObjectSettings) {

                 anchoredPosition = AnchorPosition.ANCHORED;

                 anchorPoint = AnchorPoint.TOP_LEFT_ANCHOR;

                 horizontalAlignment = HorizontalAlignment.LEFT_ALIGN;

                 horizontalReferencePoint = AnchoredRelativeTo.COLUMN_EDGE;

                 verticalAlignment = VerticalAlignment.TOP_ALIGN;

                 verticalReferencePoint = VerticallyRelativeTo.LINE_BASELINE;

                 anchorYoffset = 3;

                 pinPosition = true;

                 }

            }

       }

  if ( !confirm("Keep 'collector' textFrame with not found images?") )

       mStandaloneImgCollector.remove();

  }

Jarek

mani4Author
Known Participant
July 31, 2015

but the concept of the script is ,

we have many pictures in the indesign document and we have those pictures names anywhere in the paragraphs..

so i iterate all pictures one by one and finding the each picture name in paragraphs..

both are equal , i'm moving that picture to the picture name text below...

so, i have to move pictures from one page to another page ..thats why i removed those anchors..

Eg:  page 1

The idea of human nature and bodily experience as something fixed in our biology has been central to Western European and American ideas about humanity for more than a century. The fundamental problem with this sort of understanding is that it simply cannot account for who gets sick, who succeeds in school or business, or who is straight and who is gay. We examine why in the next two sections. (Figure 12.1 shown below)

here Figure 12.1 is the picture name and picture is in page 3 so i have to move the image to this Figure 12.1 name below..

now its moving correctly and problem is ,

its going out of the text frame  and how to fit the image into the text frame

Jump_Over
Legend
July 31, 2015

mani@pixel napisał(-a):

but the concept of the script is ,

we have many pictures in the indesign document and we have those pictures names anywhere in the paragraphs..

so i iterate all pictures one by one and finding the each picture name in paragraphs..

both are equal , i'm moving that picture to the picture name text below...

so, i have to move pictures from one page to another page ..thats why i removed those anchors..

All above OK.

Provided code is moving image with relation to found text (to the other page as well)

Only problem is what to do with standalone images (not anchored)

Just check it

Jarek

mani4Author
Known Participant
July 31, 2015

thank u so much ..i changed those lines and now its working fine.. but still i have another one problem in this script..

if i move the image at the bottom of the page , its going out of the frame ..

how to fit the image into the text frame.

Jump_Over
Legend
July 31, 2015

Hi,

Since image container anchor is released there is no longer relation to textFrame body.

To keep it you need to compare/recalculate geometricBounds again;

But I suggest to change logic and keep image containers anchored.

Didi you consider to move img.parent.parent?

Check it with this part of code

// it should move 1st anchored image into right position and pin it with relation to textFrame bounds and found text position

app.doScript('testImageAlign()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "imageAlign");

function testImageAlign() {

  var

       mDoc = app.activeDocument,

       imgname = mDoc.allGraphics[0].itemLink.name,

       mImage = mDoc.allGraphics[0],

       mImageRec = mImage.parent,

       mImageChar = mImageRec.parent,

       mFound;

  if (!mImageChar.hasOwnProperty("baseline")) {

       alert ("Anchor image named: " + imgname + " somewhere in text");

       exit();

       }

  app.findTextPreferences = NothingEnum.NOTHING;

  app.findTextPreferences.findWhat = imgname;

  mFound = mDoc.findText();

  if (mFound.length)

       mImageChar.move(LocationOptions.BEFORE,mFound[0]);

  else {

       alert ("Text: " + imgname + " not found in doc");

       exit();

       }

  with (mImageRec) {

       textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

       textWrapPreferences.textWrapOffset = [1,1,1,1];

       fit(FitOptions.frameToContent);

       with (anchoredObjectSettings) {

            anchoredPosition = AnchorPosition.ANCHORED;

            anchorPoint = AnchorPoint.TOP_LEFT_ANCHOR;

            horizontalAlignment = HorizontalAlignment.LEFT_ALIGN;

            horizontalReferencePoint = AnchoredRelativeTo.COLUMN_EDGE;

            verticalAlignment = VerticalAlignment.TOP_ALIGN;

            verticalReferencePoint = VerticallyRelativeTo.LINE_BASELINE;

            anchorYoffset = mImageChar.leading;

            pinPosition = true;

            }

       }

  }

mani4Author
Known Participant
July 31, 2015

this is my code ....

ImageAlign();

function ImageAlign()

{

   

       if (app.documents.length == 0)

      {

        alert('Please have an Indesign document before running this script.');

        return;

      }

 

     var doc = app.activeDocument;

     var img = doc.allGraphics;

     var allframes = app.documents[0].stories.everyItem().textContainers;

  

     var finds,myText,myfind,start,end,myPage,imgPage, imgname,replace,dot,extern;

  

       var myImgBounds = new Array;

       var myCurrentObjectALL = new Array;

       var myRootXMLElement = doc.xmlElements.item(0);

     app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

  

    for(var j=0 ; j<3; j++)

    {

       

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

    {

   

    imgname = img.itemLink.name;

    app.findTextPreferences.findWhat = imgname;

    finds = doc.findText();

    

       if (finds.length > 0)

      {

        myPage = finds[0].parentTextFrames[0].parentPage.name;

        imgPage = img.parentPage.name;

        myFrame = finds[0].parentTextFrames[0].geometricBounds;

        myText = finds[0].createOutlines(false);

        gb= myText[0].geometricBounds;

        end = gb[0];

        start = myFrame[1];

        myText[0].remove();

      

if(imgname == finds[0].contents)

    {

       

   if(myPage == imgPage)

   {

    if(img.parent.anchoredObjectSettings.anchoredPosition) \\ here i need to write the correct condition to check image is anchored or not

    {

     img.parent.anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED;

     img.parent.anchoredObjectSettings.releaseAnchoredObject();

     img.parent.move([start, end+1.5]);

     img.textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

     img.textWrapPreferences.textWrapOffset = [1,1,1,1];

     img.fit(FitOptions.frameToContent);

     }

    else

    {

     img.parent.move([start, end+1.5]);

     img.textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

     img.textWrapPreferences.textWrapOffset = [1,1,1,1]; 

     img.fit(FitOptions.frameToContent);    

     }

     }

else

   {

     if(img.parent.anchoredObjectSettings.anchoredPosition)  \\ here i need to write the correct condition to check image is anchored or not

     {

       

     img.parent.anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED; 

     img.parent.anchoredObjectSettings.releaseAnchoredObject();

     img.parent.move(doc.pages[Number(myPage)-1]); 

     img.parent.move([start, end+1.5]);

     img.textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

     img.textWrapPreferences.textWrapOffset = [1,1,1,1];

     img.fit(FitOptions.frameToContent);

     }

     else

     {

     img.parent.move(doc.pages[Number(myPage)-1]); 

     img.parent.move([start, end+1.5]);

     img.textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

     img.textWrapPreferences.textWrapOffset = [1,1,1,1];

     img.fit(FitOptions.frameToContent);  

      }

     }

  }

}

    else {

        //alert('There is no match for '+extern);

    }

for(var j = 0; j < img.length; j++)

            {

                var myGrapSingle = img.parent;

                myGrapSingle.fit(FitOptions.frameToContent);

            }

app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

}

}

}

Jump_Over
Jump_OverCorrect answer
Legend
July 31, 2015

Hi,

Lines 50 and 69:

if(img.parent.parent.hasOwnProperty("baseline"))

Cant manage what is this loop:

//...

for(var j=0 ; j<3; j++)

    {

//...

created for?

I suggest to remove it (if agree ==> remove last line {"}") as well

Jarek