5 Replies Latest reply on Jun 19, 2009 8:30 AM by MDSYNTST

    inserting a library item on a page using Javascript

    MDSYNTST Level 1



      I have a library with 6 different layouts in it.  I need to be able to place these on the page from an external program.  I have all my code in place to send commands to Indesign, but I'm not sure how to reference the Library items.  Can I reference them by name?  As well, I would like my external custom app to be able to list all the library items so that the user can select which one to insert.  I'm at a total loss as to how to do this.


      I need help with these two things:


      1) obtain an array or list of all the items in a particular Library, which is already open.


      2) Once I have the list of library items I need to be able to place one or multiple on the page keeping track them so I can place images and text in the appropriate boxes.


      Any help with this would be greatly appreciated.  I'm using Indesign CS3 and my external app is coded in Objective-C on a Mac.  I prefer Javascript as I'm very familiar with it as opposed to Applescript.


      Thanks for any help provided.


      Dave Williams

        • 1. Re: inserting a library item on a page using Javascript
          MDSYNTST Level 1



          For all those interested in using library items from javascript please look at this link: http://www.pdsassoc.com/index.php?Nav=javassub&Ban=LibrariesAssets&Info=jsobjectmodel/Libr ariesAssets/index.html


          It has the basics that helped solve my second question.


          I'm still looking for a way to obtain a list of all library assets in an array that works with Objective-c.  I will post when I come up with an answer.



          • 2. Re: inserting a library item on a page using Javascript
            Dave Saunders Level 4

            Back in the day when I wrote that page (it was written for CS), I thought that learning to work with assets and libraries would be the key to working with just about any objects because the principles are the same, it's just the details that are different.


            Glad it helped you.


            myNames = myLibrary.assets.everyItem().name;


            will give you the names of every asset in a library in an array. You could then use:




            to get the nth asset, although this only works if all the assets in the library have unique names -- which will only happen if the user applies appropriate discipline.



            • 3. Re: inserting a library item on a page using Javascript
              MDSYNTST Level 1

              Hi Dave,


              Thanks again for your response.  I now have a javascript that can read all the assets in a library and place them in a textbox, which I'm sure you can imagine is not what I was going for...


              So, the javascript is working, but I now need to take the names of the assets and get them into my Objective-C program.  I know this isn't the correct forum for this discussion, but since we're on the topic I thought it might be a good starting point.


              So, I have this Javascript that can work totally within itself and read the names from the library.  However, if I'm calling that script from an external app, how might I instruct the javascript to return a value to the main program?


              Again, any insight is greatly appreciated, even the smallest of advice will be explored fully to come up with a solution.




              • 4. Re: inserting a library item on a page using Javascript
                Dave Saunders Level 4

                Assuming you have access to the equivalent of doScript, when that is used to run a JavaScript, the final value of the script is returned (coerced to a string? I'm not sure).


                Of course, you can always pass information from one place to another by using keyed labels. In my work (I spend most of my time writing scripts for Typefi Publish wherein the same script might get executed at the end of every page as the Typefi Engine composes the document), I often will add a keyed label to the document that passes information from one run of the script to the next. Here's an example (that includes reading the script itself to extract version information from the second line) -- while slightly inefficient, it has the benefit of putting the version info in a defined, easy to find place:


                    function registerFirstCall(aDoc, interactive) {
                        var scriptFile = getScriptPath();
                        var scriptName = scriptFile.name;
                        if (aDoc.extractLabel(scriptName) == "" || interactive) {
                            var firstLine = scriptFile.readln();
                            var secondLine = scriptFile.readln();
                            if (secondLine.indexOf("//VERSION: ") == 0) {
                                var msg = secondLine.split("//VERSION: ")[1].split("\n")[0];
                            } else {
                                var msg = "unknown";
                            aDoc.insertLabel(scriptName, "Registered");
                            TPSlog(scriptName + " version: " + msg, interactive);
                    }// end registerFirstCall


                Ah, I see it uses the getScriptPath function, so for completeness, here it is:


                    function getScriptPath() {
                        // This function returns the path to the active script, even when running ESTK
                        try {
                            return app.activeScript;
                        } catch(e) {
                            return File(e.fileName);
                        } // end try
                    } // end getScriptPath


                The TPSlog function either pops up an alert (when working interactively -- usually means during debugging) or adds a log message for the session (that also is managed with a document keyed label). The keyed label in this case is performing the trivial task of recording that the script has previously been run against this document, in which case, it doesn't need to log a message identifying its version info.


                Hope this helps.



                • 5. Re: inserting a library item on a page using Javascript
                  MDSYNTST Level 1

                  Hi Dave,


                  I was hoping you could answer another question for me regarding the library assets.  In you tutorial about adding library items to the page I believe you stated we can't access the names of the individual frames in each asset in the library, but I have a client that is using an application that seems to be able to read those script label of the individual frames without puting them on the page first.  Because your article was written for CS, not CS3 I'm wondering if there is now a way to get the script labels of each frame in the library.


                  for example, I have a library called Library1.  In Library1 I have 5 assets called asset1 through asset5.  I can get all those asset names in my application now thanks to your help.  However, if asset1 has two text boxes, and asset2 has three rectangles, is there a way that I can retrieve those textbox and rectangle labels using javascript?