11 Replies Latest reply on Dec 8, 2014 1:20 AM by Laubender

    Align Inline Equation to Baseline

    Sudha K Level 1

      Hi,

       

         I found this code to align line objects to baseline from this forum.  But its throwing error (null object).

       

      var fp = app.selection[0].graphics[0].itemLink.filePath;

      var f = File(fp);

      f.open('r');

      var fs = f.read();

      f.close();

      var baseline = fs.match(/%%Baseline\s+(\S+)/)[1];

      app.selection[0].anchoredObjectSettings.anchorYoffset = -Number(baseline);

       

       

      Screen shot 2014-12-05 at 9.47.50 AM.png

       

      Can anyone guide me...

       

      Thanks in advance,

      Sudha K

        • 1. Re: Align Inline Equation to Baseline
          Sudha K Level 1

          Hi,

           

            Sorry.. When the document contains missing links its throwing this error.

          • 2. Re: Align Inline Equation to Baseline
            Laubender Adobe Community Professional & MVP

            @Sudha – what did you expect?


            The script is trying to open the linked EPS file and reading out the first PostScript comment starting with the string "%%Baseline" to extract the substring after a whitespace followed by a substring that is not a whitespace.

             

            What can go wrong with that:
            1. There is no EPS file properly linked; so it cannot be opened.

            2. If it is properly linked, the EPS file does not contain a readable PostScript comment

            3. The PostScript comment is there, but does not contain a substring that could be converted to a number.

             

            So you should test for all the above cases and react with an alert or something.

             

            Uwe

            • 3. Re: Align Inline Equation to Baseline
              Sudha K Level 1

              Hi,

               

                 I thought its work for all images without checking whether its missing or not. But the image is missing, as you said we cant read tat file.

               

                 So we can check the 1st point by missing links... how to check the point 2 and 3???

               

              - Sudha K

              • 4. Re: Re: Align Inline Equation to Baseline
                Laubender Adobe Community Professional & MVP

                @Sudha – do something like that:

                 

                try{
                    var baseline = fs.match(/%%Baseline\s+(\S+)/)[1];
                    }catch(e){
                        //Give alert:
                        alert("No match found:"+"\r"+e.message);
                        //Exit and end the script at this point:
                        exit();
                        };
                

                 

                Uwe

                • 5. Re: Align Inline Equation to Baseline
                  Sudha K Level 1

                  Hi,

                   

                  Ok thank you...

                   

                  is it for both point 2 and 3???

                  • 6. Re: Re: Align Inline Equation to Baseline
                    Laubender Adobe Community Professional & MVP

                    @Sudha – it is basically for the case where fs.match() is causing null.

                     

                    If a substring is returned you could test for NaN (Not a Number) before assigning it as a value for the anchorYoffset property of anchoredObjectSettings of your selection.

                     

                    if(isNaN(Number(baseline))){
                        alert("Found substring cannot be converted to a number.")
                        exit();
                        };
                    

                     

                     

                    Uwe

                    • 7. Re: Align Inline Equation to Baseline
                      Sudha K Level 1

                      HI,

                       

                      Thank you...

                       

                      When im using this logic, some of the inline objects are aligned correctly to baseline. Some is not aligned correctly... its moved down to baseline.  Don't know the reason...

                       

                      Before run the code the image was like this.

                      bef.png

                       

                      After run the code the image is like this.

                      after.png

                       

                      I run the code for all objects in document.  Some objects aligned correctly. Some having this problem.  How can i fix this issue??

                       

                      How the value baseline in postscript comment is related to inline value of anchor object settings?? Can u pls explain this???

                       

                      - Sudha K

                      • 8. Re: Align Inline Equation to Baseline
                        Laubender Adobe Community Professional & MVP

                        @Sudha – no, I cannot explain this, because I do not know the PostScript code in your EPS files. For me it seems, that assigning just the negative of a number that is used in a PostScript comment may be too simplistic to get what you want.

                         

                        Uwe

                        • 9. Re: Align Inline Equation to Baseline
                          Laubender Adobe Community Professional & MVP

                          Oh, and looking at your example: Why did you run the code on this?

                          As far as I can see the position of the equation was good before running the code…

                           

                          Uwe

                          • 10. Re: Align Inline Equation to Baseline
                            Sudha K Level 1

                            Hi,

                             

                              InDesign document having lots of equations moved to up position.  I used this script to move that equation to baseline without any check. So, Some of the Images are align correctly but some equations are moved to down not in baseline. This is my prob.  I dono wat to do..

                             

                            - Sudha K

                            • 11. Re: Re: Align Inline Equation to Baseline
                              Laubender Adobe Community Professional & MVP

                              I think, you have to find out on what occasion an equation is aligned right and on what occasion it is not.

                               

                              Since you can know what is the value of "baseline" after reading the EPS, you can do a check with an EPS that is freshly placed. Maybe this test will always involve visually looking at the line, maybe you can find a rule how these EPSes react on a given situation…

                               

                              $.writeln(baseline);
                              

                               

                              would write the value of baseline to the JavaScript Console of the ESTK.

                               

                              var myCharacterThatIsRepresentingTheEPS = app.selection[0].parent.getElements()[0];
                              

                               

                              would be the character that is representing the placed EPS in the text.
                              And you could check for any baseline shift or other properties that influence the position of the placed graphic.

                              Point size would come to my mind.

                               

                              Oh: And make sure, you do not run the script more than one time on a placed graphic.

                              Maybe the better idea is to change the baseline of the character that holds the EPS:

                               

                              app.selection[0].parent.getElements()[0].baselineShift = -1;
                              

                               

                              Instead of changing the anchored object settings.

                               

                              Uwe