I have in my hands a script that downloads a photo via the internet and opens it in Photoshop.
// OpenImageFromWeb.jsx
// Copyright 2006-2009
// Written by Jeffrey Tranberry
// Photoshop for Geeks Version 3.0
// modified by MLH
// modified by JJMACK 2010
/*
<javascriptresource>
<about>$$$/JavaScripts/OpenImageFromWeb/About=JJMack's OpenImageFromWeb.^r^rCopyright 2010 Mouseprints.^r^rJJMack's Script.^rOpen Image From Web as a Placed smart object layer!</about>
<category>JJMack's Script</category>
</javascriptresource>
*/
/*
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 html = "";
var request = "";
var url = "";
var binary = "";
var requesthtml = "";
var socket = new Socket;
var sImg = "/old/dpr/JJMack8btiSrgb.png"; // the rest of the url for the file we want
var port = ":80"; // the port for the file we want
/////////////////////////
// MAIN
/////////////////////////
var url = prompt("Enter the image's full URL http://domain/full image path",url); // prompt for domain name if (url != null && url != ""){
if ( (url.indexOf("https://") != -1) || (url.indexOf("HTTPS://") != -1)) {
//alert("Strip)")
save = url.substr(8,);
url = "http://" + save;
}
if ( (url.indexOf("http://") != -1) || (url.indexOf("HTTP://") != -1) ) {
domainPathLength = url.length - "http://".length;
domainPath = url.substr(7, domainPathLength);
pathOffset = domainPath.indexOf("/");
domain = domainPath.substr(0, pathOffset);
sImg = domainPath.substr(pathOffset, domainPath.length - pathOffset );
// Isolate Image name
var Name = sImg
var imagePath = "";
while (Name.indexOf("/") != -1 ) { // Strip Path
imagePath= imagePath + Name.substr(0, Name.indexOf("/") + 1);
Name = Name.substr(Name.indexOf("/") + 1 ,);
}
//alert("domain = " + domain + " , Image = " + sImg + " Image File Name = " + Name);
if ( domain != "" && sImg != "" && sImg != "/" && Name.indexOf(".") != -1 ) {
var f = File("~/Desktop/" + Name); // Image file name
f.encoding = "binary"; // set binary mode
f.open("w");
if (socket.open(domain + port, "binary")){
//alert("GET " + sImg +" HTTP/1.1\n\n");
requesthtml ="\n\nDmain:" + domain + " Port" + port + " binary\n"
//request ="GET " + sImg +" HTTP/1.0\n\n"
request ="GET " + sImg +" HTTP/1.1\nHost: " + domain + "\nAccept: image/gif, image/x-xbitmap, image/png, image/jpeg, */*\n\n";
//alert(request);
socket.write(request); // get the file
//alert("After Request");
var binary = socket.read(99999999);
//alert("remove Header");
binary = removeHeaders(binary);
//alert("Write File")
f.write(binary);
socket.close();
}
else { alert("Connection to Domain:" + domain + " Port" + port + " Failed ");}
//alert("Close");
f.close();
if (binary.length != 0) {
//alert ("file length = " + binary.length );
if(app.documents.length == 0) {
//app.documents.add([width] [, height] [, resolution] [, name] [, mode] [, initialFill] [,pixelAspectRatio] [, bitsPerChannel] [,colorProfileName])
app.documents.add(new UnitValue(3840,'px'), new UnitValue(2160,'px'), 72, null, NewDocumentMode.RGB, DocumentFill.WHITE, 1,BitsPerChannelType.EIGHT, "sRGB IEC61966-2.1" );
}
placeSmartObject( f );
}
f.remove(); // Remove temporary downloaded files
}
else { alert("Invalid Image URL: " + url ); }
}
else { alert("Invalid URL: " + url ); }
}
else { if ( url == "" ) alert("No URL Entered"); }
/////////////////////////
// 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) { alert("Placeing file: '" + fileRef + "' failed"); }
};
// Remove header lines from HTTP response
function removeHeaders(binary){
var bContinue = true ; // flag for finding end of header
var line = "";
var httpheader = "";
var nFirst = 0;
var count = 0;
while (bContinue) {
line = getLine(binary) ; // each header line
httpheader = httpheader + line;
bContinue = line.length >= 2 ; // blank header == end of header
nFirst = line.length + 1 ;
binary = binary.substr(nFirst) ;
}
if (httpheader.indexOf("Bad Request") != -1 || httpheader.indexOf("Not Found") != -1) {
alert (requesthtml + request + httpheader);
var binary = "";
}
//alert (requesthtml + request + httpheader + "\nFile length = " + binary.length);
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[i] ;
}
return line ;
};