4 Replies Latest reply on Oct 30, 2008 5:56 PM by (Olav_Kvern)

    CS3 Script Label not found

      Hi all,
      Very much a newbie with InDesign Scripting, but I've managed to build a script that does exactly what I want - almost.

      It opens a document, looks for a Script Label, and when it's found, it does something with the item's contents. The only problem is that some of the documents don't have a Script Label, and should therefore be ignored. I've taken this code from one of the supplied examples:

      var myPageItems = myPage.pageItems.item("myScriptLabel");
      var myString = myPageItems.contents;

      If there are no items called "myScriptLabel", the second line will generate an error. How do I test whether the myPageItems actually contains any pageItems? I've tried all the obvious things I can think of, but have had no success. I'm sure the answer has to be a simple one.

      Thanks,
      Bruce
        • 1. Re: CS3 Script Label not found
          Level 1
          Hi Bruce,

          myPageItems would be an array, so check the length of the array. If the array is not empty, iterate through the array using a for loop:

          if(myPageItems.length != 0){
          for(var myCounter = 0; myCounter < myPageItems.length; myCounter++){
          myPageItem = myPageItems[myCounter];
          //Now do whatever you want with the page item...
          }
          }

          Thanks,

          Ole
          • 2. Re: CS3 Script Label not found
            Level 1
            Hi Ole, and thanks for your quick response.

            This was something I tried, however I get the message:
            Object does not support the property or method 'length'.

            Any clues?

            Thanks again,
            Bruce
            • 3. Re: CS3 Script Label not found
              Level 1
              I've decided to run a loop, like this:<br /><br />for(j=0;j<myPage.pageItems.length;j++){<br />     if(myPage.pageItems.item("myScriptLabel") == myPage.pageItems.item(j)){<br />          founditem = true;<br />          break;<br />     }<br />}<br /><br />This works okay, but I would welcome a simpler solution if there is one.<br /><br />Thanks,<br />Bruce
              • 4. Re: CS3 Script Label not found
                Level 1
                Hi Bruce,

                Hm. I thought that this would return an array, but it doesn't--it returns a single page item, even if a.) there are no objects with that label, b.) there is one object with that label, and c.) there are multiple objects with that label. If all you want to do is set some property of all of the returned page items, this is a good thing. But if you want to do something else, it makes it more difficult. Here's a basic script that shows how to deal with it. It's probably quicker than looping through every page item.

                var myDocument = app.documents.add();
                
                var myRectangle = myDocument.pages.item(0).rectangles.add({label:"myLabel"});
                var myRectangle = myDocument.pages.item(0).rectangles.add({label:"myLabel"});
                var myObjectList = myDocument.pages.item(0).pageItems.item("myLabel");
                try{
                //If the object is not valid, the following line will throw an error.
                switch(myObjectList.id.constructor.name){
                  case("Number"):
                   //Do something with the object.
                   alert("Object ID is: " + myObjectList.id);
                   break;
                  case("Array"):
                   //Do something with each object (you can use objectByID to get a
                   //reference to each object).
                   for(var myCounter = 0; myCounter < myObjectList.id.length; myCounter++){
                    alert("Object ID is: " + myObjectList.id[myCounter]);
                   }
                   break;
                }
                }
                catch(myError){alert("eh")}

                Thanks,

                Ole