8 Replies Latest reply on Mar 20, 2015 4:08 AM by Jump_Over

    How do I loop through selected links?

    peter11391697

      Hi!

      I'm looking for clues on how to run a script on selected links (selected in the Links window).

      Like this sample script that runs an alert when links are missing:

       

      var aDoc = app.activeDocument;

       

      for(var n = 0; n < aDoc.links.length; n++) {

           var aLink = aDoc.links[n];

       

           if (aLink.status == LinkStatus.linkMissing) {

                alert(aLink.filePath);

           }

      }

       

       

      Is there something like LinkStatus.linkSelected?

      Or some other way for the script to sort out the selected links.

       

      Any input would be extremely appreciated!

      Thanks!

       

      /Peter

        • 1. Re: How do I loop through selected links?
          peter11391697 Level 1

          So, I got a little further. But this script only works with links selected in the documents or the layer tab.

          Could anyone point me to how it's done if the link is selected in the links tab?

          If its not possible just tell me. Or if you have any other solution I'd be happy to hear that as well.

           

          Current script:

           

          var sel = app.activeDocument.properties.selection;

           

          for(var n = 0; n < sel.length; n++) {

              if(sel[n] == '[object Image]'){

                  // Directly selected image

                  aLink=sel[n].itemLink.filePath;

              }

              else{

                  // Rectangle selected

                  aLink=sel[n].graphics[0].itemLink.filePath;

              }

          alert(aLink);

          }

          • 2. Re: How do I loop through selected links?
            Jump_Over Level 5

            Hi,

             

            As far as I know - not possible to read selection from Link Panel (any UI Panel).

            Other solution?

            - to create a dialog with listBox (each list item is a link read from a doc) and "multiselect" property set as "true".

            This way user can select a set of links and script can do a job based on selection.

             

            Jarek

            • 3. Re: How do I loop through selected links?
              peter11391697 Level 1

              Thanks for the reply!

              I'll look inte the listBox option. That looks like the way to go.

               

              Peter

              • 4. Re: How do I loop through selected links?
                Loic.Aigon Adobe Community Professional

                We should definitively harass Adobe to be offered such accesses. Of course one may write a c library and deal with that but it's far outside my skills fro what I am concerned.

                Just saying

                • 5. Re: How do I loop through selected links?
                  peter11391697 Level 1

                  @Loic yes. You are absolutley right. I'm really surprised that its not accessable already.

                  But if things go as I plan, my scripted links window will be better than the one that comes with InDesign. That is if I can work out how to format it right.

                  I'm really new to scripting Adobe programs, and the options are quite limited at times.

                  • 6. Re: How do I loop through selected links?
                    Jump_Over Level 5

                    Hi Peter,

                     

                    You could start with this:

                     

                    function mDialog()
                    {
                      var
                      u = undefined,
                      mRes = [], cItem, cLink,
                      mLinksNames = app.activeDocument.links.everyItem().name,
                      // ||||||||||||||||| dialog body |||||||||||||
                      mWin = new Window ("dialog", "Link manager", u, {closeButton: false}),
                      mList = mWin.add("listbox", u, mLinksNames, {multiselect: true}),
                      buttLine = mWin.add("group");
                    
                      buttLine.add("button", u, "OK", {name: "OK"});
                      buttLine.add("button", u, "Cancel", {name: "Cancel"});
                      // ||||||||||||||||| output |||||||||||||
                      if (mWin.show() == 1) {
                      var listSelection = mList.selection;
                      while ( cItem = listSelection.shift() ) {
                      cLink = app.activeDocument.links.itemByName(cItem.text);
                      mRes.push(cLink);
                      }
                      return mRes;
                      }
                      else exit();
                      }
                    
                    var mSelected = mDialog();
                    

                    Variable mSelected is empty array or array with selected links

                    You could use is for further code.

                     

                    Some question is what to do with repeated links (list unique only?).

                     

                    Jarek

                    • 7. Re: How do I loop through selected links?
                      Laubender Adobe Community Professional & MVP

                      @Jarek – hm, "list unique only" will give no flexibility. I'd list all the links.

                      And: Uniqueness in "name" is not uniqueness in "linked files".

                       

                      Could mean big trouble not to show every instance of a link.

                       

                      Uwe

                      • 8. Re: How do I loop through selected links?
                        Jump_Over Level 5

                        Hi,

                         

                        I would modify line #17:

                        while (listSelection && cItem = listSelection.shift() ) {
                        
                        

                         


                        cause empty selection leads to error.


                        Uwe,

                        In case of unique - troubles could depends on what are you going to do with output and how.

                        If we talk about dialog list only - it is much more comfortable to user just to select a link name once instead of - let say - 125 clicks (in theory).

                         

                        But maybe a better idea is to return just an array of names and convert it to useful list of object (one name into many links) outside of mDialog function()

                         

                        Jarek