Posted on

How to write scripts for Photoshop in JavaScript?

little-planet-code-01

Location of Photoshop scripts

If you want to have your Photoshop scripts directly in the menu File > Scripts you should place your script in the following Windows folder:
C:Program Files > Adobe > Adobe Photoshop CC 2014 > Presets > Scripts

Create a Photoshop script

Here a nice little script to create a little planet:

// Copyright 2015.  Roland Russwurm. Use for whatever you want and enjoy.
// This script will create little planet photos
// Written by Roland Russwurm
// Version: 1.0

<javascriptresource>
<name>Little Planet Creator</name>
</javascriptresource>

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

function main() {
var duplicateLayer;
var actLayer;
var layers = [];
var dimens;
var diffx, diffy;
var selRegion;
var execute = true;

var win = new Window("dialog{text:'Little Planet Creator',bounds:[0,0,500,300],\
text1:StaticText{bounds:[20,20,500,40], text: 'What is it all about?'}, \
text2:StaticText{bounds:[20,40,500,40], text: 'An amazing script to convert your photos to'}, \
text3:StaticText{bounds:[20,60,500,40], text: 'small little planets.'}, \
button1:Button{bounds:[20,200,220,230] , text:'Cancel', name: 'cancel' },\
button2:Button{bounds:[300,200,450,230] , text:'OK' },\
checkbox1:Checkbox{bounds:[20,133,261,154] , text:'Transparent surrounding' },\
dropdown1:DropDownList{bounds:[299,134,443,159],properties:{items:['Little Planet', 'Heaven View']}}\
};");

win.button1.onClick = function() {
	execute = false;
	return;
}

win.dropdown1.items[0].selected = true;

win.center();
win.show();


if (execute == true) {
	// Get current document
	try {
	var doc = app.activeDocument;
	} catch (e) {
		alert('No photo found to work with!');
		return;
	}

	// Get the dimensions of the image
	var w = doc.width;
	var h = doc.height;

	// Create a new document to merge all the samples into
	var newDoc = app.documents.add(w, h, 300, "Little Planet", NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);

	for (var i = 0, len = doc.artLayers.length; i < len; i += 1) {
	  layers[i] = doc.artLayers[i];
	}

	var j = 0;
	for (var i = layers.length-1; i >= 0; i -= 1, j += 1) {
		app.activeDocument = doc;
		
		duplicateLayer = layers[i].duplicate();
		duplicateLayer.rasterize(RasterizeType.ENTIRELAYER);
	  
		// Dimension from [left,top,right,bottom]
		dimens = duplicateLayer.bounds;
		
		diffx = (w - (dimens[2]-dimens[0]))/2;
		diffy = (h - (dimens[3]-dimens[1]))/2;
			
		duplicateLayer.cut();
		
		duplicateLayer.remove();
		app.activeDocument = newDoc;
		
		// left top right bottom
		selRegion = [ [dimens[0],dimens[1]], [dimens[2],dimens[1]], [dimens[2],dimens[3]], [dimens[0],dimens[3]] ];
		newDoc.selection.select(selRegion);

		newDoc.paste();
		actLayer = newDoc.activeLayer;
	}

		app.activeDocument = newDoc;
		if (newDoc.artLayers.length > 1) {
			newDoc.mergeVisibleLayers();
		}
			
		// Make the image a square
		newDoc.resizeImage(w,w, "300", ResampleMethod.BICUBIC);

		// Delete the top pixel
		if (win.checkbox1.value == true) {
			if (win.dropdown1.selection.index == 0) {
				selRegion = [ [0,0], [w,0], [w,1], [0,1] ];
			} else {
				selRegion = [ [0,h-1], [w,h-1], [w,h], [0,h] ];
			}
			newDoc.selection.select(selRegion);
			newDoc.selection.clear();
			newDoc.selection.deselect();
		}
		
		if (win.dropdown1.selection.index == 0) {
			// Rotate the image by 180 degrees
			newDoc.rotateCanvas(180);
		}
		
		// Polar transformation of the active layer
		actLayer = newDoc.activeLayer;
		actLayer.applyPolarCoordinates(PolarConversionType.RECTANGULARTOPOLAR);
	}
}

main();

Links

Script UI Classes
Adobe Photoshop CS5 Object Library
JavaScript Scripting Reference (CS2)
JavaScript Tools Guide
Photoshop CS6 Scripting Guide
Photoshop CC 2014 Javascript Scripting Reference
Adobe Photoshop Scripting Overview