Photoshop Javascript

From bernie's
Jump to navigation Jump to search

This is not updated frequently! I rarely use photoshop and even less script it.

If you want an old overview here's a tutorial I wrote a thousand years ago on scripting in Photoshop:

Launch an action using the name of the current document

Save as .jsx and launch from File>Script in photoshop

/* An example of how you can use the name of a document to launch an action that shares the same name
    try it on an image called 'Gradient Map.jpg' -- since 'Gradient Map is a default action in the 'Default Actions' set it should work on a brand new photoshop

//this gets the name of the current open (foremost document)
currentImage = ;

//this removes the extension
currentImageName = currentImage.substring(0, currentImage.lastIndexOf('.')) || currentImage ;

//this calls an action with the name of the current image from the 'Default Actions' set (which you can of course change)
app.doAction( currentImageName ,'Default Actions');

All documents to current document


docs = app.documents;
ad= app.activeDocument;
for(i = 0;i<docs.length;i++){
        activeDocument = docs[i];
        activeDocument = ad;
    //hackish but works
    for(i = 0;i<docs.length;i++){

Incremental Save

// incrementalsave
// checks if there is a double underscore in filename, if not adds them starting at 0001 
// else increments and saves as 24bit png (filename__####.png) in the doc's folder
// feel free to use & modify -
// only tested on vista+cs3

function remEx(f) {
	extPeriod = f.lastIndexOf(".");
	if(extPeriod > 0){
		return f.substr(0,extPeriod);
		return false;
function getIncrPos(nameWithoutExt){
	pos = nameWithoutExt.lastIndexOf("__");
	return nameWithoutExt.substr(pos+2,nameWithoutExt.length);
function basenameKindOf(path,match) {
	a = path.lastIndexOf(match);
	if(a > 0){;
		return path.substr(a,path.length);
		return false;
function addZeros(number){
		number = "000"+number;	
	}else if(number<100){
		number = "00"+number;
	}else if(number<1000){
		number = "0"+number;
	return number;

docP = app.activeDocument.path;
docN = remEx(;

docFold = new Folder (app.activeDocument.path);
files = docFold.getFiles(docN+"__*.png");
files =	Folder.decode(files);

if(files.length == 0){
	numbering = 0;
	files = files.split(",");
	numbering = 0;
	for(var i in files){
		curNumb = getIncrPos(remEx(basenameKindOf(files[i],docN)));
		numbering = Math.max(numbering,curNumb);
numbering = addZeros(numbering);

var exportOptions = new PNGSaveOptions();
//var type = ExportType.PNG24;
var fileSpec = new File(docP+"/"+docN+"__"+numbering+".png");
exportOptions.antiAliasing = true;
exportOptions.transparency = true;
exportOptions.saveAsHTML = false;