9 Replies Latest reply on Feb 3, 2011 3:19 PM by Lev Ger

    getting the page of a link

      hello forum

      I use applescript to automate indesign.
      now I've to do a job, where I make textframes on a new layer, same size and position as image/grafic-objects on other layers.
      to do this, I check the links of the document. if there are more than one page, I need the page-number/id to position my textframe.

      I've tried a lot of things to find out the page of a link, but nothing is perfect.
      does anybody know an absolutly sure way to get the page number of a link - for example the to get the number in the link palette of the application.

      InDesign CS3 on Mac OS X 10.4.11

      thank you in advance.
        • 1. Re: getting the page of a link
          Kasyan Servetsky Level 5
          Here is my way of finding where the link is. I had to fall back upon JavaScript. But it doesn’t work for inline graphics; I just didn’t need it.
          on whereIsTheLink(theLink)
          
          tell application "Adobe InDesign CS3"
          set myLinkIndex to index of theLink
          set myJsScript to "#target indesign
          myDoc = app.activeDocument;
          myLink = myDoc.links[ \"" & myLinkIndex & "\" – 1];
          myMessage = whereIsTheLink(myLink);
          function whereIsTheLink(theLink){
          while (theLink.constructor.name != 'Application') {
          theLink = theLink.parent;
          myHierarchy += theLink.constructor.name + '|';
          }
          if (myHierarchy.lastIndexOf('|Character|Story|Document|Application|') != -1)
          {
          return 'inline object';
          }
          else if (myHierarchy.lastIndexOf('|Page|MasterSpread|Document|Application|') != -1)
          {
          return 'master page';
          }
          else if (myHierarchy.lastIndexOf('|MasterSpread|Document|Application|') != -1)
          {
          return 'pasteboard of master page';
          }
          else if (myHierarchy.lastIndexOf('|Page|Spread|Document|Application|') != -1)
          {
          return 'page';
          }
          else if (myHierarchy.lastIndexOf('|Spread|Document|Application|') != -1)
          {
          return 'pasteboard';
          }
          }"
          • 2. Re: getting the page of a link
            Kasyan Servetsky Level 5
            P.S. Once you know that the link is on a page, you can easily find the page name (aka page number). Take note that this function works for images nested in groups (no matter how deep).


            #target indesign
            myDoc = app.activeDocument;
            myLink = myDoc.links[0];
            myMessage = whereIsTheLink(myLink);
            alert("My link is on " + myMessage);

            function whereIsTheLink(theLink){
               while (theLink.constructor.name != 'Application') {
                  if (theLink instanceof Page) {
                     var myPageName = theLink.name;
                  }
               theLink = theLink.parent;
               myHierarchy += theLink.constructor.name + '|';
               }
               if (myHierarchy.lastIndexOf('|Character|Story|Document|Application|') != -1)
                  {
                  return 'inline object';
                  }
               else if (myHierarchy.lastIndexOf('|Page|MasterSpread|Document|Application|') != -1)
                  {
                  return 'master page';
                  }
               else if (myHierarchy.lastIndexOf('|MasterSpread|Document|Application|') != -1)
                  {
                  return 'pasteboard of master page';
                  }
               else if (myHierarchy.lastIndexOf('|Page|Spread|Document|Application|') != -1)
                  {
                  return 'page ' + myPageName;
                  }
               else if (myHierarchy.lastIndexOf('|Spread|Document|Application|') != -1)
                  {
                  return 'pasteboard';
                  }
            }
            • 3. Re: getting the page of a link
              Kasyan Servetsky Level 5
              Here I wrote a new function, which returns the page number for inline graphics too.


              #target indesign
              myDoc = app.activeDocument;
              myLink = myDoc.links[0];
              myMessage = whereIsTheLink(myLink);
              if (!isNaN(parseInt(myMessage))){
                 myMessage = 'page ' + myMessage;
              }
              alert("My link is on " + myMessage);

              function whereIsTheLink(theLink){
                 while (theLink.constructor.name != 'Application') {
                    if (theLink instanceof Page) {
                       var myPageName = theLink.name;
                       return myPageName;
                    }
                    if (theLink instanceof Character) {
                       var thePage = whereIsTheLink(theLink.parentTextFrames[0]);
                       if (thePage != undefined) return  thePage;
                    }
                 theLink = theLink.parent;
                 myHierarchy += theLink.constructor.name + '|';
                 }
               
                 if (myHierarchy.lastIndexOf('|Page|MasterSpread|Document|Application|') != -1)
                    {
                    return 'master page';
                    }
                 else if (myHierarchy.lastIndexOf('|MasterSpread|Document|Application|') != -1)
                    {
                    return 'pasteboard of master page';
                    }
                 else if (myHierarchy.lastIndexOf('|Spread|Document|Application|') != -1)
                    {
                    return 'pasteboard';
                    }
              }
              • 4. Re: getting the page of a link
                Level 1
                dear kasyan

                thanks a lot for your info.
                so this means, also js cannot grab the page/page nr. directly.

                I use this handler/function in as:

                on myPageChecker(myObject)
                tell application "Adobe InDesign CS3"
                set myTemp to parent of myObject
                repeat while class of myTemp ≠ page
                if class of myTemp ≠ story then
                set myTemp to parent of myTemp
                else
                set myTemp2 to item 1 of text containers of myTemp
                repeat while class of myTemp2 ≠ page
                set myTemp2 to parent of myTemp2
                end repeat
                return id of myTemp2
                end if
                end repeat
                return id of myTemp
                end tell
                end myPageChecker

                works fine - also for inline graphics.
                just hoped there might be a "hidden feature" to get the page property directly.
                • 5. Re: getting the page of a link
                  Level 1
                  this might look better...

                  on myPageChecker(myObject)
                  tell application "Adobe InDesign CS3"
                  set myTemp to parent of myObject
                  repeat while class of myTemp ≠ page
                  if class of myTemp ≠ story then
                  set myTemp to parent of myTemp
                  else
                  set myTemp2 to item 1 of text containers of myTemp
                  repeat while class of myTemp2 ≠ page
                  set myTemp2 to parent of myTemp2
                  end repeat
                  return id of myTemp2
                  end if
                  end repeat
                  return id of myTemp
                  end tell
                  end myPageChecker
                  • 6. Re: getting the page of a link
                    (Simon_Paterson) Level 1
                    Not sure if this is of any help to you now. I find this function to be reliable because it deals with images of any type, grouped, inline or otherwise, as well as those on masterpages. (If your images are never on a masterpage, then you can simply remove that part of the function, which makes this quite compact.) Hope this helps.

                    >on GetPageOfLink(theLink)

                    tell application "Adobe InDesign CS3"

                    tell document 1

                    set theLinkId to id of theLink

                    -- Image on normal document page

                    repeat with p from 1 to (count of pages)

                    tell page p

                    set theGraphicsLinksIDs to (id of item link of all graphics) as list

                    if theLinkId is in theGraphicsLinksIDs then return id

                    end tell

                    end repeat

                    -- Image on master page

                    repeat with ms from 1 to (count of master spreads)

                    repeat with mp from 1 to (count of pages of master spread ms)

                    tell page mp of master spread ms

                    set theGraphicsLinksIDs to (id of item link of all graphics) as list

                    if theLinkId is in theGraphicsLinksIDs then return id

                    end tell

                    end repeat

                    end repeat

                    end tell

                    end tell

                    end GetPageOfLink
                    • 7. Re: getting the page of a link
                      Lev Ger Level 1

                      Dear Kasyan,

                       

                      Your function for getting the page number of a link was working great for me, but it no longer works with CS5. I have tried to tweak it, but have been unsuccessful, I believe it is something to do with the introduction of the "parentPage" property to page items in CS5. I would appreciate your help to get your function to work for both CS4 and 5.

                       

                      Thanks in advance.

                      • 8. Re: getting the page of a link
                        Harbs. Level 6

                        Try replacing this:

                        theLink = theLink.parent;

                        with something like this:

                         

                        if(theLink.hasOwnProperty("parentPage")){

                            theLink = theLink.parentPage;

                        } else {

                        theLink = theLink.parent;

                        }

                        Harbs

                        • 9. Re: getting the page of a link
                          Lev Ger Level 1

                          Thank you for your awesome solution!

                           

                          It worked with a slight amendment:

                           

                           

                          if(theLink.hasOwnProperty("parentPage") && theLink.parentPage != null){
                              theLink = theLink.parentPage;
                          } else {
                          theLink = theLink.parent;
                          }
                          

                           

                           

                          I added the " && theLink.parentPage != null", otherwise theLink is set to null if the frame is on the pasteboard.

                           

                          This works for CS4 and CS5!

                           

                          Thanks again!