23 Replies Latest reply on Aug 1, 2015 1:44 AM by mani@pixel

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

    mani@pixel Level 1

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

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

       

        • 1. Re: how to apply specific operations for anchored image and non-anchored image separately..
          Jump_Over Level 5

          Hi,

           

          Iterate through app.activeDocument.allGraphics      ==> array

          If particular graphic.parent.parent.constructor.name == "Character"      ==> this graphic's container is anchored

           

          Jarek

          • 2. Re: how to apply specific operations for anchored image and non-anchored image separately..
            mani@pixel Level 1

            actually what i want to know is ,

            i have many pictures in my indesign document and all are anchored as inline..

            now i want to move pictures from one page to another page ...

            so i iterate all pictures and release the anchor of all the pictures..

            so that i can move the pictures from one page to another page..

            but after releases the anchor of all pictures , when i run the script its showing error like..

            Snap 2015-07-31 at 12.33.01.png


             

            • 3. Re: how to apply specific operations for anchored image and non-anchored image separately..
              Jump_Over Level 5

              Hi,

               

              Hard to say without looking into entire code.

              Basically you cannot check this property for object which anchor were released.

              Order of iterating is important as well.

               

              Did you consider to move image.parent.parent? ==> it means to move character and keep graphic anchored

               

              Jarek

              • 4. Re: how to apply specific operations for anchored image and non-anchored image separately..
                mani@pixel Level 1

                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[i].itemLink.name;

                    app.findTextPreferences.findWhat = imgname;

                    finds = doc.findText();

                 

                    

                       if (finds.length > 0)

                      {

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

                        imgPage = img[i].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[i].parent.anchoredObjectSettings.anchoredPosition) \\ here i need to write the correct condition to check image is anchored or not

                    {

                     img[i].parent.anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED;

                     img[i].parent.anchoredObjectSettings.releaseAnchoredObject();

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

                     img[i].textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

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

                     img[i].fit(FitOptions.frameToContent);

                     }

                    else

                    {

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

                     img[i].textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

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

                     img[i].fit(FitOptions.frameToContent);    

                     }

                     }

                else

                   {

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

                     {

                       

                     img[i].parent.anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED; 

                     img[i].parent.anchoredObjectSettings.releaseAnchoredObject();

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

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

                     img[i].textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

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

                     img[i].fit(FitOptions.frameToContent);

                     }

                     else

                     {

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

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

                     img[i].textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;

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

                     img[i].fit(FitOptions.frameToContent);  

                      }

                     }

                  }

                }

                    else {

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

                    }

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

                            {

                                var myGrapSingle = img[j].parent;

                                myGrapSingle.fit(FitOptions.frameToContent);

                            }

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

                }

                }

                }

                • 5. Re: how to apply specific operations for anchored image and non-anchored image separately..
                  Jump_Over Level 5

                  Hi,

                   

                  Lines 50 and 69:

                  if(img[i].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

                  • 6. Re: how to apply specific operations for anchored image and non-anchored image separately..
                    mani@pixel Level 1

                    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.

                    • 7. Re: how to apply specific operations for anchored image and non-anchored image separately..
                      Jump_Over Level 5

                      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;
                                  }
                             }
                        }
                      
                      • 8. Re: how to apply specific operations for anchored image and non-anchored image separately..
                        mani@pixel Level 1

                        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

                        • 9. Re: how to apply specific operations for anchored image and non-anchored image separately..
                          Jump_Over Level 5

                          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

                          • 10. Re: how to apply specific operations for anchored image and non-anchored image separately..
                            mani@pixel Level 1

                            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(); 

                                   }

                            • 11. Re: how to apply specific operations for anchored image and non-anchored image separately..
                              Jump_Over Level 5

                              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

                              • 12. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                mani@pixel Level 1

                                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 ,,

                                Snap 2015-07-31 at 18.05.31.png

                                • 13. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                  Jump_Over Level 5

                                  Hi,

                                   

                                  Second time in the same doc? Other doc?

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

                                   

                                  Jarek

                                  • 15. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                    mani@pixel Level 1

                                    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..

                                    • 16. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                      Jump_Over Level 5

                                      Hi,

                                       

                                      standalone images ignored (if name not found)

                                      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")) {
                                          // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
                                          mImageName = cImage.itemLink.name,
                                          app.findTextPreferences.findWhat = mImageName;
                                          mFound = mDoc.findText();
                                      
                                          if (!mFound.length){
                                        alert ("Text: " + mImageName + " not found in doc");
                                        continue;
                                        }
                                          // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
                                                  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: " + mImageName + " not found in doc");
                                        continue;
                                        }
                                          // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
                                            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;
                                                      }
                                                  }
                                            }
                                        mStandaloneImgCollector.remove();
                                        app.findTextPreferences = NothingEnum.NOTHING;
                                        }
                                      
                                      
                                      

                                       

                                      In case of 2nd (or more) run in the same doc ==> tell me what is the reason to do it?

                                       

                                      Jarek

                                      • 17. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                        mani@pixel Level 1

                                        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

                                        • 18. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                          Jump_Over Level 5

                                          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

                                          • 19. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                            mani@pixel Level 1

                                            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..

                                            • 20. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                              Jump_Over Level 5

                                              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

                                              • 21. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                                mani@pixel Level 1

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

                                                • 22. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                                  Jump_Over Level 5

                                                  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

                                                  • 23. Re: how to apply specific operations for anchored image and non-anchored image separately..
                                                    mani@pixel Level 1

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