Difference between revisions of "Photoshop Javascript"

From bernie's
Jump to navigation Jump to search
m
(No difference)

Revision as of 21:10, 18 December 2020

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: https://www.kirupa.com/motiongraphics/ps_scripting.htm

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 = app.activeDocument.name ;

//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

9mtpo.png

docs = app.documents;
ad= app.activeDocument;
for(i = 0;i<docs.length;i++){
    if(docs[i]!=ad){
        activeDocument = docs[i];
        docs[i].selectAll;
        docs[i].layers[0].copy(); 
        activeDocument = ad;
        ad.paste();
     }
}
while(docs.length>1){
    //hackish but works
    for(i = 0;i<docs.length;i++){
        if(docs[i]!=ad){
            docs[i].close(SaveOptions.DONOTSAVECHANGES); 
        }
    }
}

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 - mbernadat@gmail.com
//
//
// only tested on vista+cs3

function remEx(f) {
	extPeriod = f.lastIndexOf(".");
	if(extPeriod > 0){
		return f.substr(0,extPeriod);
	}else{
		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);
	}else{
		return false;
	}
}
function addZeros(number){
	if(number<10){
		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(app.activeDocument.name);

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

if(files.length == 0){
	numbering = 0;
}else{
	files = files.split(",");
	numbering = 0;
	for(var i in files){
		curNumb = getIncrPos(remEx(basenameKindOf(files[i],docN)));
		numbering = Math.max(numbering,curNumb);
		//alert("'"+remEx(files[i])+"'");
	}
}
numbering++;
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;
app.activeDocument.saveAs(fileSpec,exportOptions,1,Extension.LOWERCASE);