39 Replies Latest reply on May 3, 2017 7:43 AM by simonh27074964

    Is it possible to load web content?

    mannyk1234

      Hello,

       

      I'd like to know if its possible to load a file (an xml file) from a web resource with a given address like: http://server.com/file.xml.

      I tried to do this with

      myDocument.importXML(File(file));

      but this does not seem to work.

       

      Thanks in advance and kind regards,

      mannyk

        • 1. Re: Is it possible to load web content?
          Kasyan Servetsky Level 5

          Check out this podcast at Kris Coppieters' blog.

           

          Kasyan

          1 person found this helpful
          • 2. Re: Is it possible to load web content?
            mannyk1234 Level 1

            Hello,

             

            thanks for the link. It helped to get the text of the web resource!

             

            However, I don't know how I can import the xml text as an xml file.

            var data = GetURL("http://server.com/xmlfile.xml",false);

            myDocument.importXML(data.body);

            does not work.

             

            Regards,

            mannyk

            • 3. Re: Is it possible to load web content?
              [Jongware] Most Valuable Participant

              What does "does not work" mean? (I.e., 'nothing happens at all', 'i get an error', 'my screen exploded', or something else.)

               

              I would think importXML only works with a file, not with an internal string. If so, you should download the file, store it locally, then use importXML.

              • 4. Re: Is it possible to load web content?
                Marijan Tompa [tomaxxi] Level 4

                Maybe you can do something like this:

                 

                var XMLurl = GetURL("http://server.com/xmlfile.xml",false);
                var myXml = new XML(XMLurl);
                myDocument.importXML(myXml.body);
                

                 

                (it's just a hint, not tested)

                 

                --

                tomaxxi

                http://indisnip.wordpress.com/

                • 5. Re: Is it possible to load web content?
                  Kasyan Servetsky Level 5

                  Here is an example -- works for me: imports an XML file from a web site into the active document.

                   

                  if (app.documents.length == 0) {
                       alert("Please open a document and try again");
                       exit();
                  }
                  var doc = app.activeDocument;
                  if (! doc.saved) {
                       alert("Please save document somewhere first");
                       exit();
                  }
                  var url = "http://kasyan.ho.com.ua/downloads/temp/xmlfile.xml";
                  var fileName = url.split("/");
                  fileName = fileName[fileName.length - 1];
                  var xmlFilePath = File(doc.filePath + "/" + fileName);

                  var xmlData = GetURL(url, false);
                  if (xmlData != null && xmlData.body != null) {
                    xmlFilePath.open("w");
                    xmlFilePath.write(xmlData.body);
                    xmlFilePath.close();
                  }

                  if (xmlFilePath.exists) doc.importXML(xmlFilePath);

                  // ****************
                  function GetURL(url,isBinary)
                  {
                    //
                    // This function consists of up to three 'nested' state machines.
                    // At the lowest level, there is a state machine that interprets UTF-8 and
                    // converts it to Unicode - i.e. the bytes that are received are looked at
                    // one by one, and the Unicode is calculated from the one to four bytes UTF-8
                    // code characters that compose it.
                    //
                    // The next level of state machine interprets line-based data - this is
                    // needed to interpret the HTTP headers as they are received.
                    //
                    // The third level state machine interprets the HTTP reply - based on the
                    // info gleaned from the headers it will process the HTTP data in the HTTP
                    // reply
                    //
                   
                    //
                    // If things go wrong, GetURL() will return a null
                    //
                    var reply = null;
                   
                    //
                    // Below some symbolic constants to name the different states - these
                    // make the code more readable.
                    //
                    const kUTF8CharState_Complete = 0; 
                    const kUTF8CharState_PendingMultiByte = 1;
                    const kUTF8CharState_Binary = 2;
                   
                    const kLineState_InProgress = 0;
                    const kLineState_SeenCR = 1;
                   
                    const kProtocolState_Status = 1;
                    const kProtocolState_Headers = 2;
                    const kProtocolState_Body = 3;
                    const kProtocolState_Complete = 4;
                    const kProtocolState_TimeOut = 5;
                   
                    do
                    {
                      //
                      // Chop the URL into pieces
                      //
                      var parsedURL = ParseURL(url);
                     
                      //
                      // We only know how to handle HTTP - bail out if it is something else
                      //
                      if (parsedURL.protocol != "HTTP")
                      {
                        break;
                      }
                     
                      //
                      // Open up a socket, and set the time out to 2 minutes. The timeout
                      // could be parametrized - I leave that as an exercise.
                      var socket = new Socket;   
                      socket.timeout = 120;
                     
                      //
                      // Open the socket in binary mode. Sockets could also be opened in UTF-8 mode
                      // That might seem a good idea to interpret UTF-8 data, but it does not work out
                      // well: the HTTP protocol gives us a body length in bytes. If we let the socket
                      // interpret UTF-8 then the body length we get from the header, and the number of
                      // characters we receive won't match - which makes things quite awkward.
                      // So we need to use BINARY mode, and we must convert the UTF-8 ourselves.
                      //
                      if (! socket.open(parsedURL.address + ":" + parsedURL.port,"BINARY"))
                      {
                        break;
                      }

                      //
                      // Dynamically build an HTTP 1.1 request.
                      //
                      if (isBinary)
                      {
                        var request =
                          "GET /" + parsedURL.path + " HTTP/1.1\n" +
                          "Host: " + parsedURL.address + "\n" +
                          "User-Agent: InDesign ExtendScript\n" +
                          "Accept: */*\n" +
                          "Connection: keep-alive\n\n";
                      }
                      else
                      {
                        var request =
                          "GET /" + parsedURL.path + " HTTP/1.1\n" +
                          "Host: " + parsedURL.address + "\n" +
                          "User-Agent: InDesign ExtendScript\n" +
                          "Accept: text/xml,text/*,*/*\n" +
                          "Accept-Encoding:\n" +
                          "Connection: keep-alive\n" +
                          "Accept-Language: *\n" +
                          "Accept-Charset: utf-8\n\n";
                      }

                      //
                      // Send the request out
                      //
                      socket.write(request);
                   
                      //
                      // readState keeps track of our three state machines
                      //
                      var readState =
                      {
                        buffer: "",
                        bufPos: 0,
                        //
                        // Lowest level state machine: UTF-8 conversion. If we're handling binary data
                        // the state is set to kUTF8CharState_Binary which is a 'stuck' state - it
                        // remains in that state all the time. If the data really is UTF-8 the state
                        // flicks between kUTF8CharState_PendingMultiByte and kUTF8CharState_Complete
                        //
                        curCharState: isBinary ? kUTF8CharState_Binary : kUTF8CharState_Complete,
                        curCharCode: 0,
                        pendingUTF8Bytes: 0,     
                        //
                        // Second level state machine: allows us to handle lines and line endings
                        // This state machine can process CR, LF, or CR+LF line endings properly
                        // The state flicks between kLineState_InProgress and kLineState_SeenCR
                        //
                        lineState: kLineState_InProgress,
                        curLine: "",
                        line: "",
                        isLineReadyToProcess: false,
                        //
                        // Third level state machine: handle HTTP reply. This state gradually
                        // progresses through kProtocolState_Status, kProtocolState_Headers,
                        // kProtocolState_Body, kProtocolState_Complete.
                        // contentBytesPending is part of this state - it keeps track of how many
                        // bytes of the body still need to be fetched.
                        //     
                        protocolState: kProtocolState_Status,
                        contentBytesPending: null,
                        dataAvailable: true,
                        //
                        // The HTTP packet data, chopped up in convenient pieces.
                        //
                        status: "",
                        headers: {},
                        body: ""
                      };

                      //
                      // Main loop: we loop until we hit kProtocolState_Complete as well as an empty data buffer
                      // (meaning all data has been processed) or until something timed out.
                      //
                      while
                      (
                        ! (readState.protocolState == kProtocolState_Complete && readState.buffer.length <= readState.bufPos)
                       &&
                        readState.protocolState != kProtocolState_TimeOut
                      )
                      {
                        //
                        // If all data in the buffer has been processed, clear the old stuff
                        // away - this makes things more efficient
                        //
                        if (readState.bufPos > 0 && readState.buffer.length == readState.bufPos)
                        {
                          readState.buffer = "";
                          readState.bufPos = 0;
                        }
                     
                        //
                        // If there is no data in the buffer, try to get some from the socket
                        //
                        if (readState.buffer == "")
                        {     
                          //
                          // If we're handling the body of the HTTP reply, we can try to optimize
                          // things by reading big blobs of data. Also, we need to look out for
                          // completion of the transaction.
                          //
                          if (readState.protocolState == kProtocolState_Body)
                          {
                            //
                            // readState.contentBytesPending==null means that the headers did not
                            // contain a length value for the body - in which case we need to process
                            // data until the socket is closed by the server
                            //
                            if (readState.contentBytesPending == null)
                            {
                              if (! readState.dataAvailable && ! socket.connected)
                              {
                                //
                                // The server is finished with us - we're done
                                //
                                socket = null;
                                readState.protocolState = kProtocolState_Complete;
                              }
                              else
                              {
                                //
                                // Attempt to read a byte
                                //
                                readState.buffer += socket.read(1);
                                readState.dataAvailable = readState.buffer.length > 0;
                              }
                            }
                            else
                            {
                              //
                              // If the socket is suddenly disconnected, the server pulled the
                              // rug from underneath us. Register this as a time out problem and
                              // bail out.
                              //
                              if (! readState.dataAvailable && ! socket.connected)
                              {
                                socket = null;
                                readState.protocolState = kProtocolState_TimeOut;
                              }
                              else
                              {
                                //
                                // Try to get as much data as needed from the socket. We might
                                // receive less than we've asked for.
                                //
                                readState.buffer = socket.read(readState.contentBytesPending);
                                readState.dataAvailable = readState.buffer.length > 0;
                                readState.contentBytesPending -= readState.buffer.length;
                                //
                                // Check if we've received as much as we were promised in the headers
                                // If so, we're done with the socket.
                                //
                                if (readState.contentBytesPending == 0)
                                {
                                  readState.protocolState = kProtocolState_Complete;
                                  socket.close();
                                  socket = null;
                                }
                                //
                                // If we're downloading binary data, we can immediately shove the
                                // whole buffer into the body data - there's no UTF-8 to worry about            
                                //
                                if (isBinary)
                                {
                                  readState.body += readState.buffer;
                                  readState.buffer = "";
                                  readState.bufPos = 0;
                                }
                              }
                            }
                          }
                          else if (readState.protocolState != kProtocolState_Complete)
                          {
                            //
                            // We're reading headers or status right now - look out
                            // for server disconnects
                            //
                            if (! readState.dataAvailable && ! socket.connected)
                            {
                              socket = null;
                              readState.protocolState = kProtocolState_TimeOut;
                            }
                            else
                            {
                              readState.buffer += socket.read(1);
                              readState.dataAvailable = readState.buffer.length > 0;
                            }
                          }
                        }
                       
                        //
                        // The previous stretch of code got us as much data as possible into
                        // the buffer (but that might be nothing, zilch). If there is data,
                        // we process a single byte here.
                        //
                        if (readState.buffer.length > readState.bufPos)
                        {
                          //
                          // Fetch a byte
                          //
                          var cCode = readState.buffer.charCodeAt(readState.bufPos++);
                         
                          switch (readState.curCharState)
                          {
                            case kUTF8CharState_Binary:
                              //
                              // Don't use the UTF-8 state machine on binary data
                              //
                              readState.curCharCode = cCode;
                              readState.multiByteRemaining = 0;
                              break;
                            case kUTF8CharState_Complete:
                              //
                              // Interpret the various UTF-8 encodings - 1, 2, 3, or 4
                              // consecutive bytes encode a single Unicode character. It's all
                              // bit-fiddling here: depending on the masks used, the bytes contain
                              // 3, 4, 5, 6 bits of the whole character.
                              // Check
                              // http://en.wikipedia.org/wiki/UTF-8
                              //
                              if (cCode <= 127)
                              {
                                readState.curCharCode = cCode;
                                readState.multiByteRemaining = 0;
                              }
                              else if ((cCode & 0xE0) == 0xC0)
                              {
                                readState.curCharCode = cCode & 0x1F;
                                readState.curCharState = kUTF8CharState_PendingMultiByte;
                                readState.pendingUTF8Bytes = 1;
                              }
                              else if ((cCode & 0xF0) == 0xE0)
                              {
                                readState.curCharCode = cCode & 0x0F;
                                readState.curCharState = kUTF8CharState_PendingMultiByte;
                                readState.pendingUTF8Bytes = 2;
                              }
                              else if ((cCode & 0xF8) == 0xF0)
                              {
                                readState.curCharCode = cCode & 0x07;
                                readState.curCharState = kUTF8CharState_PendingMultiByte;
                                readState.pendingUTF8Bytes = 3;
                              }
                              else
                              {
                                // bad UTF-8 char
                                readState.curCharCode = cCode;
                                readState.pendingUTF8Bytes = 0;
                              }
                              break;
                            case kUTF8CharState_PendingMultiByte:
                              if ((cCode & 0xC0) == 0x80)
                              {
                                readState.curCharCode = (readState.curCharCode << 6) | (cCode & 0x3F);
                                readState.pendingUTF8Bytes--;
                                if (readState.pendingUTF8Bytes == 0)
                                {
                                  readState.curCharState = kUTF8CharState_Complete;
                                }
                              }
                              else
                              {
                                // bad UTF-8 char
                                readState.curCharCode = cCode;
                                readState.multiByteRemaining = 0;
                                readState.curCharState = kUTF8CharState_Complete;
                              }
                              break;
                          }
                         
                          //
                          // If we've got a complete byte or Unicode char available, we process it
                          //
                          if (readState.curCharState == kUTF8CharState_Complete || readState.curCharState == kUTF8CharState_Binary)
                          {
                            cCode = readState.curCharCode;
                            var c = String.fromCharCode(readState.curCharCode);
                            if (readState.protocolState == kProtocolState_Body || readState.protocolState == kProtocolState_Complete)
                            {
                              //
                              // Once past the headers, we simply append new bytes to the body of the HTTP reply
                              //
                              readState.body += c;     
                            }
                            else
                            {
                              //
                              // While reading the headers, we look out for CR, LF or CRLF sequences           
                              //
                              if (readState.lineState == kLineState_SeenCR)
                              {
                                //
                                // We saw a CR in the previous round - so whatever follows,
                                // the line is now ready to be processed.
                                //
                                readState.line = readState.curLine;
                                readState.isLineReadyToProcess = true;
                                readState.curLine = "";
                                readState.lineState = kLineState_InProgress;
                                //
                                // The CR might be followed by another one, or
                                // it might be followed by a LF (which we ignore)
                                // or any other character (which we process).
                                //
                                if (cCode == 13) // CR
                                {
                                  readState.lineState = kLineState_SeenCR;
                                }
                                else if (cCode != 10) // no LF
                                {
                                  readState.curLine += c;
                                }
                              }
                              else if (readState.lineState == kLineState_InProgress)
                              {
                                //
                                // If we're in the midsts of reading characters and we encounter
                                // a CR, we switch to the 'SeenCR' state - a LF might or might not
                                // follow.
                                // If we hit a straight LF, we can process the line, and get ready
                                // for the next one
                                //
                                if (cCode == 13) // CR
                                {
                                  readState.lineState = kLineState_SeenCR;
                                }
                                else if (cCode == 10) // LF
                                {
                                  readState.line = readState.curLine;
                                  readState.isLineReadyToProcess = true;
                                  readState.curLine = "";
                                }
                                else
                                {
                                  //
                                  // Any other character is appended to the current line
                                  //
                                  readState.curLine += c;
                                }
                              }
                             
                              if (readState.isLineReadyToProcess)
                              {
                                //
                                // We've got a complete line to process
                                //
                                readState.isLineReadyToProcess = false;
                                if (readState.protocolState == kProtocolState_Status)
                                {
                                  //
                                  // The very first line is a status line. After that switch to
                                  // 'Headers' state
                                  //
                                  readState.status = readState.line;
                                  readState.protocolState = kProtocolState_Headers;
                                }
                                else if (readState.protocolState == kProtocolState_Headers)
                                {
                                  //
                                  // An empty line signifies the end of the headers - get ready
                                  // for the body.
                                  //
                                  if (readState.line == "")
                                  {
                                    readState.protocolState = kProtocolState_Body;
                                  }
                                  else
                                  {
                                    //
                                    // Tear the header line apart, and interpret it if it is
                                    // useful (currently, the only header we process is 'Content-Length'
                                    // so we know exactly how many bytes of body data will follow.
                                    //
                                    var headerLine = readState.line.split(":");
                                    var headerTag = headerLine[0].replace(/^\s*(.*\S)\s*$/,"$1");
                                    headerLine = headerLine.slice(1).join(":");
                                    headerLine = headerLine.replace(/^\s*(.*\S)\s*$/,"$1");
                                    readState.headers[headerTag] = headerLine;
                                    if (headerTag == "Content-Length")
                                    {
                                      readState.contentBytesPending = parseInt(headerLine);
                                      if (isNaN(readState.contentBytesPending) || readState.contentBytesPending <= 0)
                                      {
                                        readState.contentBytesPending = null;
                                      }
                                      else
                                      {
                                        readState.contentBytesPending -= (readState.buffer.length - readState.bufPos);
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                   
                      //
                      // If we have not yet cleaned up the socket we do it here
                      //
                      if (socket != null)
                      {
                        socket.close();
                        socket = null;
                      }
                     
                      reply =
                      {
                        status: readState.status,
                        headers: readState.headers,
                        body: readState.body
                      };
                    }
                    while (false);

                    return reply;
                  }

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

                  function ParseURL(url)
                  {
                    url=url.replace(/([a-z]*):\/\/([-\._a-z0-9A-Z]*)(:[0-9]*)?\/?(.*)/,"$1/$2/$3/$4");
                    url=url.split("/");

                    if (url[2] == "undefined") url[2] = "80";
                   
                    var parsedURL =
                    {
                      protocol: url[0].toUpperCase(),
                      address: url[1],
                      port: url[2],
                      path: ""
                    };

                    url = url.slice(3);
                    parsedURL.path = url.join("/");
                   
                    if (parsedURL.port.charAt(0) == ':')
                    {
                      parsedURL.port = parsedURL.port.slice(1);
                    }
                   
                    if (parsedURL.port != "")
                    {
                      parsedURL.port = parseInt(parsedURL.port);
                    }
                   
                    if (parsedURL.port == "" || parsedURL.port < 0 || parsedURL.port > 65535)
                    {
                      parsedURL.port = 80;
                    }
                   
                    parsedURL.path = parsedURL.path;
                   
                    return parsedURL;
                  }

                   

                  Kasyan

                  • 6. Re: Is it possible to load web content?
                    mannyk1234 Level 1

                    Thanks, I will test this today

                    • 7. Re: Is it possible to load web content?
                      Loic.Aigon Adobe Community Professional

                      One french word : chapeau !

                      • 8. Re: Is it possible to load web content?
                        RorohikoKris-3tbJf9

                        P.S. In case anyone wants additional info on how the GetURL function Kasyan used above works - as Kasyan mentioned before, check my blog post... It's a sample of three nested state machines. For CS5 you need to apply a small change - that's also explained there.

                         

                        http://rorohiko.blogspot.com/2008/07/lightning-brain-podcast-click-here-to.html

                         

                        Cheers,

                         

                        Kris

                        • 9. Re: Is it possible to load web content?
                          Kasyan Servetsky Level 5

                          Thank you, Kris.

                          I am waiting for new interesting posts on your blog.

                           

                          Kasyan

                          • 10. Re: Is it possible to load web content?
                            Marc Autret Level 4

                            Also, you can perform HTTP requests very easily by using the http module of the Extendables library from Stijn Debrouwere, as demonstrated here:

                            http://www.indiscripts.com/post/2010/11/my-first-extendables-script-for-indesign

                             

                            @+

                            Marc

                            • 14. Re: Is it possible to load web content?
                              Harbs. Level 6

                              Kris,

                               

                              Your reply didn't come through.

                              • 15. Re: Is it possible to load web content?
                                RorohikoKris-3tbJf9 Level 1

                                Duh. The Adobe forum server decided to conk out on me - it locked up as I was typing an answer and I could not get back into it any more. Now, if my reply would have been something deep and meaningful - that would have been nice. But all I meant to say was:

                                 

                                "Wow! That's pretty impressive - that'll definitely go into my toolbox!"

                                 

                                Anyway - sorry for contributing idle 'chatter' to the forums!

                                 

                                Cheers,

                                 

                                Kris

                                • 16. Re: Is it possible to load web content?
                                  Harbs. Level 6

                                  Kris, Your chatter is always welcome...

                                   

                                  Harbs

                                  • 17. Re: Is it possible to load web content?
                                    Jeremy bowmangraphics Level 2

                                    Kasyan, that's fantastic!

                                     

                                    (I can't seem to get it to work with my own php-generated XML though.)

                                     

                                    Jeremy

                                     

                                    EDIT: Now I get it: get the PHP to write its own XML file. Works a treat! Thanks!

                                    • 18. Re: Is it possible to load web content?
                                      Andreas Jansson Level 2

                                      Hi,

                                       

                                      Would it be possible to implement https support for the GetURL function?

                                       

                                      I haven't tried the extendables much, but I found no info on https, and in a simple test I didn't get it to work, so I guess it's not supported there either?

                                       

                                      Kris's GetURL works perfectly, it's just the https that we would need.


                                      Best regards,

                                      Andreas

                                      • 19. Re: Is it possible to load web content?
                                        John Hawkinson Level 5

                                        You should probably shell out to a utility like curl.

                                        • 20. Re: Is it possible to load web content?
                                          Andreas Jansson Level 2

                                          It has to work on both mac and win (CS4 and up). Some years ago (before the Socket object) we approached this problem by calling an applescript file when the operating system was Mac OSX, and a VBScript from Windows.

                                          This is not optimal, since

                                          1) it takes a fraction of a second extra for each call (executes in separate processes), and with a number of calls it adds noticably to the total time to execute, and

                                          2) there are more "problem sources", spreading the code to two other scripting languages and also two different http implementations.

                                          It works (for RPC methods), but it's not a road we would like to follow back.

                                           

                                          The GetURL function and the Extendables library use the Socket object which comes with InDesign (and other Adobe products).

                                          So that's the way I feel we need to go. It's just the HTTPS (for secure connections) that is missing.

                                          • 21. Re: Is it possible to load web content?
                                            John Hawkinson Level 5

                                            The GetURL function and the Extendables library use the Socket object which comes with InDesign (and other Adobe products).

                                            So that's the way I feel we need to go. It's just the HTTPS (for secure connections) that is missing.

                                            You do understand that doing so is...extremely difficult?

                                             

                                            The Javascript implemention offers bindings to the operating system's socket interface. Implementing HTTP on top of that is really really easy. Depending on how much of HTTP you need, it's just a few lines of code (the JavaScript Tools Guide example is 7 lines).

                                             

                                            HTTPS is a whole different animal. You have to implement a lot of code (thousands of lines, if not more!), and worse, a lot of it is cryptographic and security-critical. And generally speaking, we know it is a bad idea for people to re-implement security-critical code when they don't have to -- mistakes will be made and they will compromise security. (It's also a very good idea for there to be multiple interoperable implementations, but SSL already has that.)

                                             

                                            It is also quite plausible you would have performance issues.

                                             

                                            I suppose it's not hopeless. Probably the thing to do would be to build an ExternalObject library that lets you link against the OpenSSL libraries. Or libcurl.

                                            Good luck with that, and have fun doing it portably for both OSes.

                                             

                                            I would just shell out to curl unless it was really performance-critical, in which case I would wonder why you were doing it in InDesign...

                                            • 22. Re: Is it possible to load web content?
                                              Andreas Jansson Level 2

                                              Dear John,

                                               

                                              No, I didn't realize the complexity.

                                               

                                              I guess I was hoping that there were some Adobe built-in support for https, that just had to be turned on by setting the right properties, such as ports etc, and that I just didn't know how. (Then the call would have been made from javascript and thus from either OS.)

                                               

                                              For https, our current solution use the old way (as described above) of calling separate kinds of "http-getters" from Apple- and VB scripts for OSX and Win, respectively. Curl is probably the one in the applescript version.

                                               

                                              Not reflecting over any possible overwhelming difficulties, I wrote this question. I apologize if that upset you.

                                               

                                              Yours sincerely,

                                              Andreas Jansson

                                              • 23. Re: Is it possible to load web content?
                                                John Hawkinson Level 5

                                                Upset? No, not at all! I'm sorry if I gave that impression, it was not my intent.

                                                I just wanted you to have a detailed understanding.

                                                • 24. Re: Is it possible to load web content?
                                                  Andreas Jansson Level 2

                                                  Great then :-)

                                                   

                                                  Info: We just contacted the only customer that required https for their connections, and puh(!)... they no longer expose the service outside their own network, so there is currently no need for https support.

                                                   

                                                  A few hairs lost and somewhat more enlightened...

                                                   

                                                  Thanks,

                                                  Andreas

                                                  • 25. Re: Is it possible to load web content?
                                                    jasonr16384347 Level 1

                                                    ok I know this post is really, really old at this point -- but the code above actually has a problem for those using it post Marty-McFly 2015.

                                                     

                                                    HTTP requires a CR and LF after each header, not just a LF. This may have worked fine a few years ago but web endpoints are getting more and more strict these days with security concerns and such, and I found many servers reject HTTP requests that aren't 100% up to par.

                                                     

                                                     

                                                    ... so replace this section of code above (basically I've just replaced all the "\n"s with "\r\n"s. I this I also tweaked the headers a bit, but that's less important than just making sure it's "\r\n" not just "\n":

                                                     

                                                    .

                                                    .

                                                    .

                                                      //
                                                      // Dynamically build an HTTP 1.1 request.
                                                      //
                                                      if (isBinary)

                                                      {

                                                       var request =

                                                       "GET /" + parsedURL.path + " HTTP/1.1\r\n" +

                                                       "Host: " + parsedURL.address + "\r\n" +

                                                       "User-Agent: InDesign ExtendScript\r\n" +

                                                       "Accept: */*\r\n" +

                                                       "Connection: keep-alive\r\n\r\n";
                                                      }

                                                      else
                                                      {

                                                    var request =

                                                       "GET /" + parsedURL.path + " HTTP/1.1\r\n" +

                                                       "Host: " + parsedURL.address + "\r\n" +

                                                       "User-Agent: InDesign ExtendScript/1.0\r\n" +

                                                       "Accept: */*\r\n" +

                                                       "Accept-Encoding: gzip,deflate\r\n" +

                                                       "Content-Length: 0\r\n" +

                                                       "Content-Type: application/x-www-form-urlencoded\r\n"+

                                                    "Connection: close\r\n\r\n";
                                                      }

                                                    .

                                                    .

                                                    .

                                                    2 people found this helpful
                                                    • 26. Re: Is it possible to load web content?
                                                      Kasyan Servetsky Level 5

                                                      Thank you very much for updating the code! I've posted the whole script for the future generations of scripters here on my site in the Miscellaneous InDesign scripting tips & tricks section.

                                                       

                                                      Regards,
                                                      Kasyan

                                                      • 27. Re: Is it possible to load web content?
                                                        Trevorׅ Adobe Community Professional

                                                        Hi all,

                                                         

                                                        Here's another library worth noting

                                                        GitHub - Schreiber-und-Freunde/porky: JavaScript ExtendScript Toolkit (ESTK) automation library with SQL database, JSON,…

                                                        Please check the product website http://porky.io for code examples!

                                                         

                                                        HTH

                                                         

                                                        Trevor

                                                        • 28. Re: Is it possible to load web content?
                                                          Loic.Aigon Adobe Community Professional

                                                          IN the rare occasions I use getUrl from Rorohiko:

                                                           

                                                          Rorohiko: GetURLs.jsx - access HTTP content from InDesign ExtendScript

                                                           

                                                          I was told Extendables also has some utility

                                                          GitHub - debrouwere/Extendables: A framework for Adobe ExtendScript. Unmaintained and out of date.

                                                          But as status said, mostly out of date now.

                                                           

                                                          Last but not least, I never could something out of Porky. has anyone succeded ?

                                                           

                                                          FWIW

                                                           

                                                          Loic

                                                          • 29. Re: Is it possible to load web content?
                                                            Laubender Adobe Community Professional & MVP

                                                            Hi together,

                                                            I had success with a test using ExtendScript utilizing curl that is installed with every OSX I think.

                                                             

                                                            FWIW: With PhotoShop ExtendScript there is the method app.system() that—unfortunately—InDesign is lacking.

                                                            As shown by Fabian Morón Zirfas here:

                                                            javascript - How to download an image from url to use in Photoshop scripting (ExtendScript Toolkit) - Stack Overflow

                                                             

                                                            Something that should work with InDesign CS4 ( and above ) on Mac OSX using AppleScript's do shell script command with ExtendScript.
                                                            ( Note: The image is provided by Fabian Morón Zirfas. )

                                                             

                                                            var myScript = 
                                                            'do shell script "curl -o ~/Desktop/test.png https://dl.dropboxusercontent.com/u/73950/IMG_1827%20%282%29.png"';
                                                            app.doScript(myScript, ScriptLanguage.applescriptLanguage);
                                                            

                                                             

                                                            Curl is documented here:

                                                            https://curl.haxx.se/docs/features.html

                                                             

                                                            I'm not sure if or how curl is implemented generally with e.g. Windows 7 or Windows 10…
                                                            And if it could be easily accessed by VB Script with a doScript() that comes along with ExtendScript code.

                                                             

                                                            Regards,
                                                            Uwe

                                                            1 person found this helpful
                                                            • 30. Re: Is it possible to load web content?
                                                              Trevorׅ Adobe Community Professional

                                                              Hi Uwe,

                                                               

                                                              The curl method looks like a nice and easy option.

                                                              Here's a link for the Windows version.

                                                              vbscript - HTTP GET in VBS - Stack Overflow

                                                              I haven't tested either on Mac or Windows, but the potential of replacing Kris's masterpiece with a 4 or 5 lines certainly sounds appetizing. (No offense Kris!)

                                                               

                                                              I have never tried using porky or extendables for this. I have used Kris's, I tried to implement a product activation system with it but it didn't pass a large corporations firewall so for the activation I used a link that opened an online page that displayed a code that could only be used once and the user needed to paste the code in the edit box. Not very graceful but works well and there's no problems with firewalls.

                                                               

                                                              Regards

                                                               

                                                              Trevor

                                                              • 31. Re: Is it possible to load web content?
                                                                Loic.Aigon Adobe Community Professional

                                                                @Trevor,

                                                                I tried to implement a product activation system with it but it didn't pass a large corporations firewall

                                                                Yeah I came into the same trouble

                                                                 

                                                                Nice that you could circumvent it somehow.

                                                                • 32. Re: Is it possible to load web content?
                                                                  Trevorׅ Adobe Community Professional

                                                                  Hi Loic,

                                                                   

                                                                  I won't post a working link but you can try out this one.

                                                                  http://activation.creative-scripts.com/index.php/f72d9e737de8b2289d1a/elizabeth@palace.gov .uk/HRM/activate/

                                                                  In a working one you would get a code and instructions of how activate the program with it.

                                                                  You can have the script generate a random number and generate a matching link, the link will only work with that particular installation because only that installation will have that random number.  The link will automatically update the online SQL and once the links been used once it will no longer work.

                                                                  With a bit of creativity should should be able to get a working system.

                                                                   

                                                                  HTH

                                                                   

                                                                  Trevor

                                                                  • 34. Re: Is it possible to load web content?
                                                                    simonh27074964

                                                                    Hi all,

                                                                     

                                                                    I'm currently developing a windows/osx plugin for Illustrator CC 2017 which imports an image from a remote url.

                                                                     

                                                                    The getUrl() function works wonders in Photoshop, but I'm running into problems in illustrator because it doesn't support sockets. I have played with a BridgeTalk solution, but I was wondering if anybody knew of an alternative method? Is curl the way to go?

                                                                     

                                                                    Thanks in advance for any feedback.

                                                                    • 36. Re: Is it possible to load web content?
                                                                      Silly-V Adobe Community Professional

                                                                      I was using the Bridge HttpConnection object (ExternalObject) but it quit working in Bridge 2017. I'm just telling the client to use Bridge previous to the 2017 version, but a CEP extension is going to be the way going forward.

                                                                      1 person found this helpful
                                                                      • 37. Re: Is it possible to load web content?
                                                                        simonh27074964 Level 1

                                                                        Thanks Stefan and Silly-V. I had already created an extension, but I solved the problem by moving the dependency for the http get request from extendscript/socket object to the application side. Once I had the file data, I passed it in via evalScript.

                                                                        • 38. Re: Is it possible to load web content?
                                                                          Silly-V Adobe Community Professional

                                                                          So to understand correctly, you were using socket object in .jsx scripts, but now are using the CEP-side javascript such as jQuery's $.get() ?

                                                                          • 39. Re: Is it possible to load web content?
                                                                            simonh27074964 Level 1

                                                                            Exactly.

                                                                             

                                                                            Some sample code:

                                                                             

                                                                              var downloadAndOpenInIllustrator = function(url, id, preview) {

                                                                                var xhr = new XMLHttpRequest();

                                                                                xhr.open('GET', url, true);

                                                                                xhr.responseType = 'arraybuffer';

                                                                                xhr.onload = function(e) {

                                                                                  if (this.status == 200 || this.status == 304) {

                                                                                    var uInt8Array = new Uint8Array(this.response);

                                                                                    var i = uInt8Array.length;

                                                                                    var binaryString = new Array(i);

                                                                                    while (i--)

                                                                                      binaryString[i] = String.fromCharCode(uInt8Array[i]);

                                                                                    var data = binaryString.join('');

                                                                                    var base64 = window.btoa(data);

                                                                             

                                                                                    var downloadedFile = createTempFolder() + id + '-' + preview + '.jpg';

                                                                                    window.cep.fs.writeFile(downloadedFile, base64, cep.encoding.Base64);

                                                                             

                                                                                    if (preview) csInterface.evalScript(`placeImagePreviewIllustrator("${downloadedFile}", "${id}", "preview");`, function(result){  

                                                                                      console.log(result);  

                                                                                    });

                                                                                    else csInterface.evalScript(`placeImageLicensedIllustrator("${downloadedFile}", "${id}", "");`); 

                                                                                  }      

                                                                                };

                                                                                xhr.send();

                                                                              };

                                                                             

                                                                            I used this as a reference: Samples/Flickr at master · Adobe-CEP/Samples · GitHub