#target photoshop
selectCSV();
function selectCSV() {
if ( /Macintosh/i.test( $.os ) ) {
var csvFile = File.openDialog( 'Select a CSV File', function (f) { return ( f instanceof Folder ) || f.name.match( /\.csv$/i );} );
} else {
var csvFile = File.openDialog( 'Select a CSV File', 'File (*.csv):*.csv;' );
};
if ( csvFile != null ) { var fileArray = readInCSV( csvFile ) };
var output = Folder( Folder.desktop + '/Output' );
if ( ! output.exists ) { output.create(); }
createOutput( fileArray, output )
};
function createOutput( fileArray, saveFolder ) {
var userUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var doc = app.activeDocument;
var opts = new TiffSaveOptions();
opts.embedColorProfile = true;
opts.imageCompression = TIFFEncoding.TIFFLZW;
opts.byteOrder = ByteOrder.IBM; //MACOS
opts.alphaChannels = false;
opts.layers = false;
for ( var i = 0; i < fileArray.length; i++ ) {
var x1 = fileArray[i][0];
var y1 = fileArray[i][1];
var x2 = fileArray[i][2];
var y2 = fileArray[i][3];
var name = fileArray[i][4];
doc.selection.select([ [x1,y1], [x2,y1], [x2,y2], [x1,y2] ]);
var idCrop = charIDToTypeID( "Crop" );
var desc36 = new ActionDescriptor();
var idDlt = charIDToTypeID( "Dlt " );
desc36.putBoolean( idDlt, true );
executeAction( idCrop, desc36, DialogModes.NO );
var saveFile = File( saveFolder.fsName + '/' + name + ".tif" );
doc.saveAs( saveFile, opts, false, Extension.LOWERCASE );
doc.activeHistoryState = doc.historyStates[0];
};
// doc.close( SaveOptions.DONOTSAVECHANGES );
app.preferences.rulerUnits = userUnits;
};
function readInCSV( fileObj ) {
var fileArray = new Array();
fileObj.open( 'r' );
//fileObj.seek( 0, 0 );
while( ! fileObj.eof ) {
var thisLine = fileObj.readln();
var csvArray = thisLine.split( ',' );
fileArray.push( csvArray );
};
fileObj.close();
return fileArray;
};