Site logo

Functions for Photoshop

Get channel

function GetChannel(name) {
	var channel;
	
	try {
		channel = doc.channels.getByName(name);
	}
	catch(err) {
		channel = null;
	}

	return channel;
}

Resize selection

function resizeSelection(horizontal, vertical){
	var strtRulerUnits = app.preferences.rulerUnits;
	var strtTypeUnits = app.preferences.typeUnits;
	app.preferences.rulerUnits = Units.PIXELS;
	app.preferences.typeUnits = TypeUnits.PIXELS;
	var SB = activeDocument.selection.bounds;
	var Height = SB[3].value - SB[1].value;
	var Width = SB[2].value - SB[0].value;
	var onePixH = 100/Height;
	var onePixW = 100/Width;
	var newWidth = onePixW * horizontal;
	var newHeight = onePixH * vertical;
	activeDocument.selection.resize( newWidth , newHeight, AnchorPosition.MIDDLECENTER  );
	app.preferences.rulerUnits = strtRulerUnits;
	app.preferences.typeUnits = strtTypeUnits;
}

Save as JPEG

function saveJPEG( doc, saveFile, qty ) {
     var saveOptions = new JPEGSaveOptions( );
     saveOptions.embedColorProfile = true;
     saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
     saveOptions.matte = MatteType.NONE;
     saveOptions.quality = qty; 
     doc.saveAs( saveFile, saveOptions, true );
}


saveJPEG( app.activeDocument, new File('~/Desktop/sample.jpg'), 10 );

Save as TIF

function SaveAsTIFF( inFileName, inLZW ) {    
    var tiffSaveOptions = new TiffSaveOptions();    
    if ( inLZW ) {    
        tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;    
    } else {    
        tiffSaveOptions.imageCompression = TIFFEncoding.NONE;    
    }     
    app.activeDocument.saveAs( File( inFileName ), tiffSaveOptions );    
};  

Fit image by Xbytor

function fitImage(doc, width, height) {
function cTID(s) { return app.charIDToTypeID(s); };
function sTID(s) { return app.stringIDToTypeID(s); };

app.activeDocument = doc;
var desc = new ActionDescriptor();
desc.putUnitDouble( cTID('Wdth'), cTID('#Pxl'), width );
desc.putUnitDouble( cTID('Hght'), cTID('#Pxl'), height );

var fitId = sTID('3caa3434-cb67-11d1-bc43-0060b0a13dc4');
executeAction(fitId , desc, DialogModes.NO );
};

function main() {
if (app.documents.length == 0) {
alert ("No Open Documents, Script will terminate");
return;
}

var ru = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

var doc = app.activeDocument;

try {
var s = prompt("Largest New Dimension (pixels)",
doc.height.value,
"Dimension");

var lrgdim = Number(s);
if (isNaN(lrgdim) || lrgdim < 0) {
alert("Bad dimension specified: " + s);
}

var s = prompt ("New Resolution (ppi)", 300, "Resolution");
var resolution = Number(s);
if (isNaN(resolution) || resolution < 0) {
alert("Bad dimension specified: " + s);
}

doc.resizeImage(undefined, undefined, 72, ResampleMethod.NONE);
fitImage(doc, lrgdim, lrgdim);
doc.resizeImage(undefined, undefined, resolution, ResampleMethod.NONE);

} finally {
app.preferences.rulerUnits = ru;
}
};

main();

Wait for redraw

A helper function for debugging It also helps the user see what is going on. if you turn it off for this example you get a flashing cursor for a number time

function WaitForRedraw()
{
	var eventWait = charIDToTypeID("Wait")
	var enumRedrawComplete = charIDToTypeID("RdCm")
	var typeState = charIDToTypeID("Stte")
	var keyState = charIDToTypeID("Stte")
	var desc = new ActionDescriptor()
	desc.putEnumerated(keyState, typeState, enumRedrawComplete)
	executeAction(eventWait, desc, DialogModes.NO)
}

Get snapshot

var doc = app.activeDocument;

Main();

function Main() {
	var snapshot = GetSnapshot("Snapshot 1");
}
function GetSnapshot(name) {
	var snapshot;
	
	try {
		snapshot = doc.historyStates.getByName(name);
	}
	catch(err) {
		snapshot = null;
	}

	return snapshot;
}

Make snapshot

function makeSnapshot(name) {
	try {
		var desc1 = new ActionDescriptor();
		var ref1 = new ActionReference();
		ref1.putClass(cTID('SnpS'));
		desc1.putReference(cTID('null'), ref1);
		var ref2 = new ActionReference();
		ref2.putProperty(cTID('HstS'), cTID('CrnH'));
		desc1.putReference(cTID('From'), ref2);
		desc1.putString(cTID('Nm  '), name);
		desc1.putEnumerated(cTID('Usng'), cTID('HstS'), cTID('FllD'));
		executeAction(cTID('Mk  '), desc1);
	}
	catch(err) {
		$.writeln(err.message + ", line: " + err.line);
	}
}