Skip to main content
This topic has been closed for replies.
Correct answer Paul Riggott

The problem is in the extension, the code is set up for a 3 character extension, to get it to work use:-


var f = File("~/socket_sample_" + sImg.substr(sImg.length-5)); // 4 = .gif or .jpg

3 replies

Participant
July 1, 2010

that example does work with using the image provided in the code, but if I try and put in my own image from a location it does not work.. for example the following image would not load

var domain = "www.wallpapers247.com" // the domain for the file we want

var images = ["/images/wallpapers/eiffel-tower-1024-833291.jpeg"];

Anyone able to help?

Paul Riggott
Paul RiggottCorrect answer
Inspiring
July 1, 2010

The problem is in the extension, the code is set up for a 3 character extension, to get it to work use:-


var f = File("~/socket_sample_" + sImg.substr(sImg.length-5)); // 4 = .gif or .jpg

Inspiring
July 1, 2010

A better fix might be

var f = File("~/socket_sample_" + sImg.match( /(.*)(\.[^\.]+)/ )[2]);

And unless you edited the script it does not take an array so the brackets in

var images = ["/images/wallpapers/eiffel-tower-1024-833291.jpeg"];

might also be a problem.

Inspiring
June 24, 2010

If you have a newer version of Photoshop that supports sockets you can do something like this.

// openImageFromWeb.jsx
// Copyright 2006-2009
// Written by Jeffrey Tranberry
// Photoshop for Geeks Version 3.0

// modified by MLH

/*
Description:
This sample script shows how to download images from a web server using the
Socket object.
*/

// Note: Socket.read() parameter & behavior
// Socket.read() will read or time out. It may not read all data fromserver.
// Socket.read(999999) will read 999999 bytes, or timeout, or socket will be
// closed by the server.

// enable double clicking from the
// Macintosh Finder or the Windows Explorer
#target photoshop

// Make Photoshop the frontmost application
app.bringToFront();

/////////////////////////
// SETUP
/////////////////////////

var socket = new Socket;
var html = "";
var domain = "www.adobe.com" // the domain for the file we want
var sImg = "/ubi/globalnav/include/adobe-lq.png"; // the rest of the url for the file we want
var port = ":80"; // the port for the file we want

/////////////////////////
// MAIN
/////////////////////////


var f = File("~/socket_sample_" + sImg.substr(sImg.length-4)); // 4 = .gif or .jpg
f.encoding = "binary"; // set binary mode
f.open("w");

if (socket.open(domain + port, "binary")){
        // alert("GET " + sImg +" HTTP/1.0\n\n");
        socket.write("GET " + sImg +" HTTP/1.0\n\n"); // get the file
        var binary = socket.read(9999999);
        binary = removeHeaders(binary);
        f.write(binary);
        socket.close();
}

f.close();
if(app.documents.length == 0) app.documents.add(new UnitValue(200,'px'), new UnitValue(200,'px'), 72, 'Untitled 1');
placeSmartObject( f );
f.remove(); // Remove temporary downloaded files

/////////////////////////
// FUNCTIONS
/////////////////////////
function placeSmartObject(fileRef){
//create a new smart object  layer using a file
     try {
          var desc = new ActionDescriptor();
               desc.putPath( charIDToTypeID( "null" ), new File( fileRef ) );
              desc.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ),charIDToTypeID( "Qcsa" ));
              desc.putUnitDouble( charIDToTypeID( "Wdth" ),charIDToTypeID( "#Prc" ), 100 );
              desc.putUnitDouble( charIDToTypeID( "Hght" ), charIDToTypeID( "#Prc" ), 100 );
              desc.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), 0 );
              desc.putBoolean( charIDToTypeID( "Lnkd" ), true );
               executeAction( charIDToTypeID( "Plc " ), desc, DialogModes.NO );
               activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER);
               activeDocument.revealAll();
      } catch (e) {
  if (!e.toString().match(/Place.+is not currently available/)) {
      throw e;
      }
  }
};
// Remove header lines from HTTP response
function removeHeaders(binary){
        var bContinue = true ; // flag for finding end of header
        var line = "";
        var nFirst = 0;
        var count = 0;
        while (bContinue) {
        line = getLine(binary) ; // each header line
        bContinue = line.length >= 2 ; // blank header == end of header
        nFirst = line.length + 1 ;
        binary = binary.substr(nFirst) ;
        }
        return binary;
}


// Get a response line from the HTML
function getLine(html){
        var line = "" ;
        for (var i = 0; html.charCodeAt(i) != 10; i++){ // finding line end
        line += html ;
        }
        return line ;
}

You may have to check your firewall setting if you have one and it doesn't work with some servers. At least I can not get it to work with some.

Muppet_Mark-QAl63s
Inspiring
June 24, 2010

I've not seen that 'socket' thing before what versions of CS can you do that with Mike? and is it available across the suite? I should imagine its a lot more robust than what I had in mind too. curl did work when I checked using bridge to make the system call…

Inspiring
June 24, 2010

CS3 or higher. You may be able to use Bridgetalk and access the file by HTTP with CS2, I can't remember.

Muppet_Mark-QAl63s
Inspiring
June 24, 2010

On the mac I would look to using app.system() and passing it 'curl -o' 'localPath' 'remotePath' then place the file as smart object. Im at a CS2 mac at the moment so I can't test this…