Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Scripting - From JSON Url to an Image in Photoshop

Community Beginner ,
Dec 03, 2020 Dec 03, 2020

Hello PS community, 

 

I have a bunch of logo URL's stored in a JSON dataset and I would like to run a script that goes through these URLs, convert these URLs to corresponding logo images and place the logos on my Photoshop canvas. How can I accomplish this? 

 

Thank you all in advance,

TOPICS
Actions and scripting
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 04, 2020 Dec 04, 2020

Use a script to place in the logo size it and position it for the image's size. Here is] how I do it. It easy to change this script for your use.

 

 

/* ==========================================================
// 2017  John J. McAssey (JJMack) 
// ======================================================= */
// This script is supplied as is. It is provided as freeware. 
// The author accepts no liability for any problems arising from its use.
/*
<javascriptresource>
<about>$$$/JavaScripts/PlaceWa
...
Translate
Adobe
Community Expert ,
Dec 03, 2020 Dec 03, 2020

If the URLs are for Logo files a script be able to connect to the HTML port and down the logo files so you can place them into your document.  A URL basically is a Web address not a logo.

 

I have an old script that use to work to open web images into Photoshop I have not use it in years. You may want to look at the code.

// 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("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("~/" + Name); // Image file name
			f.encoding = "binary"; // set binary mode
			f.open("w");

			if (socket.open(domain + port, "binary")){
				//alert("GET " + sImg +" HTTP/1.0\n\n");
				requesthtml ="\n\nDmain:" + domain + " Port" + port + " binary\n"
				//request ="GET " + sImg +" HTTP/1.0\n\n"
				request ="GET " + sImg +" HTTP/1.0\nHost: " + domain + "\nAccept: image/gif, image/x-xbitmap, image/png, image/jpeg, */*\n\n";
 				socket.write(request); // get the file
				var binary = socket.read(99999999);
				binary = removeHeaders(binary);
				f.write(binary);
				socket.close();
				} 
			else { alert("Connection to Domain:" + domain + " Port" + port + " Failed   ");}

			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(1600,'px'), new UnitValue(1200,'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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 04, 2020 Dec 04, 2020

Thank you for your answer! I figured it would be easier for me to use Python to save the images. Now I am trying to figure out how I can place those images in a Frame by using PS scripting.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 04, 2020 Dec 04, 2020

Use a script to place in the logo size it and position it for the image's size. Here is] how I do it. It easy to change this script for your use.

 

 

/* ==========================================================
// 2017  John J. McAssey (JJMack) 
// ======================================================= */
// This script is supplied as is. It is provided as freeware. 
// The author accepts no liability for any problems arising from its use.
/*
<javascriptresource>
<about>$$$/JavaScripts/PlaceWatermark/About=JJMack's PlaceWatermark ^r^rCopyright 2017 Mouseprints.net^r^rPhotoshop Script^rCustomize using first four var</about>
<category>JJMack's Script</category>
<enableinfo>true</enableinfo>
</javascriptresource>
*/
#target photoshop;  
app.bringToFront();  
var logoFile = "~/Desktop/JJMack.png";						// Watermark file should be large for resize down works better than up
var LogoSize = 10;											// percent of document height to resize Watermark to
var LogoMargin = 1;                                         // percent of Document height the Watermark should have as a margin
var BottomLetf = false;										// false = Bottom Right true Bottom Left 
//placeWatermark(logoFile, LogoSize, LogoMargin);           // Place Watermark into the bottom of the document
if (documents.length) app.activeDocument.suspendHistory('placeWatermark','placeWatermark(logoFile,LogoSize,LogoMargin)' );
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function placeWatermark(Image,Size,Margin){  
	if(!documents.length) return;  							// if no document return
		var fileObj = new File(Image);	                	// the passed file
	if(!fileObj.exists){  									// If file does not exits tell user 
		alert(fileObj.name  + " does not exist!"); 			// Alert User 
		return;  											// return
		}  
	try{  
		var doc = app.activeDocument;						// set Doc object to active document
		app.displayDialogs = DialogModes.NO;				// Dialog off 
		var strtRulerUnits = app.preferences.rulerUnits;	// Save Users ruler units 
		var strtTypeUnits = app.preferences.typeUnits;		// Save Users Type units 
		app.preferences.rulerUnits = Units.PIXELS;			// work with pixels 
		app.preferences.typeUnits = TypeUnits.PIXELS;		// work with pixels 
		var layers = app.activeDocument.layers;				// get layers
		app.activeDocument.activeLayer = layers[0];			// Target Top Layer
		placeFile(fileObj); 								// Place in file the Watermark png file
		activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER); // Insure Place did not scale layer  
		var SB = activeDocument.activeLayer.bounds; 		// get layers bounds 
		var layerHeight = SB[3] - SB[1];					// get layers height  
		var resizePercent = (100/layerHeight)*(Size/100*doc.height.value); // Percent to resize by 
		activeDocument.activeLayer.resize(resizePercent ,resizePercent,AnchorPosition.MIDDLECENTER);  // Resize width and height by percentage 
		SB = activeDocument.activeLayer.bounds;				// get resized layers bounds  
		activeDocument.activeLayer.translate(-SB[0].value,-SB[1].value); // Move resized layer to top left canvas corner 
		var LayerWidth = (SB[2].value - SB[0].value);		// get resized layers width  
		var LayerHeight = (SB[3].value - SB[1].value);  	// get resized layers height
		marginSize = Margin/100*doc.height.value;			// Margin size
		// move resized watermark into the document lower right corner with some margin or lower left
		if  ( BottomLetf) {activeDocument.activeLayer.translate(marginSize,( doc.height.value -marginSize - LayerHeight));}
		else {activeDocument.activeLayer.translate((doc.width.value -marginSize - LayerWidth),( doc.height.value -marginSize - LayerHeight));}
	}
	catch(e) { alert(e + ': on line ' + e.line); }			// inform user of error  
	finally{  
		app.preferences.rulerUnits = strtRulerUnits;		// Restore user ruler units  
		app.preferences.typeUnits = strtTypeUnits;			// Restore user type units    
	};  
}; 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
function placeFile(placeFile) {  
    var desc21 = new ActionDescriptor();  
    desc21.putPath( charIDToTypeID('null'), new File(placeFile) );  
    desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );  
    var desc22 = new ActionDescriptor();  
    desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );  
    desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );  
    desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 );  
    executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO );  
};  

 

 

 

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 04, 2020 Dec 04, 2020
LATEST

Worked like a charm! Thanks a lot @JJMack !!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 03, 2020 Dec 03, 2020

Hi Kaan5CAB,

this one could help perhaps:

https://stackoverflow.com/questions/39001280/how-to-download-an-image-from-url-to-use-in-photoshop-s...

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 04, 2020 Dec 04, 2020

Hi Uwe, I actually had tried this prior but didn't work for me. I ended up using Python to save the images. Now i'm trying to place these images into Frames on PS using scripting.

Best,

Kaan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines