19 Replies Latest reply on Oct 26, 2012 7:22 AM by Loic.Aigon

    app.selection throws error in doScript context

    Loic.Aigon Adobe Community Professional

      Hi guys,

       

      I have a script working fine in straight mode. Once I put it in a doScript, it executes well once and then throws an error ( I also undo script in the meanwhile to start over). The issue seems to deal with a reference error ( object not valid ) while I try to reach the app.selection object.

       

      Has anypne experienced such issues ?

       

      TIA for any hints,

       

      Best,

       

      Loic

        • 1. Re: app.selection throws error in doScript context
          Harbs. Level 6

          I don't remember if I had this specific issue, but I have seen funniness related to selections and undo modes…

           

          Harbs

          • 2. Re: app.selection throws error in doScript context
            Loic.Aigon Adobe Community Professional

            Hi Harbs,

             

            Thanks for your answer. It's really a weird stuff indeed.

             

            Loic

            • 3. Re: app.selection throws error in doScript context
              Trevorׅ Adobe Community Professional

              Hi Loic

               

              I doubt this is the problem but just in case

               

              This will work

               

              app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Delete Questions / Answers");
              

               

              These will not work

              app.doScript("main", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Delete Questions / Answers"); // displays function in js console but does not run it.
              
              app.doScript(main(), ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Delete Questions / Answers"); // gives invalid value... error
              

               

              I have some vague memory that

              app.doScript(main, ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Delete Questions / Answers");

              can sometime also cause problems, but I expect to be jumped upon for such a statement

               

              Trevor

              • 4. Re: app.selection throws error in doScript context
                Loic.Aigon Adobe Community Professional

                Hi Travis,

                 

                Thanks for the advice. I do use indeed "main()" or main() but nor main nor "main". Really what's weird is that the script runs once smoothly but if you do undo ( so taking advantage of the UndoMode ) and then rerun the script, the app.selection object is not reachable anymore

                 

                I would be surprise that such an issue has never been raised so I want to think I am doing something wrong but I can't see why unfortunately.

                 

                Loic

                • 5. Re: app.selection throws error in doScript context
                  Trevorׅ Adobe Community Professional

                  Loic_aigon wrote:

                   

                  Thanks for the advice. I do use indeed "main()" or main() but nor main nor "main".

                  Hi Loic, I highly doubt that it is the source of of the problem but writing main() without the "quote marks" can be very problematic.

                   

                  The real question is do you get the error on the re-run even after you make a manual selection yourself or just when you run the script twice in a row without making a selection in the meantime?

                   

                  If it's just when running the script twice in a row then you'll have to see if something else in the script either deselects the selection or makes indesign inactive.

                  I sounds from your posts that the object is still selected but inaccessible.

                   

                  What happens when 1) You type in the JS console "alert(app.selection[0].constructor.name)" ? and 2) when you add the line "alert(app.selection[0].constructor.name)" after the doScript line?

                   

                  Other than the above, I have no ideas.

                   

                  Good luck,

                   

                  Trevor

                  • 6. Re: app.selection throws error in doScript context
                    Trevorׅ Adobe Community Professional

                    Just as an after thought, have you tried changing the type of undo mode?

                    • 7. Re: app.selection throws error in doScript context
                      Loic.Aigon Adobe Community Professional

                      Hi Travis,

                       

                      Yep, I don't use main() neither, I just typed too quickly

                       

                      The scenario that raises issue is :

                       

                      1) run script once ( with an active selection ) //OK

                      2) undo ( selection still active ) //OK

                      3) re run the script => ERROR ( although selection is active )

                       

                      At this point none of my attempts to read at the app.selection object worked. The script engine still claims for a reference error ( "object is not valid" );

                       

                      Even weirder, if I look at the object properties with for ( prop in app.selection )…I can read "length" and indeed app.selection.length returns 1 in my case. BUT if I try to get inside that array, it raises an error ( app.selection[0] ).

                       

                      I do use ENTIRE_SCRIPT only but I may change mode to see if it improves anything.

                       

                      Thanks for your time and effort,

                       

                      Loic

                      • 8. Re: app.selection throws error in doScript context
                        Trevorׅ Adobe Community Professional

                        But what happens between stage 2 and 3

                        1) You type in the JS console "alert(app.selection[0].constructor.name)" ?

                        and 2) when you add the line "alert(app.selection[0].constructor.name)" after the doScript line?

                        Do you also get "object is not valid" errors then?

                         

                        Trevor (not Travis although for some reason I do get call Travis pretty often well 3 out of 5 letters isn't bad)

                        • 9. Re: app.selection throws error in doScript context
                          Laubender Adobe Community Professional & MVP

                          @Trevor – did your "vague memory" turn into something not so vague concerning:

                           

                          app.doScript(main, etc...
                          

                           

                          ??

                           

                          My memory tells me  "UndoModes.ENTIRE_SCRIPT" should be used over "UndoModes.FAST_ENTIRE_SCRIPT".

                           

                          I think Harbs once did some testing and reporting about the pros and cons. Maybe there is a blog post about the problem by Marc Autret. But that's my vague memory ;-)

                           

                          Hah! Just searched Mark's website and came up with that:

                           

                          http://www.indiscripts.com/post/2011/08/notes-on-fastentirescript-undo-mode

                           

                          And that leads us to Harb's comments at:

                           

                          http://forums.adobe.com/message/3152845#3152845

                           

                           

                          Uwe

                          • 10. Re: app.selection throws error in doScript context
                            Loic.Aigon Adobe Community Professional

                            Hi Trevor,

                             

                            I feel ashamed to have mistyped your name. I am the one annoyed with typos with my name so I can figure the trouble here.

                            I also think I may have mixed with Trevor Morris ( is that you BTW ? ) so at the end it became Travis…Sorry.

                             

                            Well to answer your question, indeed I can reach and read the selection after point 2. It's just running the script over that raises the issue. Still no clue why but at least it's a lead.

                             

                            Thanks for all.

                             

                            Loic

                            • 11. Re: app.selection throws error in doScript context
                              Trevorׅ Adobe Community Professional

                              Loic:

                               

                              Nothing to be ashamed of, I have been called much worse things before (not by mistake)

                              I can most definitely say that I am not Trevor Morris.

                               

                              Uwe:

                               

                              No still as vague and wishy washy as ever, but I did rember Marcs and Harbs comments and suspect that the problem was to do the "FAST_ENTIRE_SCRIPT" syndrome

                               

                              Regards

                               

                              Trevor

                              • 12. Re: app.selection throws error in doScript context
                                Marc Autret Level 4

                                Hi all,

                                 

                                Just to clarify, the first argument we pass to app.doScript(…) may be, as said in the doc:

                                 

                                (1) any File which contains a valid code (including jsxbin format);

                                (2) any String which contains a valid code (including jsxbin format);

                                (3) any JS Function.

                                 

                                The difference between app.doScript("myFunc()", …) and app.doScript(myFunc, …) is the difference between (2) and (3). The benefit of using the String form is that you can easily pass arguments to the function, or even invoke mutiple functions — app.doScript("myFunc1(); myFunc2(); /*etc*/", …) — as the entire string passed to app.doScript is simply evaluated as a code.

                                 

                                By contrast there is usually no sense in writing:

                                 

                                    app.doScript(myFunc( ), …)

                                 

                                as the above line simply means: call myFunc(), then pass the returned value as the first argument of app.doScript.

                                Note that this could have sense if myFunc() returns a string which contains a valid code, e.g.:

                                 

                                function myFunc(){ return "alert('hello World!');" }

                                app.doScript(myFunc( ), …)

                                 

                                 

                                Also, there is definitely no sense in writing:

                                 

                                    app.doScript("myFunc", …)

                                 

                                as the above line simply means: tell app.doScript to execute the statement contained in the string "myFunc", i.e. the statement:

                                    myFunc;

                                which does not call the function at all.

                                 

                                 

                                @+

                                Marc

                                • 13. Re: app.selection throws error in doScript context
                                  Trevorׅ Adobe Community Professional

                                  Marc said:

                                   

                                  Also, there is definitely no sense in writing:

                                   

                                      app.doScript("myFunc", …)

                                   

                                  Hi Marc & all,

                                   

                                  Nice and clear, just one point of possible interest.

                                   

                                  Who can guess what the following  scripts will do?

                                   

                                  #target indesign
                                  #targetengine session
                                  app.doScript("myFunc2");
                                  function myFunc1()
                                  {
                                      Hello
                                  }        
                                          
                                  function myFunc2() 
                                  { 
                                              blah
                                              blah
                                              blah
                                  }
                                  

                                   

                                  #target indesign
                                  #targetengine session
                                  app.doScript("myFunc2");
                                  function myFunc1()
                                  {Hello All}        
                                          
                                  function myFunc2() 
                                  { 
                                              blah
                                              blah
                                              blah
                                  }
                                  

                                   

                                  #target indesign
                                  #targetengine main
                                  app.doScript("myFunc2");
                                  function myFunc1() {Hello}        
                                  function myFunc2() {blah}
                                  

                                   

                                  The answers

                                  Script one:

                                  Will "Write" to the js console, the following:

                                  function myFunc2()

                                  {

                                              blah

                                              blah

                                              blah

                                  }

                                   

                                  Script Two:

                                  will produce and error that it expected an ; after the Hello.

                                   

                                  Script Three:

                                  Will write the following to the js console:

                                  function myFunc()

                                   

                                  Well I hope everybody guessed right.

                                   

                                  Well with this very trivial piece of knowledge we can find a use for app.doScript("myFunc")

                                   

                                  If we have a several thousand line script running in "session" and we want to copy a long function without having to find it's beginning or end the we clear the JS console and can type in app.doScript("myFunc") this will give us the text of the function provided that it does not have statements like "Hello All" in it.

                                   

                                  It would appear that some form of testing even of functions that are "not called on" occurs when one calls
                                  app.doScript("myFunc").

                                   

                                  Well, that's quite enough said by me for now

                                   

                                  Bye all, blah, blah blah

                                   

                                  Trevor (AKA Travis)


                                  • 14. Re: app.selection throws error in doScript context
                                    Marc Autret Level 4

                                    Hi Trevor,

                                     

                                    I'm probably missing the obvious, but I don't undertand your point at all. Maybe your proof of concept requires the ESTK to be used?? (As I'm not very familiar with that program, I feel unable to comment on the results you get in the JS console.)

                                     

                                    Anyway, what is really obscure to me is the difference you want to illustrate between the main engine and a session-persistent one.

                                     

                                    Technically, your examples #1 and #3 have a correct syntax (even though they seem pretty confusing), while your example #2 is not syntactically valid.

                                     

                                    #1 is based on:

                                     

                                    function myFunc1()
                                    {
                                        Hello
                                    }        
                                            
                                    function myFunc2() 
                                    { 
                                                blah
                                                blah
                                                blah
                                    }
                                    

                                     

                                    so myFunc1, if invoked, would attempt to access to a global variable, Hello, which is undeclared. Also, thanks to the newlines that act as semicolons, myFunc2 would attempt three times to access to a global variable, blah, which is undeclared too. These two functions would fail if they were invoked—by chance they are not—but they both remain syntactically valid, so the JS interpreter is OK with that.

                                     

                                    #3 is based on the following declarations:

                                     

                                    function myFunc1() {Hello}
                                    function myFunc2() {blah}
                                    

                                     

                                    which for the same reasons are valid.

                                     

                                    By contrast, #2 cannot be intrepreted due to the wrong syntax of myFunc1:

                                     

                                    function myFunc1()
                                    {Hello All}
                                    

                                     

                                    Indeed there is no operator between the supposed variables Hello and All.

                                     

                                    This explains why #2 leads the intepreter to an error, whereas #1 and #3 do not. Target engine and app.doScript are not involved in that.

                                     

                                    Now, about the result of executing #1 or #3 and considering what we know about app.doScript(…), we can say that in both cases all leads to the statement:

                                     

                                        myFunc2

                                     

                                    which is a passive reference to an existing Function object. myFunc2 is not executed, only referred to. The script in itself has no effect, but in the JS console you should see the result of evaluating that expression. But evaluating a reference to a function is not calling that function. This is just a display thing, similar to: alert( myFunc2 ). Depending on how works the console output, you may see the body of the function if myFunc2.toString() is implicitly invoked, or simply the string "myFunc2", I don't know…

                                     

                                    Here again, I don't see the connection with target engines or app.doScript().

                                    You should come to the same output by simply running a script like:

                                     

                                    function myFunc2() {blah}
                                    myFunc2
                                    

                                     

                                    don't you?

                                     

                                    @+

                                    Marc

                                    • 15. Re: app.selection throws error in doScript context
                                      Loic.Aigon Adobe Community Professional

                                      Hi Marc,

                                       

                                      Any experience with doScript/Undo raising errors while accessing app.selection object ?

                                       

                                      Loic

                                      • 16. Re: app.selection throws error in doScript context
                                        Marc Autret Level 4

                                        Hi loïc,

                                         

                                        I had a similar issue (reference error) in CS5 and later, but not related to app.selection. In my case, ID couldn't resolve app.activeDocument when re-running the script. (No issue in CS4, BTW.)

                                         

                                        I found a fix but I can't explain why and how it worked. At the end of my script—after the app.doScript(…) stuff—I just wrote this:

                                         

                                        app.properties.activeDocument && app.activeDocument.getElements();

                                         

                                        That's it. Just telling ID to resolve the internal reference of the property 'activeDocument'.

                                        Weird, but the bug didn't appear again.

                                         

                                        Does your script work in CS4?

                                         

                                        @+

                                        Marc

                                        • 17. Re: app.selection throws error in doScript context
                                          Trevorׅ Adobe Community Professional

                                          Hi Mark,

                                           

                                          Looks there that you might well have a good lead for Loic.

                                           

                                          I was referring above to the results in the js consul of the ESTK.

                                           

                                          There is a difference in what the console displays depending on the target engine.

                                          I see now however that that difference has nothing to do with the doScript.

                                          The "Result" given is the result of the last command executed in the script.

                                           

                                          If

                                          function myFunc2() {blah}
                                          myFunc2

                                          is run in session the result will give "function myFunc2() {blah}"

                                          if it is run in main the result will give "myFunc2"

                                           

                                          As you wrote app.doScript("myFunc2") is exactly the same as writing myFunc2 not in the doScript.

                                          The "Result" in the ESTK will differ in accordance with the target engine.

                                           

                                          See screen shots

                                          ScreenShot089.png

                                          ScreenShot088.png

                                          Trevor

                                          • 18. Re: app.selection throws error in doScript context
                                            Marc Autret Level 4

                                            Hi Loïc,

                                             

                                            Try to insert:

                                             

                                            app.selection && app.selection[0];
                                            

                                             

                                            just BEFORE the app.doScript(…) statement.

                                             

                                            Any change?

                                             

                                            @+

                                            Marc

                                            • 19. Re: app.selection throws error in doScript context
                                              Loic.Aigon Adobe Community Professional

                                              Thanks a lot Marc,

                                               

                                              That was a tricky one !

                                               

                                              Get well,

                                               

                                              Loic