5 Replies Latest reply on Oct 17, 2017 11:17 AM by Silly-V

    JS - basics: tips to debug - variables types issue

    et3d_ Level 1

      Hi, I'm using textFrames to build a list;

      Is there a better software than estk to debug, because on my case I've had to guess on myself that my variable wasn't of the type - maybe should I declare everytime?

       

      ESTK said: "no such element" which is meaningless

       

      previous issue:

      The core snippet works fine but I can't manage to use parameters correctly.
      I do a lot of debug, in my code the "d" that refers to Files[i] does not propagate into the function "makelist".

      I've checked on jshint.com, there's no obvious error;

       

      I want to overcome this issue because I'm updating every script I make to avoid using globals, to simplify and increase reusage

       

      #target Illustrator-21
      #script "export_text_for_list"
      "use strict"
      $.level=2;
      main();
      $.write("extex ok >");
      $.gc();
      
      function main(){
      //INIT
        var fileType = "*.pdf", i , imax, d, f = [] , sF = [];
        sF[1] = Folder ("S:/Articles/Source/01");
      
      //MAIN CODE  
        f = sF[1].getFiles( fileType );
        i = 0;
        imax = f.length; //files
        if ( f.length != 0 ){
        for ( ; i < imax; i++ ){
        d=f[i];
        app.open(d);  //<<< d is not a document: SOLVED 
        makelist(d);
        }
        }
      }
      
      function makelist(d){ //HERE d was not defined correclty
      var tL = []; //text array
      tL.push(d.name);
        var tF = d.textFrames; //array
      for ( var i = 0; i < tF.length; i++ ){
      tL.push(tF[i].contents);
      }
        return (tL);
      } //then export
      

      Thanks for your time

      EDIT: Ok found it : a [File] is not a [Document]

      d = app.open(f[i] ) //fixes d
      

       

      Message was edited by: et3d SOLVED

        • 1. Re: JS - basics: tips to debug - variables types issue
          Silly-V Adobe Community Professional

          "no such element" supplies you the information which implies that the error is a result of trying to obtain reference to an item of a collection which does not exist. The subtle yet critical difference between the message "undefined is not an object". It means, the data type of the reference has to be one of Illustrator's collection objects. But they are not the same! Check out my test and the varying out-of-index situations and what errors they get:

           

          #target illustrator

          function test(){

            var arr = ["a", "b"];

            alert(arr[2]); // undefined

           

            try {

              alert(app.documents[0].layers[3].name);

            } catch(e) {

              alert(e); // PARM

            }

           

            try {

              alert(app.documents[0].layers[0].layers[1].name);

            } catch(e) {

              alert(e); // Index is out of bounds

            }

             

           

            try {

              alert(app.documents[2]);

            } catch(e) {

              alert(e); // no such element

            }

           

            try {

              alert(app.activeDocument.textFrames[2]);

            } catch(e) {

              alert(e); // no such element

            }

           

           

            try {

              alert(app.activeDocument.artboards[2]); // ALWAYS returns an artboard

            } catch(e) {

              alert(e);

            }

           

           

          };

          test();

          1 person found this helpful
          • 2. Re: JS - basics: tips to debug - variables types issue
            et3d_ Level 1

            Thanks!

            I've got it!

             

            I've used Notepad++ (win) but I don't know if it's possible to execute estk jsx from it. Using NP++ helps with typos errors. I no longer do because editing this way taking more time (save, switch focus, reload...)

            Do you use other tools than the Estk IDE?

            • 3. Re: JS - basics: tips to debug - variables types issue
              Silly-V Adobe Community Professional

              I just use Sublime text to edit my script and ESTK to run it, on mac - (because it's super slow on Macs if you don't turn off App Nap for all applications).

              I enjoy Sublime's text editor features, such as F12 shortcut to go to definition of a function.

              One thing which also helps is having a main script file and using #include to hold the various editable parts - this way I simply switch and run without having to reload more. Also, I made some shortcut keys for switching applications, so it helps with switching faster.

              • 4. Re: JS - basics: tips to debug - variables types issue
                et3d_ Level 1
                #target Illustrator-21
                #script "script v00"
                "use strict"
                $.level=2;
                main();
                $.write("everything went nice and smooth; ok >");
                $.gc();
                
                

                 

                I'm using this header. But I don't know how garbage collection works; Moreover, I see stuff that stays after script execution into the data navigator panel of ESTK, even thougheverything is mean't to be local scope. Not sure if it might be an issue though.

                Thanks again for practical exemples.

                 

                I'm a bit sad because my boss want this stuff shut down because, I'm not "payed for scripting". (I've only said I was doing Actions modification though). So I must do everything "à la mano" hand by hand. I'm a temporary worker, there's no point arguing with someone that only sees GUI (everything must be done with mouse) for HID (transforming raw material into something that can be processed)

                 

                Anyway this was very fun from my VB6 experience! : D

                • 5. Re: JS - basics: tips to debug - variables types issue
                  Silly-V Adobe Community Professional

                  That's sad, they should actually be paying you lots more and encouraging as much scripting as possible!

                  You should still do it via script and ease your own job though, it's the only way to fly.

                  As for the scope variables, I also get those - but I also am aware that I'm running startup scripts from a long time ago which leak a couple of variables in.