6 Replies Latest reply on Jul 2, 2013 3:18 PM by RobertKyle

    InDesign library: How is this possible?

    RobertKyle Level 2

      I've been using this snippet to test whether a given library is open and, if so, to bring it to the front:

       

      if (app.libraries.itemByName("Bob.indl").isValid == true) {

          app.libraries.itemByName("Bob.indl").associatedPanel.visible=true;

          }

       

      But every once in a while the second line throws an error: "Null is not an object."

       

      It looks like it is somehow possible for the library to be valid without the panel being open. I just saw such a case: app.libraries.everyItem().name included "Bob.indl" but the panel was not visible and the name did not appear in the Windows menu.

       

      I don't think there is a "isValid" property for the panel, but app.libraries.everyItem("Bob.indl").assts.length returned 0 (the actual library has many more than zero assets).

       

      So my first instinct is to test for the existence of the library object, then ask for a count of assets before trying to bring it to the front. If the asset count is 0, then I would want to remove the library object and reopen it from the file system.

       

      But it's hard to test because I don't know how to reproduce this condituion, where the library is in some kind of invisible, asset-less state. I wonder if anyone else has seen this?

        • 1. Re: InDesign library: How is this possible?
          John Hawkinson Level 5

          Have you tried instead

           

          if (app.libraries && app.libraries.itemByName("Bob.indl") && app.libraries.itemByName("Bob.indl").isValid)

           

          ?

           

          The first one is probably unnecessary, and arguably you should cache the result of the itemByName in a variable, but you get the idea...

           

          (Also, it's generally recommended that you not test if (var==true); just use if (var).)

          • 2. Re: InDesign library: How is this possible?
            suripappu

            hi, by using apple script

             

            tell application "Adobe InDesign CS5"

            set lib to libraries

            if count of lib > 0

            --already open

            else

              --not open

            end if

             

            end tell

            • 3. Re: InDesign library: How is this possible?
              RobertKyle Level 2

              John, thanks for the tip about var==true. I think sometimes I test for var==false, so I have a bad habit

               

              But I don't get the idea of your snippet. What does

               

              if (app.libraries && app.libraries.itemByName("Bob.indl")

               

              test for?

              • 4. Re: InDesign library: How is this possible?
                RobertKyle Level 2

                suripappu, I'm not looking for a simple count of libraries. I'm trying to determine whether a specific library (filename: Bob.indl) is currently open. I had thought that checking .isValid would do the trick, but there apparently is some in-between state where the library is open but its associatedPanel is not.

                • 5. Re: InDesign library: How is this possible?
                  John Hawkinson Level 5

                  John, thanks for the tip about var==true. I think sometimes I test for var==false, so I have a bad habit

                  Indeed, you're better off with if (!var) ... in that case.

                   

                  But I don't get the idea of your snippet. What does

                  if (app.libraries && app.libraries.itemByName("Bob.indl")

                  test for?

                  Test that app.libraries is an object before calling app.librarites.itemByName. (This is probably unnecessary.)

                  But then test that app.libraries.itemByName("Bob.indl") returns something, before trying to dereference it. Because if it returns null, then you will evaluate null.isValid, and that will generate an error.

                  • 6. Re: InDesign library: How is this possible?
                    RobertKyle Level 2

                    Well,

                     

                    app.libraries.itemByName("nonesuch.indl").isValid;

                     

                    returns a nice, neat false if that library isn't open.

                     

                    But I get the gist of it.

                     

                    My real problem comes in the next step. If the library isVald, I'm trying to bring the associatedPanel to the front. But there is this odd case where the library isValid but the associatedPanel does not exist and can't be addressed. In a nutshell:

                     

                    app.libraries.itemByName("Bob.indl").isValid; // true

                    app.libraries.itemByName("Bob.indl").associatedPanel.isValid; // true

                    app.libraries.itemByName("Bob.indl").associatedPanel.visible=false;

                    app.libraries.itemByName("Bob.indl").isValid; // false

                    app.libraries.itemByName("Bob.indl").associatedPanel.isValid; // error:  Object is invalid

                     

                    In real life, I don't make the panel invisible (it seems that that makes ALL libraries invisible if they are docked together). But imagine what happens if line 4 returns true. I can't reproduce it, but somehow the InDesign users in my office are getting into that position. I saw it on another guy's PC for a few minutes the other day and it looked like this:

                     

                    app.libraries.itemByName("Bob.indl").isValid; // true

                    app.libraries.itemByName("Bob.indl").associatedPanel.isValid; // error:  Object is invalid

                    app.libraries.itemByName("Bob.indl").assets.length; // 0

                     

                    So the library is open but the window doesn't exist and it has no assets.

                     

                    Is it a bug in the scripting model if associatedPanel.isValid returns true but not false?