Skip to main content
Known Participant
January 12, 2021
Answered

Opening a remote image in Photoshop

  • January 12, 2021
  • 1 reply
  • 1475 views

In a script code is there a way to ask photoshop to download a photo from the internet and open it?


Example; I have an image on the internet "https://upload.wikimedia.org/wikipedia/commons/e/ec/Sunset_in_Manaus.jpg"

I would like it to open in Photoshop via script. Would it be possible?

* Photoshop would download the photo to the pc and then open it.



This topic has been closed for replies.
Correct answer JJMack

If you can connect to the servers html port I had a scripot the could. I do not thinsk it could  do is with https. I think I rememver I was able to change https to http and  do it. I believe I took the socket cone from and adobe sample script. The Script will most like not work  in 2021

 

// 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 domain = "www.mouseprints.net" // the domain for the file we want
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 ;
};

 

1 reply

JJMack
Community Expert
JJMackCommunity ExpertCorrect answer
Community Expert
January 12, 2021

If you can connect to the servers html port I had a scripot the could. I do not thinsk it could  do is with https. I think I rememver I was able to change https to http and  do it. I believe I took the socket cone from and adobe sample script. The Script will most like not work  in 2021

 

// 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 domain = "www.mouseprints.net" // the domain for the file we want
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 ;
};

 

JJMack
Known Participant
January 12, 2021

soooo much friend it worked in 2021: D