Wednesday, June 27, 2012

Javascript and Python communication by socket and eval javascript in PP

Javascript and Python communication by socket and eval javascript in PP


Extendscript code piece:

//#target photoshop
//#target bridge
//#target estoolkit
function doSometing(info)
{
    eval (info);
}    
var flag =true;
var tcp = null;
var demon = true;
function startServer(port) 
{
    tcp = new Socket;
    // listen on port 1234
    //$.writeln ("Chat server listening on port 1234");
    if (tcp.listen (port)) 
    {
            // poll for a new connection
            var connection = pollConnection();
            if (connection != null) 
            {
                   // alert ("Connection from " + connection.host);
                    m_cConnection =  connection;
                   readRequestFromClient();
            }
    }
}

function pollConnection()
{
    alert("pollConnection");
   var connection = null;
   while ((connection =tcp.poll())== null && flag) 
    $.sleep (1000);
   return  connection;
}
function readRequestFromClient()
{
    alert("readRequestFromClient");
    while (flag)
    {
      info = m_cConnection.read();
      alert ("info is :" + info); 
      if (info == "EXIT")
      {
          closeConnection();
          if (demon)
            { 
                var connection = pollConnection();
                if (connection != null) 
                {
                    m_cConnection =  connection;
                    continue;
                  }
            }else
            {
                return;
                
                }
      }
      if (info == "" || info == null)
      {
      $.sleep (500);
      continue;
      }
      
      doSometing(info);
        break;
    }
}
function responseToClient(info)
{
    alert("responseToClient");
  if(m_cConnection != null)
  {
      m_cConnection.writeln(info);
      readRequestFromClient();
     // m_cConnection.close();
     // delete m_cConnection;
  }else{
      alert("m_cConnection == null");
      }
}
function closeConnection()
{
     alert("closeConnection");
  if(m_cConnection != null)
  {
     m_cConnection.close();
     delete m_cConnection;
  }
}    
function stopServer()
{
    flag = false;
}
var m_cConnection = null;
var btPS = new BridgeTalk;
btPS.target = "photoshop";

function runscriptViaPS(script)
{
    alert("runscriptViaPS: "+ script);
    btPS.body = script;
    btPS.onResult = function (resObj)
    {
        alert("runscriptViaPS results: "+resObj.body );
        doc = eval(resObj.body);
        if(doc == false)
        {
            
          responseToClient("Failt to  runscriptViaPS")
        }    
        else
        {
            alert("runscriptViaPS successfully");
          //alert("sendCreateDocoPS successfully");
          responseToClient("runscriptViaPS successfully");
        }
    }
    btPS.send();

}
startServer(1234);

Python code piece:

# Echo server program
import socket
import time                
HOST = '127.0.0.1'                 # Symbolic name meaning the local host
PORT = 1234              # Arbitrary non-privileged port


createDocviaPS = "runscriptViaPS(\" var doc = app.documents.add(800, 600, 2, \\\"docRef\\\", NewDocumentMode.RGB); var r =(doc != null); r.toSource();\")";

saveAsJPGviaPS = "runscriptViaPS(\"var thistimestamp = Math.round(new Date().getTime() / 1000); 
saveFile = new File(\\\"Z:/geometrixx/test/1.jpg\\\" );saveOptions = new JPEGSaveOptions();  saveOptions.embedColorProfile = true;saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;saveOptions.matte = MatteType.NONE;saveOptions.quality = 9;var doc=app.activeDocument.saveAs(saveFile, saveOptions,true,Extension.LOWERCASE);var r =(doc != null); r.toSource();\")";

EXIT = "EXIT";

def startServer():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    print 'Connected by', addr
    while 1:
      data = conn.recv(1024)
      if not data: break
      conn.send(data)
      print 'Received', repr(data);
    conn.close()
    pass
                                                            
def startClient():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT));
    #msg = raw_input().rstrip( '\n' )
    msg = createDocviaPS;
    s.send(msg);
    data = s.recv(1024);
    print 'Received', repr(data);
    #time.sleep(400);
    msg = saveAsJPGviaPS;
    s.send(msg);
    data = s.recv(1024);
    print 'Received', repr(data);
    s.send(EXIT);
    s.close()
    pass
startClient();
""""
# Echo client program
import socket

HOST = 'daring.cwi.nl'    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
"""

No comments:

Post a Comment