11 Replies Latest reply on Dec 4, 2009 10:29 PM by dhishok

    get object reference to selection...

    dhishok Level 1

      Hi Everyone,


      I have generating art links report. I want to include page numbers along with the art link names.

      I getting page numbers through (itemLink.parent.parent.name). If the link has grouped with 1 or 2 or more groups then it is difficult for me to get the page numbers.

       

      Expecting better way from you people. Thanks in advance.

       

      Regards

      Thiyagu

        • 1. Re: get object reference to selection...
          Loic.Aigon Adobe Community Professional

          you may need to recurse the object while the parent isn't a page

           

          var theParent = itemLink.parent.parent;

           

          while(theParent.constructor.name!="Page")

          {

               theParent= theParent.parent;

          }

          alert(theParent.name);

           

          Loic

          • 2. Re: get object reference to selection...
            dhishok Level 1

            Hi Loic,

             

            Its working fine. Thanks for your good help.

             

            Thiyagu

            • 3. Re: get object reference to selection...
              dhishok Level 1

              Hi Loic,

               

              I have checked the syntax and came to know that, it doesn't give page number, when the image placed in achored frame. Because when we try to find parent for this anchored image, the loop going in such a way " itemLink / image / rectangle / character / ".

               

              Can you please look in to this and help me out. Me too checking in my side.

               

              Thiyagu

              • 4. Re: get object reference to selection...
                Loic.Aigon Adobe Community Professional

                Hi,

                 

                That should do the trick :

                 

                var doc = app.activeDocument;
                var myPageInfo ="";
                var myLinks = doc.links;

                 

                for(i=0; i<myLinks.length; i++)
                {
                    thePage = myLinks[i].parent;
                    while(thePage.constructor.name!="Page")
                    {
                        if(thePage.constructor.name == "Character")
                        {
                            thePage = thePage.parentTextFrames[0].parent;
                        }
                        else
                        {
                            thePage = thePage.parent;
                        }
                    }
                    var msg = myLinks[i].name +" is located on page " +thePage.name;
                    myPageInfo+=msg;
                    if(i<myLinks.length-1)
                    {
                        myPageInfo+="\n";
                    }
                }
                alert(myPageInfo);

                • 5. Re: get object reference to selection...
                  dhishok Level 1

                  Hi Loic,

                   

                  Thanks lot its working fine. But the concern is when i try to write these report as .txt or .xls file. The report starts with undefined. I have checked in several ways and it can't be solved. Below is my complete script.

                   

                   

                  //--------------------

                  var data;
                  var Rpt;
                  var theParent;
                  var myDoc = app.activeDocument;
                  try{
                      var myPath = myDoc.fullName + "";
                      }
                  catch(_){alert ("Save document and try again!");exit();}
                  myPath = myPath.replace(".indd",".txt");
                  var myFile = new File (myPath);
                  myFile.open ('w');

                   

                  var g = app.activeDocument.links;
                  myCount=0;
                  for (i = 0; i < g.length; i++)
                      {
                          if ((g[i].parent.absoluteHorizontalScale!=100) || (g[i].parent.absoluteVerticalScale!=100))
                          {
                              theParent=g[i].parent;
                              while(theParent.constructor.name!="Page")
                              {
                                  if (theParent.constructor.name=="Character")
                                  {
                                      theParent=theParent.parentTextFrames[0].parent;
                                  }
                                  else
                                  {
                                      theParent=theParent.parent;
                                  }
                              }
                              Rpt="\n"+g[i].name+"    "+g[i].parent.absoluteHorizontalScale+"%    "+g[i].parent.absoluteVerticalScale+"%    "+theParent.name;
                              data += Rpt;
                              myCount++;
                          }
                      else {continue;}
                      }
                  myFile.writeln (data);
                  myFile.close('w');
                  alert ( "The Scaling Report Generated for "+myCount+" Images." );       
                  exit(0);

                   

                  //----------------------------

                   

                  Can you please look into this help me out. Thanks in advance.

                   

                  Thiyagu

                  • 6. Re: get object reference to selection...
                    Loic.Aigon Adobe Community Professional

                    Hi,

                     

                    First of all, declare the type of the data var. Inspite JS is loosely typed, data type var is not defined yet when you first try to add string to it. That's why you get the undefined.

                     

                    Secondly, I usually add the carriage return at end of the loop with a condition based on the increment value. See the code...

                     

                    Bye,

                     

                    Loic

                     

                    PS: Do you know that this kind of code has already been written dozen of times :-D ? Have a look on Indesign secrets (http://bit.ly/V7xBp) and FWIW you may have a look there two :-) :http://bit.ly/70EKUp

                     

                    Regards, Loic

                     

                     

                    //--------------------

                     

                    var data=''; //Declare data as an empty string
                    var Rpt;
                    var theParent;
                    var myDoc = app.activeDocument;
                    try{
                        var myPath = myDoc.fullName + "";
                        }
                    catch(_){alert ("Save document and try again!");exit();}
                    myPath = myPath.replace(".indd",".txt");
                    var myFile = new File (myPath);
                    myFile.open ('w');

                     

                     

                     

                    var g = app.activeDocument.links;
                    myCount=0;
                    for (i = 0; i < g.length; i++)
                        {
                            if ((g[i].parent.absoluteHorizontalScale!=100) || (g[i].parent.absoluteVerticalScale!=100))
                            {
                                theParent=g[i].parent;
                                while(theParent.constructor.name!="Page")
                                {
                                    if (theParent.constructor.name=="Character")
                                    {
                                        theParent=theParent.parentTextFrames[0].parent;
                                    }
                                    else
                                    {
                                        theParent=theParent.parent;
                                    }
                                }
                                Rpt=g[i].name+"    "+g[i].parent.absoluteHorizontalScale+"%    "+g[i].parent.absoluteVerticalScale+"%    "+theParent.name;
                                data += Rpt;
                                if(i<g.length-1) //Adds a carriage return while i < length-1 meaning we are not at the last step of the loop
                                {
                                    data +="\n";
                                }
                                myCount++;
                            }
                        else {continue;}
                        }
                    myFile.writeln (data);
                    myFile.close('w');
                    alert ( "The Scaling Report Generated for "+myCount+" Images." );      
                    exit(0);

                     

                     

                     

                    //----------------------------

                    1 person found this helpful
                    • 7. Re: get object reference to selection...
                      Dirk Becker  Level 4

                      Loic,

                       

                      have a look this approach, instead of your counter.

                      It may even save another millisecond with the shorter intermediate strings.

                       

                      Dirk

                       

                      var data = []; // empty array

                      data.push("line1");

                      data.push("line2");

                      alert(data.join("\n"));

                      • 8. Re: get object reference to selection...
                        Loic.Aigon Adobe Community Professional

                        Hi Dirk and thanks for the input.

                        I will try it as soon as possible.

                        I am quite puzzled. I may understand array speed up the script but I can figure out when it has to bes used.

                         

                        var data = []; // empty array (start of the script)

                        ...

                        for links loop

                        {

                        data.push(i-1);

                        data.push(i);

                        data.join('\n')

                        {

                         

                        is that so ?

                         

                        Loic

                        • 9. Re: get object reference to selection...
                          Dirk Becker  Level 4

                          Almost right. Apparently I was a bit too dense, let's retry with comments:

                           

                          // The strings are collected into the variable "strings", initialized to an empty array.

                          var strings = [];

                          // just some loop

                          for( i=1; i<=10; i++ ) {

                               // create one string

                               var str = "Line "+i;

                               // function Array.push() append the given string, modifying the array in place.

                               strings.push(str);

                          }

                           

                          // combine the array entries into a string, with the given separator

                          var separator = "\n";

                          // the result is returned, the array itself is unchanged.

                          var data = strings.join(separator);

                          // output

                          alert(data);

                          1 person found this helpful
                          • 10. Re: get object reference to selection...
                            Loic.Aigon Adobe Community Professional

                            Ok Dirk,

                             

                            Thanks a lot for time passed  in the explanation ;-)

                             

                            Loic

                            • 11. Re: get object reference to selection...
                              dhishok Level 1

                              Hi Loic / Dirk,

                               

                              Now its clear for me. Thanks for your kind  support. The array concept also working fine.

                               

                              Regards

                              Thiyagu