Making HTTP calls & interacting with Disk from within Photoshop

This blog post has sat in Draft for far too long. I have no intention of writing a full article so let’s let the code live on.

Using the Adobe Photoshop CS ExtendScript Toolkit (which is included by default in the PS scripting environment), we can make HTTP calls, write to disk, and have some fun. Imagine exposing a REST API of batch jobs to execute and having Photoshop pull them down and execute them in near real-time. I did this and it works.

Here is a simple gist of opening a connection and writing the response to a file.

function execute_modifications() {
	var doc = activeDocument;
	var reply = "";
	var conn = new Socket;
 
	// access some URL
	if (conn.open ("www.google.com:80")) {
 
		// issue http command
		conn.write ("GET /index.html HTTP/1.0\n\n");
 
		// read connection response (and a lot of it)
		reply = conn.read(999999);
 
		var file;
 
		try {
			file = new File('~/.ps_actions.cnf');
			if (!file.open('e')) {
				alert('failed to open file: ' + file.error);
			}
 
			if (!file.write(reply)) {
				alert("Failed to write to file: " + file.error);
			}
		} catch(e) {
			alert('Failed to write to file: ' + e + ":" + e.line);
		} finally {
			file.close();
		}
 
		conn.close();
	}
}
 
 
execute_modifications();

More documentation at:
Javascript Tools Guide (Page 17 is where it gets interesting)
Photoshop Scripting

What I’m Reading

Hey All! I’ve been using Pocket to save articles from the Internet to read later. It’s been great. It’s upping the amount of content I consume and making article management a lot easier (no more bookmarks!). I created a What I’m Reading┬ápage on my blog so you can see what I’ve been consuming. I’ll update it from time to time.