3 Replies Latest reply on Jan 16, 2011 9:14 PM by Haakenlid

    Creating InDesign from Javascript

    Bommannan.R

      Hello,

       

      I have a WCF WebService written in C#.NET which returns an IDML file content as a ByteArray. From Javascript file, I have consumed it and retried the ByteArray in a javascript variable. Javascript runs inside the InDesign Workstation.

       

      Can anyone let me know,

      1. How can I open that ByteArray as an InDesign file within the WorkStation? or

      2. How Can I merge that as a New Layer into an Existing File?

       

      Following is the code snippet I used to Call WebService and Retrieve the ByteArray:

       

      function httpconn(method,ssl)
      {
          // todo: implements test for set of methods
          this._coveredMethods = ["GET", "POST"];
          this.method = (method == undefined) ? "GET" : method;
          this.port = (ssl == undefined) ? 80 : ssl;
          this.init();
      }


      httpconn.prototype.setUrl=function(url)
      {
          this.url = url;
          this._configureSocket();
          return this;
      }


      httpconn.prototype.init=function()
      {
          this.socket = new Socket;
          this.socket.timeout = 15;
          this._parsedparam = new Object();
          this.params = new Object();
          this.contentType = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8; ";
          this.extraHeader = "";
      }


      httpconn.prototype.clean=httpconn.prototype.init;


      httpconn.prototype._configureSocket=function()
      {
          var _urlsplit = this.url.replace(/[http|https]*[\:\/\/]*/,"");
          _urlsplit = _urlsplit.split("/");
          this.host = _urlsplit[0];
          if (_urlsplit[1] != undefined )
          {
              _urlsplit.shift();
              this.path = "/" + _urlsplit.join("/");
          }
          else
          {
              this.path = "/";
          }
          return this;
      }


      httpconn.prototype._parseParam=function()
      {
          var _parsedparam = "";
          for (i in this.params)
          {
              _parsedparam = _parsedparam + ( ( (_parsedparam.length!=0) ? "&" : "") + i+ "=" +String(this.params[i]) );
          }
          this._parsedparam.str = _parsedparam;
          this._parsedparam.length = _parsedparam.length;
          return this;
      }


      httpconn.prototype.addParam=function(name, value)
      {
          this.params[name] = value;
          return this;
      }


      httpconn.prototype.setContentType=function(ct)
      {
          this.contentType = ct + "\n";
      }


      httpconn.prototype.addHeader=function(headerLine)
      {
          this.extraHeader+=headerLine + "\n";
      }


      httpconn.prototype.request=function()
      {
          var _request = this.method;

          if (this.method=="POST")
              _request += " " + this.path + " HTTP/1.1\n";
          else
              _request += " " + this.path + "?" + this._parsedparam.str + " HTTP/1.1\n";

          _request += "Host: " + this.host + ":" + this.port + " \n" + "User-Agent: Arizona Systems\n" + "Accept: */*\n" + this.contentType;
          _request += this.extraHeader;
          if (this.method=="POST")
          _request += "Content-Length: " + this._parsedparam.length + "\n";

          _request += "Connection: keep-alive\r\n\r\n";

          alert("host:"+this.host + "\n method:" + this.method+"\n port:" + this.port + "\n dados:" + this._parsedparam.str);
          alert("request:" + _request);
          this.socket.listen(this.port);
          this.socket.open(this.host + ":" + this.port);
          this.socket.write(_request);
          this.socket.write(this._parsedparam.str);
          //    this.clean();
      alert("Success");
          return this.socket.read();
      }

       

      //Calling WebService

      http = new httpconn("GET", 45640);
      foo="http://127.0.0.1/Service1.svc/bytedata?boo=welcometosampletextfilecreation";

      var retorno = http.setUrl(foo).request();

      // retorno has the ByteArray data
      alert("Result :" + retorno);
      alert("Length: " + retorno.length);

       

      Thanks in Advance for your suggestions and help.

       

      Regards,

      Bommannan.R

        • 1. Re: Creating InDesign from Javascript
          Haakenlid Level 3

          I think you have to create a File object and write the byteArray to that, with File.write();

          Then you close the File object and give it a filename with .indd extention and open it in InDesign with with app.open()

           

          Here's some code to get you started.

          This has not been tested, and I'm not really sure if this is the correct way of writing a binary file.

          But I think it should be something like this:

           

          var myFile = createFileFromBinary(myByteArray);
          var myTargetDocument = app.activeDocument;
          var myImportedDocument = app.open(myFile);
          var myLayer = myImportedDocument.layers.add("");
          myLayer.name = "ImportedStuff";
          myImportedDocument.pageItems.everyItem().move(myLayer);
          
          for (var p = 0; p<myImportedDocument.pages.length; p++){
               if (p>myTargetDocument.pages.length){
                    myTargetDocument.pages.add();
               }
               myImportedDocument.pages[p].pageItems.everyItem().duplicate(myTargetDocument.pages[p]);
          }
          
          myImportedDocument.close(SaveOptions.NO);
          myFile.remove(); // you might want to skip this part. It will delete the temporary .indd file.
          
          function createFileFromBinary(myByteArray){ // String -> File
               // WARNING: this function is very much untested
               var myFile = new File("somepath/nameofmytemporaryfile.indd");
               myFile.open("BINARY"); 
               myFile.write(myByteArray);
               myFile.close();
               return myFile;
          }
          
          1 person found this helpful
          • 2. Re: Creating InDesign from Javascript
            Bommannan.R Level 1

            Thank you Haakenlid. This was helpful to me in creation of IDML. Actually, I was getting the content interms of XML. so, i had to write them to an IDML file.

             

            Thanks for your reply. I'll post the completed code; once it fully finished.

            • 3. Re: Creating InDesign from Javascript
              Haakenlid Level 3

              You're welcome.