15 Replies Latest reply on Aug 3, 2011 12:55 PM by Pickory

    A few questions about indesign CS3 scripting

    Dargon Kong Level 1

      1.     I often do one thing that pressing a Ctrl + ~, followed by a Ctrl + Shift + E. So I have spent one day trying to write a script today but failed to. I am going to be insane sooner or later.

      I have tried this line "app.menus[0].menuElements[10].menuElements[1].shortcutKey" for "Ctrl + Shift + E". This line is the farthest point I can get. It doesn't work.

      ------------------------------------------------------------------------------------------ -----------

      2.     if (app.menuActions.item (5262).name == "Stop BlackLining" || app.menuActions.item (5262).name ==  "Start BlackLining")

       

      is not equal to

       

      if (app.menuActions.item (5262).name == ("Stop BlackLining" || "Start BlackLining"))

       

      why?

      ------------------------------------------------------------------------------------------ -----------

      3.     I try this line "app.menuActions.item (5262).name" in ESTK. JavaScript Console reads "Start BlackLining".

      When I select and click the menu "Start BlackLining", the menu's contents changes to "Stop BlackLining". See below.

      pic 1.jpg

      pic 2.jpg

       

      Then I try the line refered above. JavaScript Console reads "Start BlackLining" again. As you can see, I consider that it should be "Stop BlackLining". The two different menus use the same index in Indesign.

      But when I only click their parent menu "BlackLining" once and then try the line, JavaScript Console reads "Stop BlackLining". That's normal. But how come? Do I click their parent menu to refresh all the menus to make it normal?

       

      Thanks in advance.

        • 1. Re: A few questions about indesign CS3 scripting
          John Hawkinson Level 5

          It is not normal to write scripts that are based on the menu interface. Instead, you are expected to deal with the document object model. This is a different way of thinking.

           

          Dargon Kong wrote:

           

          1.     I often do one thing that pressing a Ctrl + ~, followed by a Ctrl + Shift + E. So I have spent one day trying to write a script today but failed to. I am going to be insane sooner or later.

          I have tried this line "app.menus[0].menuElements[10].menuElements[1].shortcutKey" for "Ctrl + Shift + E". This line is the farthest point I can get. It doesn't work.

          There are some reasons to avoid dealing with selections in scripts, but perhaps in this case it is correct to do so. Then you would use app.selection[0].fit(FitOptions.CENTER_CONTENT);

           

          Oh, do you have some script or extension bound to Ctrl+Shift+E? You'll need to tell us more about that.

           

          2.     if (app.menuActions.item (5262).name == "Stop BlackLining" || app.menuActions.item (5262).name ==  "Start BlackLining")

           

          is not equal to

           

          if (app.menuActions.item (5262).name == ("Stop BlackLining" || "Start BlackLining"))

           

          In JavaScript you should use === and !== instead of == and !=, unless you want to be tripped up by weird type coercion problems.

          But anyhow, hwy would you think these would be equivalent?

          First, let's write out your first case parenthesized according to the JS precedence rules, so there is no ambiguity:

           

          ((app.menuActions.item(5262).name=="Stop BlackLining") || (app.menuActions.item(5262.name == "Start BlackLining"))

           

          Hopefully that's what you expect. Let's suppose it is the start case. Then that will evaluate to:

           

          ((false) || (true))

           

          which evaluates to true.

           

          But your second case is nothing like that.:

           

          (app.menuActions.item (5262).name == ("Stop BlackLining" || "Start BlackLining"))

           

          So first the interpreter evaluates ("Stop BlackLining" || "Start Blacklining"), that is, it takes the logical OR of two strings. This is not a very useful thing to do. It will always return the first one. That is ("a" || "b") always evalutes to "a". So then you have:

           

          (app.menuActions.item (5262).name == "Stop BlackLining")

           

          and if we're in the start case, then it is: false.

           

          As for your third question, well, the language is a bit confused, but menus can change their appearance when they are clicked on. That's a good reason not to use menu names or positions in scripts. Doesn't this blacklining extension (whatever it is) define a stopBlackLining() function somewhere in the DOM? Or someting like that?

          • 2. Re: A few questions about indesign CS3 scripting
            Dargon Kong Level 1

            Thanks for the tips. When I write a script, I will open all the Help Document, Object Model Viewer, Core JavaScript Classes and ScriptUI Classes.

            ————————————————————

            function(){return A.apply(null,[this].concat($A(arguments)))}

            function(){return A.apply(null,[this].concat($A(arguments)))}There are some reasons to avoid dealing with selections in scripts, but perhaps in this case it is correct to do so. Then you would use app.selection[0].fit(FitOptions.CENTER_CONTENT);

            After reading instruction in OMV, I can't figure it out what exactly "app.selection[0].fit(FitOptions.CENTER_CONTENT);" does.

            As the "Ctrl+Shift+E" is a shortcut customized for starting or stopping the function of the Plug-In "BlackLining" (The  Plug-In's name is BlackLining).

            ————————————————————

            function(){return A.apply(null,[this].concat($A(arguments)))}

            function(){return A.apply(null,[this].concat($A(arguments)))}

            In JavaScript you should use === and !== instead of == and !=, unless you want to be tripped up by weird type coercion problems.

            But anyhow, hwy would you think these would be equivalent?

            First, let's write out your first case parenthesized according to the JS precedence rules, so there is no ambiguity:

             

            ((app.menuActions.item(5262).name=="Stop BlackLining") || (app.menuActions.item(5262.name == "Start BlackLining"))

             

            Hopefully that's what you expect. Let's suppose it is the start case. Then that will evaluate to:

             

            ((false) || (true))

             

            which evaluates to true.

             

            But your second case is nothing like that.:

             

            (app.menuActions.item (5262).name == ("Stop BlackLining" || "Start BlackLining"))

             

            So first the interpreter evaluates ("Stop BlackLining" || "Start Blacklining"), that is, it takes the logical OR of two strings. This is not a very useful thing to do. It will always return the first one. That is ("a" || "b") always evalutes to "a". So then you have:

             

            (app.menuActions.item (5262).name == "Stop BlackLining")

             

            and if we're in the start case, then it is: false.

            I think there is no way to make it short, right?
            In fact, it's hard for me to find material of learning script t just execpt serveral PDFs (about basic of script) was added when the software was installed. I learned the C Language before. I try to remember the similar usage in C Language. Maybe I am wrong.

            ————————————————————

            function(){return A.apply(null,[this].concat($A(arguments)))}

            function(){return A.apply(null,[this].concat($A(arguments)))}

            As for your third question, well, the language is a bit confused, but menus can change their appearance when they are clicked on. That's a good reason not to use menu names or positions in scripts. Doesn't this blacklining extension (whatever it is) define a stopBlackLining() function somewhere in the DOM? Or someting like that?

            BTW, "BlackLining" is a Plug-In. Is Plug-In equal to extension in English? And DOM is short for document object model? It seems there are a lot more for me to learn.

            ————————————————————

            I want to tell you what kind of script I want to write. Make it simple. Forgiving my poor English.

            Let's get it started.

            I have to open many documents at the same time and then shut down the plug-in "BlackLining". So I have to press the shortcut "Ctrl+~", which means selecting the next document, and then press the shortcut "Ctrl+Shift+E", which is set by me and means starting or stopping the plug-in.

            When I finished some operions, which can't be proceeding while the plug-in "BlackLining" is on, I have to press the shortcut "Ctrl+~", and then press shortcut "Ctrl+Shift+E" to start "BlackLining".

            I have to do it over and over again day after day. I am almost insane. (Problem one)

             

            So, I want to write a script, let the computer do it for me. Further more, the shortcut "Ctrl+Shift+E" is used for both starting and stoping "BlackLining". Can't be set separately.

            pic 4.jpg

            At the same time, I don't know whether the "BlackLining" is on or not. What if it's off? Maybe old documents are on, some subsequent are off. See below:

             

            pic 3.jpg

            So I have to check the state before I press "Ctrl+Shift+E". (Problem two and three)

            Can you help me out? Thanks.

            If you need "BlackLining", you can download demo at http://www.blacklining.com/.

            • 3. Re: A few questions about indesign CS3 scripting
              John Hawkinson Level 5
              Thanks for the tips. When I write a script, I will open all the Help Document, Object Model Viewer, Core JavaScript Classes and ScriptUI Classes.

              You may find Jongware's HTML version (http://jongware.com/idjshelp.html)  more useful, or my unzippped version of it: http://jongware.mit.edu/idcs5js/

               

              After reading instruction in OMV, I can't figure it out what exactly "app.selection[0].fit(FitOptions.CENTER_CONTENT);" does.

              It was assuming the default binding of Control-Shift-E. If app.selection[0] is a rectangle, then it centers the content in the rectangle. But it turns out not to be relevant, since you're not talking about Center Content but instead a custom plugin that you didn't mention the first time...

               

              I think there is no way to make it short, right?

              In fact, it's hard for me to find material of learning script t just execpt serveral PDFs (about basic of script) was added when the software was installed. I learned the C Language before. I try to remember the similar usage in C Language. Maybe I am wrong.

               

              I can't think of a language where you could do it that way, not in C, no. There are lots of ways to make it short, though. You could use:

               

              var t=app.menuActions.item(5262).name;
              if (t==="Stop BlackLining" || t==="Start BlackLining")
              

               

              Or you could juse use:

               

              if (app.menuActions[5262].name.match(" BlackLining$"))
              

               

              Or you could use

               

              switch (app.menuActions[5262].name) {
                case "Start BlackLining":
                case "Stop BlackLining":
                  // do whatever
                  break;
                default:
                  // do something else
              }
              

               

              BTW, "BlackLining" is a Plug-In. Is Plug-In equal to extension in English? And DOM is short for document object model? It seems there are a lot more for me to learn.

               

              They are mostly equal, but not always. Best to say plug-in if you mean plug-in. Yes, DOM is the Document Object Model.

              Ideally your plugin should have defined scripting commands so you can call it by name without using the menuActions. But that all depends on your plugin, and I am not familiar with it.

               

              Are you saying that looking at the name proprety does not work?

              Have you tried app.menuActions[5262].invoke()?

              1 person found this helpful
              • 4. Re: A few questions about indesign CS3 scripting
                Dargon Kong Level 1

                Thanks for your materials provided.

                I have tried app.menuActions[5262].invoke().

                • 6. Re: A few questions about indesign CS3 scripting
                  Harbs. Level 6

                  Try app.menuActions.itemByID(5262).invoke()

                   

                  Harbs

                  • 7. Re: A few questions about indesign CS3 scripting
                    Dargon Kong Level 1

                    No, it didn't work. I have tried everything I can find in the OMV. I can't find any information to deal with BlackLining or Plug-In or Panel or Extension or Paltte or shortcuts.

                    I don't even know how to start with selecting the next document in Indeisgn. app.activeDocument = app.documents[i]? No can do.

                    Finally, I gave it up.

                    Thanks for concerning.

                    • 8. Re: A few questions about indesign CS3 scripting
                      Dargon Kong Level 1

                      I have tried

                       

                      app.menuActions.itemByID(5262).invoke ()

                      app.menuActions[5262].invoke ()

                      app.menuActions.item (5262).invoke ()

                      app.menuActions.item ("Start BlackLning").invoke ()

                      app.menuActions.itemByName ("Start BlackLning").invoke ()

                       

                      Anyway, thanks for concerning.

                      • 9. Re: A few questions about indesign CS3 scripting
                        moluapple Level 4

                        Dargon Kong wrote:

                        I don't even know how to start with selecting the next document in Indeisgn. app.activeDocument = app.documents[i]? No can do.

                        This will do the trick:

                         

                        app.windows[i].bringToFront();
                        
                        • 10. Re: A few questions about indesign CS3 scripting
                          [Jongware] Most Valuable Participant

                          Dargon Kong wrote:

                          [...] I don't even know how to start with selecting the next document in Indeisgn. app.activeDocument = app.documents[i]? No can do.

                           

                           

                          Sure you can. "app.activeDocument" is a read/write operation, and so you can set it to any opened document.

                           

                          The thing is, as with all collections, you need some safeguarding against processing the same document twice. With ten documents open, I ran this little script:

                           

                          for (i=0; i<app.documents.length; i++)
                          {
                           app.activeDocument = app.documents[i];
                           app.activeDocument.rectangles.add();
                          }
                          

                           

                          and it added no rectangle to some documents, two to a few other documents, and three to one document.

                           

                          But that's not a problem! Just as it's advised not to use "app.select(stuff)" and then do something on the selection, this little useless script could be re-written as:

                           

                          for (i=0; i<app.documents.length; i++)
                          {
                           app.documents[i].rectangles.add();
                          }
                          

                           

                          and then there is no problem at all processing all of the open documents, and you don't need to set the activeDocument anywhere.

                          • 11. Re: A few questions about indesign CS3 scripting
                            Dargon Kong Level 1

                            Thanks for concerning.

                            Suddenly, I have come up with an idea.

                             

                            var ll = app.documents.length - 1
                            for (i = ll; i >= 0; i--) {

                                 app.activeDocument = app.documents[ll]

                            }

                             

                            can do the trick.

                            • 12. Re: A few questions about indesign CS3 scripting
                              Dargon Kong Level 1

                              Thanks for your reminding.

                              I have considered what you remind me before I post the problem.

                              The problem is that I DO NOT KNOW how to access the switch of the Plug-In. I don't even know whether there is a switch or not.

                              Just like this:

                              for (i=0; i<app.documents.length; i++)
                              {
                              app.documents[i].?????????.invoke();   or   app.documents[i].?????????.open();
                              }
                              So I try to solve the problem by selecting every opening document one after one and executing the commands of menus.

                              Just like this:

                              var judge = ......................checkedState

                              var ll = app.documents.length - 1

                              for (i = ll; i >= 0; i--) {

                                   app.activeDocument = app.documents[i]    

                                   // I have just come up with the Idea. Lines above work.

                                   if (judge) {

                                        try {

                                             app.menuActions.item ("Start BlackLing").invoke ()

                                        }

                                        .......

                                   }

                                   else {

                                        try {

                                             app.menuActions.item ("Stop BlackLing").invoke ()

                                        }

                                        .......

                                   }

                              }

                              It seems working now ....... =.= Dizzy.

                              Thanks all the same.

                              • 13. Re: A few questions about indesign CS3 scripting
                                John Hawkinson Level 5

                                I'm a bit confused, you told us that none of these worked:

                                app.menuActions.itemByID(5262).invoke ()

                                app.menuActions[5262].invoke ()

                                app.menuActions.item (5262).invoke ()

                                app.menuActions.item ("Start BlackLning").invoke ()

                                app.menuActions.itemByName ("Start BlackLning").invoke ()

                                But now you're saying they work fine? What changed?

                                • 14. Re: A few questions about indesign CS3 scripting
                                  Dargon Kong Level 1

                                  Hawkinson.

                                   

                                  It seems possbile. I am saying this method may be work (using the try statement). Not talking about the code you refered.

                                  The main problem now is the third one I arise at first --- the menus refresh their "name" or "title". Because I don't know any object model related with the "Blacklining", I have to deal with it through the menuActions. But I have to judege the situation before I do it.

                                   

                                  I wrote some code below:

                                   

                                  var diaLen = app.dialogs.length
                                  for (a = 0; a < diaLen; a++) {
                                  app.dialogs[0].destroy()
                                  }
                                  with (app.dialogs.add ()) {
                                  with (dialogColumns.add ()) {
                                    with (radiobuttonGroups.add ()) {
                                     var butOne = radiobuttonControls.add ({staticLabel: "Start BlackLining"})
                                     var butTwo = radiobuttonControls.add ({staticLabel: "Stop BlackLining"})
                                    }
                                  }
                                  }  
                                  if (app.dialogs[0].show() == 0) {
                                  exit()
                                  }
                                  var ll =

                                  for (i = app.documents.length - 1; i >= 0; i--) {
                                  app.windows[ll].bringToFront()
                                  if (butOne.checkedState == true) {
                                    try {
                                     app.menuActions.item ("Start BlackLining").invoke()
                                    }
                                    catch (err) {
                                     $.error
                                    }
                                  }
                                  else {
                                    if (butTwo.checkedState == true) {
                                     try {
                                      app.menuActions.item ("Stop BlackLining").invoke()
                                     }
                                     catch (err) {
                                      $.error
                                     }
                                    }
                                  }
                                  }

                                  But it doesn't work because of the menus' name or title.

                                  • 15. Re: A few questions about indesign CS3 scripting
                                    Pickory Level 3

                                    Hello,

                                     

                                    I see BlackLining mentioned, this is some thing I know about.

                                     

                                    I have to say I work for this company.

                                     

                                    http://www.kerntiff.co.uk/products-4-indesign/edit-marks-blacklining

                                     

                                    I know it is not script solution but it might be of use to some one.

                                     

                                    P.

                                     

                                    P.S. We are happy to make it scriptable.

                                     

                                    P.P.S., Our BlackLining plugin is not the plugin you are currently using.