• ps script http通信


    // Copyright 2008. Studio Melipone. All rights reserved.
    // Licence GNU
    LGPL
    // Send the active document to the UpShot web service
    (http://upshotit.com)
    // The document is sent as a .png file, as a draft on
    the user's account.
    // Therefore you must have a document opened and Adobe
    Bridge installed to perform this script.

    /*
     
     <javascriptresource>
          <name>UpShot</name>
        
     <type>automate</type>
          <about>
          Script for
    Upshot
          Copyright 2008 Studio Melipone
          
    <http://upshotit.com>
          </about>
        
     <enableinfo>true</enableinfo>
       </javascriptresource> 
     
    */

    //#target photoshop
    #include "Base64.jsx"


    // 
    getSource
    //   In CS2, the function 'toSource()' is broken for functions. The
    return
    //   value includes the text of the function plus whatever else is
    left
    //   in the file.
    //   This function gets around the problem if you
    follow some conventions.
    //   First, the function definition _must_NOT_ end
    with a ';'.
    //   Second, the next line must contain '// EOF'
    //   The
    result string can then be passed on to Bridge as needed.
    //   ftn is the
    (variable) name of the function
    //
    function getFunctionSource(ftn) {
     
    if (isCS2()) {
        var str = ftn.toSource();
        var len = str.indexOf("//
    EOF");
        return str.substring(0, len) + ')';
      }

      return
    ftn.toSource();
    };

    function bridgeUpload(file, login, pass, buffer)
    {

      function _bridgeUpload(file, login, pass, buffer) {
       
    if(!ExternalObject.webaccesslib ) {
          ExternalObject.webaccesslib = new
    ExternalObject('lib:webaccesslib');
        }
        var http = new
    HttpConnection('http://127.0.0.1:3000/en/users/get_id.xml') ;
       
    http.response = idfile ;
        http.username = login;
        http.password =
    pass;
        http.mime='text/xml';
        http.responseencoding='utf8';
       
    http.execute();
        http.response.close();
        http.close();
       
    idfile.open("r");
        var str = idfile.read();
        idfile.close();
       
    var send = new HttpConnection('http://127.0.0.1:3000/en/users/' +
               
                          str + '/upshots') ;
        send.method = 'POST';
       
    send.username = login;
        send.password = pass;
       
    send.mime='text/xml';
       
    send.requestheaders=['Host','http://localhost:3000'];
       
    send.requestheaders=['Accept','*/*'];
       
    send.requestheaders=['Content-Type','text/xml'];
        send.request =
    ('<upshot><file_name>' + f.name +' </file_name><size>'
    +
                       f.length + '</size><javafile>' + buffer
    +
                       '</javafile></upshot>');
       
    send.execute();
        /* ??? What is this for ??? The 'return' was missing ???
    */
        return idfile.toSource();
      }
      // EOF
      // The EOF must
    occur immediately after the function and the function
      // must not be
    terminated by a ;

      if (BridgeTalk.appName == "bridge") {
       
    _bridgeUpload(file, idfile, login, pass, buffer);
        return;
      }

     
    var src = _bridgeUpload.toSource();

      var brCode = ("function _run(file,
    idfile, login password) {\n" +
                    "  var getMetadata = " + src +
    ";\n\n" +
                    "  return getMetadata(files);\n" +
                 
      "};\n" +
                    "_run(" +
                    file.toSource() + ',
    ' +
                    idfile.toSource() + ', ' +
                   
    login.toSource() + ', ' +
                    pass.toSource() +
                 
      ");\n");

      // create a new BridgeTalk message object
      var bt = new
    BridgeTalk;
      // target the Adobe Bridge application
      bt.target =
    bridgeTarget;
      //p173 of Javascript Tools Guide for CS3 for http
    message
      bt.body = brCode;

      bt.onResult = function(result) {
       
    var object = bt.result = eval(result.body);
        //file.remove();
       
    //object.remove();
        //bridge.quit ();
        //eval(result.body);
     
    };

      bt.onError = function( message ) {
        var errCode = parseInt
    (message.headers ["Error-Code"]);
        throw new Error (errCode,
    message.body);
      } ;

      //send the message ( also launch the targetted
    application)
      // 'send' with a timeout arg only works in CS3
      // you
    need to use sendSync from XBridgeTalk
      bt.send(10);

      return
    bt.result;
    };

    //////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////
    /*functions
    from <http://www.ps-scripts.com/bb/viewtopic.php?t=1282>
    */
    //////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////

    function
    getAppSpecifier(appName) {
      if (isCS2()) {
        if (appName ==
    'photoshop') {
          return 'photoshop-9.0';
        }
        if (appName ==
    'bridge') {
          return 'bridge-1.0';
        }
        // add other apps
    here
      }

      if (isCS3()) {
        if (appName == 'photoshop') {
       
      return 'photoshop-10.0';
        }
        if (appName == 'bridge') {
         
    return 'bridge-2.0';
        }
        // add other apps here
      }

     
    return undefined;
    };

    function isCS2() {
      var appName =
    BridgeTalk.appName;
      var version = BridgeTalk.appVersion;

      if
    (appName == 'photoshop') {
        return version.match(/^9\./) != null;
     
    }
      if (appName == 'bridge') {
        return version.match(/^1\./) !=
    null;
      }

      return false;
    };
    function isCS3() {
      var appName
    = BridgeTalk.appName;
      var version = BridgeTalk.appVersion;

      if
    (appName == 'photoshop') {
        return version.match(/^10\./) != null;
     
    }
      if (appName == 'bridge') {
        return version.match(/^2\./) !=
    null;
      }

      return false;
    };

    function main() {
     
    app.bringToFront();

      if (BridgeTalk.appName == "bridge") {
        //
    this is for testing only
        var file = "someFilename";
        var idfile =
    "somepath/id.xml") ;
        var login = "someLogin";
        var pass =
    "somePass";
        var f = new File(filepath);
        f.open();
       
    f.encoding = 'BINARY';
        var str = f.read();
        f.close();

       
    var buffer = base64encode(str);

        bridgeUpload(file, idfile, login,
    pass, buffer);

        return;
      }

      if (app.documents.length==0) {
    // is a document opened ?
        alert("There are no Photoshop documents opened
    !");
        return;
      }

     
    /********************************************************/

      // Only
    Bridge can use HttpConnection, so we test if it is installed
      var
    bridgeTarget = BridgeTalk.getSpecifier(getAppSpecifier("bridge"));
     
     
    if( !bridgeTarget ) {
        alert("Adobe Bridge not installed, needed to
    continue.");
        return;
      }

      app.preferences.rulerUnits =
    Units.PIXELS;
      app.displayDialogs = DialogModes.NO;

     
    /*********************************************/
      /* STEP 1 : save current
    document as image file (temporary) */
     
    /******************************************/
      var docRef =
    activeDocument;
      //create the image file in the installation folder of
    Photoshop
      var filepath=app.path.toString()+"/"+docRef.name+".jpg";
      var
    file = new File(filepath);
      //var options = new
    ExportOptionsSaveForWeb();
      //options.format = SaveDocumentType.PNG;
     
    var options = new JPEGSaveOptions();
      options.quality=8;
      docRef.saveAs
    (file, options, true, Extension.LOWERCASE);
      //docRef.exportDocument (file,
    ExportType.SAVEFORWEB , options);
      file.close();


     
    /**********************************/
      /* STEP 2 : retrieve user's login
    & password */
      /*******************************/
     
      res =
    "dialog { text: 'UpShot authentication', \
                   info: Panel {
    orientation: 'column', alignChildren:'right', \
                             
     text: 'Please Identify Yourself',                   \
                          
        login: Group { orientation: 'row',                  \
                       
              s: StaticText { text:'Login :' },                 \
                 
                    e: EditText { characters: 30 }                    \
           
                       },                                                  \
        
                          passwd: Group { orientation: 'row',                 \
     
                                s: StaticText { text:'Password :' },               
    \
                                  e: EditText { characters: 30,
    properties:{noecho: true} }, \
                               }                 
                                       \
                         },                 
                                          \
                   buttons: Group {
    orientation: 'row',                          \
                            okBtn:
    Button { text:'OK', properties:{name:'ok'} },    \
                          
     cancelBtn: Button { text:'Cancel', properties:{name:'cancel'} } \
              
        }                                                             \
           
        }";

      dlg = new Window (res);
      dlg.center();
     
    dlg.show();

      var idfile = new File(app.path + "/id.xml") ;
      var
    login = dlg.info.login.e.text;//retrieve the values given in the form
      var
    pass = dlg.info.passwd.e.text;

      /******************************/
      /*
    STEP 3 : send image through Bridge */
     
    /***************************/

      var f = new File(filepath);
     
    f.open();
      f.encoding = 'BINARY';
      var buffer = f.read();
     
    f.close();
      var b64buffer = base64encode(str);

      alert("file size
    "+file.length);
      alert("f size "+f.length);
      alert("BUF "+ buffer);
     
    // I thinks this
      //     alert(base64encode("B64
    "+base64encode(buffer)));
      // should be this
      alert("B64 "+
    b64buffer);

      bridgeUpload(file, idfile, login, pass,
    b64buffer);

       /**********************************************/
       /*
    STEP 4: Once all done, delete the image previously created */
     
     /*******************************************/
    };

    main();

    //
    EOF

  • 相关阅读:
    GZIPInputStream 流未关闭引起的内存泄漏问题
    Java ExcutorService优雅关闭方式
    redis pipline
    LeetCode 30与所有单词相关联的字串
    Scala不使用null 而使用Option,None,Some的好处
    记录: 一次解决整型溢出攻击(使用scala,隐式转换)
    Scala 封装可break和continue的foreach循环
    记录: 百度webuploader 分片文件上传java服务器端(spring mvc)示例的优化
    HBase shell 中的十六进制数值表示
    关于getSystemResource, getResource 的总结
  • 原文地址:https://www.cnblogs.com/fx2008/p/3098548.html
Copyright © 2020-2023  润新知