• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
3

console.log() shim

Explorer ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting

Views

5.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
New Here ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

This!

I find it very useful and was looking all over the place to bypass ESTK. Works very well.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2020 Apr 26, 2020

Copy link to clipboard

Copied

LATEST

As ESTK becomes history (i. e. EOL as its not running under macOS Catalina any longer) and 

$.writeln()

won't work, the approach writing messages to the socket is a life-saver!

Thank you for this. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines