0 Replies Latest reply on Jun 5, 2018 5:52 AM by sidpalas

    console.log() shim

    sidpalas Level 2

      Hello again!

       

      A few days ago I shared a simple logging utility to help with creating and using log files (Simple Logging Utility ). I recently created a simple utility that fills another gap in my development process.

       

      Like many people coming to ExtendScript with previous some experience writing JavaScript I was annoyed by inability to use console.log() to print messages at runtime. I soon discovered $.writeln() within ExtendScript ToolKit, but most of the time I prefer to work outside of ESTK and only open it up for specific debugging needs.

       

      So, I wrote a simple library which uses the Socket object to send messages to a port on localhost which can then be listened to and printed in a Terminal using the netcat command (availalbe on OSX by default, and there exist implementations for Windows as well, but I haven't tested any).

       

      The code can be found at: https://github.com/sidpalas/extendscript-console

       

      An example of usage can be found in main.jsx, the contents of which have been included below:

       

      #target photoshop
      //@include "./console.jsxlib"
      
      var console = new ConsoleLog();
      
      for (i = 0; i < 50; i++) {
        console.log(i);
        $.sleep(10)
      }
      

       

      After instantiating the ConsoleLog() log object and storing it as the console variable, the log method can be used throughout your code. To view the log, open up a Terminal and run:

       

      $ nc -lvk 8000
      

       

      This will listen to localhost on port 8000 (the port specified in console.jsxlib) and print any incoming messages.

       

      If you look at the console.jsxlib code (pasted below), you will see that it checks whether ESTK is running and then:

       

      If ESTK is running:

           console.log() simply calls $.writeln() and the messages are written to the Javascript Console within ESTK.

      If ESTK is not running:

           console.log() opens a socket connection to a port on localhost and sends the messages there.

       

      This way, when you are using ESTK, it will print to the JavaScript Console there, but if not, it won't cause ESTK to launch.

       

      function ConsoleLog() {
          //To view log from OSX terminal, execute:
          //$ nc -lvk <port> (listens on localhost:<port>)
          this.socket = new Socket();
          this.estkIsRunning = BridgeTalk.isRunning ('estoolkit');
          this.hostPort = "127.0.0.1:8000";
          this.log = function(logMessage){
                  if (this.estkIsRunning){
                     $.writeln(logMessage);
                  }
                  else{
                      if (this.socket.open(this.hostPort)){
                        this.socket.write (logMessage + "\n");
                        this.socket.close();
                      }
                  }
               }
          }
      

       

      Hopefully others find this useful too!