6 Replies Latest reply on May 14, 2015 12:08 AM by oleh.melnyk

    Placing an image on my page using a URL

    Roy Marshall Level 1

      Hi.

       

      I have no idea if this is possible, but would love to hear if anyone can point me in the right direction.

      Using an InDesign Javascript I can piece together a URL, that when pasted directly into a browser displays an image that I need to place into InDesign.

      Here is the URL

      http://XXXXXXXXXXX.co.nz/login/SearchService?SERVICE_REQUEST_TYPE=4&SERVICE_REQUEST_USERID =1650&SERVICE_REQUEST_ASSETID=84035&SERVICE_REQUEST_PASSKEY=xxxxxxxx

       

      You will have to believe me that putting in the proper data instead of the xxxx does show an image in my browser

       

      Is there a way I can grab the image, and place into InDesign using Javascript?

      I know I may have to download to a temporary location first which is fine.

      Maybe some way to encode to base64....

       

      I would like to keep this platform independent, but if I need to resort to injecting a bit of applescript, I can live with it.


      Hopefully this is enough for now to get some suggestions!


      Thanks in advance,


      Roy

        • 1. Re: Placing an image on my page using a URL
          Vamitul Level 4

          you can get the image using the Socket object. In the Javascript Tools Guide Pdf, ~page 190 you will find a working example that gets a html file, but it can easily be adapted to get a image.

          then here is also Rorohiko: July 2008

          • 2. Re: Placing an image on my page using a URL
            Roy Marshall Level 1

            Hi

            Thanks for the reply.

            I do use sockets for other web page content that I need to access, but thats only to access text/data in the reply back from the connection read.

            What I am trying to do here is return back an image from this page (that is all the physical page contains) and use it in InDesign.

             

            I have tried different variations of the call, but the reply is always nothing. Even if it was to return something, all it would be would be a link to the image online wouldn't it? How would I use that?

            Anyway, here is the line of code I am using in full so you can see what I mean.

            http://redworksnz.co.nz/login/SearchService?SERVICE_REQUEST_TYPE=4&SERVICE_REQUEST_USERID= 1650&SERVICE_REQUEST_ASSETID=84035&SERVICE_REQUEST_PASSKEY=password

            There is nothing here that is really sensitive.

             

            I would appreciate any further comments.

             

            Thanks again

             

            Roy

            • 3. Re: Placing an image on my page using a URL
              Vamitul Level 4
              var s=new Socket();
              s.open('redworksnz.co.nz:80');
              s.write ('GET login/SearchService?SERVICE_REQUEST_TYPE=4&SERVICE_REQUEST_USERID=1650&SERVICE_REQUEST_ASSETID=84035&SERVICE_REQUEST_PASSKEY=password');
              var r= s.read();
              $.write(r);
              

               

              You get a CDATA block as a reply that should contain the image.

              • 4. Re: Placing an image on my page using a URL
                Roy Marshall Level 1

                Hey.

                Thanks for that.

                After a quick test, I get

                <![CDATA[]]>Result: undefined

                doesnt look right...

                 

                I did get Chris (Rorohiko) script to work, but just need analyse further.

                • 5. Re: Placing an image on my page using a URL
                  oleh.melnyk Level 3

                  Hi Roy,

                   

                  I'm also interested how to download / upload files using ExtendScript.

                  I tried to make script, while it's not able get successfully download image, it do something close: read response from server and write png file to desktop - the problem is that "saved" file is too small, I haven't yet figured out why, but maybe you will be more successful with that. Will keep working on that later today. And if you find solution - let me know

                   

                  var reply = "";
                  var conn = new Socket();
                  var site = "redworksnz.co.nz:80";
                  var path = "/login/SearchService?SERVICE_REQUEST_TYPE=4&SERVICE_REQUEST_USERID=1650&SERVICE_REQUEST_ASSETID=84035&SERVICE_REQUEST_PASSKEY=password";
                  
                  
                  if (conn.open(site)) 
                  {
                      // send a HTTP GET request
                      conn.write ("GET "+path+" HTTP/1.0\n\n");
                      // and read the server’s reply
                      reply = conn.read(999999999); //
                      conn.close();
                  
                  
                      alert(reply.toString(), "Response from server");
                  }
                  else
                  {
                      alert(conn.error);
                  }
                  
                  
                  if(reply){
                      ext = reply.split("\n\n")[0].match(/^Content-Type:\s\w+\/(\w+)/im)[1]; // extension: png
                     
                      body = reply.split("\n\n")[1];
                     
                      alert(body.length, "Response body length");
                     
                      var f = File(Folder.desktop.fsName+"/SearchService."+ext);
                      if ( f.open('wb') )
                      {       
                          f.write(body);
                          f.close();
                      }   
                  }
                  
                  • 6. Re: Placing an image on my page using a URL
                    oleh.melnyk Level 3

                    This version seems to download and write everything from server to file, but png file still corrupted - probably because I dont know how to write binary files properly

                    // something useful here http://www.ps-scripts.com/bb/viewtopic.php?t=2665
                    
                    var site = "redworksnz.co.nz:80";
                    var path = "/login/SearchService?SERVICE_REQUEST_TYPE=4&SERVICE_REQUEST_USERID=1650&SERVICE_REQUEST_ASSETID=84035&SERVICE_REQUEST_PASSKEY=password";
                    
                    var conn = new Socket();
                    //conn.encoding = "UTF-8"; // encoding: "ASCII", "binary", or "UTF-8"
                    
                    if (conn.open (site)) //, "BINARY"
                    {
                        $.writeln("Connected, wait!");
                        
                        conn.timeout=6000;
                        conn.write("GET "+path+" HTTP/1.0\n\n");
                        reply = conn.read(399999999999);
                        conn.close();
                        
                        alert(reply.toString(), "Response from server");
                        $.writeln("Response length: " + reply.length);
                    }
                    else
                    {
                        $.writeln("Error: " + conn.error);
                    }
                    
                    if(reply){
                        ext = reply.split("\n\n")[0].match(/^Content-Type:\s\w+\/(\w+)/im)[1]; // extension: png
                        
                        tmp = reply.split("\n\n");    
                        tmp.shift();
                        
                        body = tmp.join("\n\n");    
                    
                        var f = File(Folder.desktop.fsName+"/SearchService."+ext); //.txt
                        if ( f.open('w') )
                        {
                            //f.encoding = "binary";
                            f.write(body);
                            //f.rename(fileName);
                            f.close();
                        }
                        $.writeln("Done!");
                    }