Help polish my script?

Upload Photoshop Scripts, download Photoshop Scripts, Discussion and Support of Photoshop Scripts

Moderators: Tom, Kukurykus

ehime

Help polish my script?

Post by ehime »

Script is finished, but I'd like to add a few features to it if possible and
not terribly sure how I'd go about it. This script opens a select image,
throws a filter on it, resizes and saves multiple times to different dash
names. It already remembers last dir for output.

What I'd like to do is have a few radio buttons in it, one to run through
an entire directory if checked. One to enable/disable my filter, and maybe
on that will put the output into the directories parent folder. This would
save a lot of time I figure with folders I need to flesh out 40 some odd
files like I'm doing now. Here's my scripts source for anyone who wants
to take a peek.

Code: Select all#target photoshop
function main(){
var dlg=
"dialog{text:'Script Interface',bounds:[100,100,650,250],"+
"panel0:Panel{bounds:[10,10,540,140] , text:'' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},"+
"statictext0:StaticText{bounds:[10,10,111,27] , text:'Select JPEG file' ,properties:{scrolling:undefined,multiline:undefined}},"+
"jpeg:EditText{bounds:[150,10,430,30] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"Browse0:Button{bounds:[440,10,521,31] , text:'Browse' },"+
"statictext1:StaticText{bounds:[10,40,140,60] , text:'Select Output Folder' ,properties:{scrolling:undefined,multiline:undefined}},"+
"folder:EditText{bounds:[150,40,430,60] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"Browse1:Button{bounds:[440,40,520,61] , text:'Browse' },"+
"statictext2:StaticText{bounds:[10,70,130,90] , text:'New FileName No Ext.' ,properties:{scrolling:undefined,multiline:undefined}},"+
"name:EditText{bounds:[150,70,430,90] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"button2:Button{bounds:[10,100,260,121] , text:'Ok' },"+
"button3:Button{bounds:[270,100,520,121] , text:'Cancel' }}};";

var win = new Window(dlg,"Process for Server");
win.center();
win.panel0.jpeg.enabled = false;
win.panel0.folder.enabled = false;

win.panel0.Browse0.onClick = function() {
selectedFile = File.openDialog("Please select JPEG.","JPG File:*.jpg");   
   if(selectedFile !=null){
      win.panel0.jpeg.text =  decodeURI(selectedFile.fsName);
      }
}

win.panel0.Browse1.onClick = function() {
   outputFolder = Folder.selectDialog("Please select OUTPUT folder.",existFolder);   
   if(outputFolder !=null){
      win.panel0.folder.text =  decodeURI(outputFolder.fsName);
      }
}

var done = false;
    while (!done) {
      var x = win.show();
      if (x == 0 || x == 2) {
        win.canceled = true;
        //Cancelled
        done = true;
      } else if (x == 1) {
        done = true;
       var result = validate();
        if(result != true) {
           alert(result);
           return;
        }else
        {
         processPic(); 
        }
      }
   }
function validate(){
if(outputFolder != ''){
Ini.open("w");
Ini.writeln(outputFolder);
Ini.close();
}
if(win.panel0.jpeg.text == '') return 'No JPEG file selected';
if(win.panel0.folder.text == '') return 'No Output Folder selected';
if(win.panel0.name.text == '') return 'No FileName Supplied';
   return true;
}
function processPic(){
var strtRulerUnits = app.preferences.rulerUnits;            
var strtTypeUnits = app.preferences.typeUnits;            
app.preferences.rulerUnits = Units.PIXELS;            
app.preferences.typeUnits = TypeUnits.PIXELS; 
var file = new File(selectedFile);
   open(file);
resize(540);
smartSharpen();
activeDocument.activeLayer.duplicate ();
activeDocument.activeLayer = app.activeDocument.artLayers[0];
app.activeDocument.activeLayer.opacity=60;
app.activeDocument.activeLayer.blendMode = BlendMode.MULTIPLY;
executeAction(app.charIDToTypeID('MrgV'), undefined, DialogModes.NO );
var fileName = outputFolder +"/"+win.panel0.name.text;
var saveFile = new File(fileName+".jpg");
SaveJPEG(saveFile, 12);
resize(170);
var saveFile = new File(fileName+"-m.jpg");
SaveJPEG(saveFile, 12);
resize(50);
var saveFile = new File(fileName+"-sm.jpg");
SaveJPEG(saveFile, 12);
resize(20);
var saveFile = new File(fileName+"-ico.jpg");
SaveJPEG(saveFile, 12);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.preferences.rulerUnits = strtRulerUnits; 
app.preferences.typeUnits = strtTypeUnits;
}
}
var Ini = new File("~/rememberFolder.ini");
if(!Ini.exists){
Ini.open("w");
Ini.writeln("");
Ini.writeln("");
Ini.close();
var existFolder = '';
}else{
Ini.open("r");
var existFolder = Ini.readln();
Ini.close();   
   }
var selectedFolder = '';
main();

function smartSharpen() {
  function cTID(s) { return app.charIDToTypeID(s); };
  function sTID(s) { return app.stringIDToTypeID(s); };

    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble( cTID('Amnt'), cTID('#Prc'), 120.000000 );
    desc1.putUnitDouble( cTID('Rds '), cTID('#Pxl'), 0.700000 );
    desc1.putInteger( cTID('Thsh'), 0 );
    desc1.putInteger( cTID('Angl'), 0 );
    desc1.putBoolean( sTID('moreAccurate'), true );
    desc1.putEnumerated( cTID('blur'), sTID('blurType'), sTID('lensBlur') );
    desc1.putString( sTID('preset'), "Default" );
        var desc2 = new ActionDescriptor();
        desc2.putUnitDouble( cTID('Amnt'), cTID('#Prc'), 0.000000 );
        desc2.putUnitDouble( cTID('Wdth'), cTID('#Prc'), 50.000000 );
        desc2.putInteger( cTID('Rds '), 1 );
    desc1.putObject( cTID('sdwM'), sTID('adaptCorrectTones'), desc2 );
        var desc3 = new ActionDescriptor();
        desc3.putUnitDouble( cTID('Amnt'), cTID('#Prc'), 0.000000 );
        desc3.putUnitDouble( cTID('Wdth'), cTID('#Prc'), 50.000000 );
        desc3.putInteger( cTID('Rds '), 1 );
    desc1.putObject( cTID('hglM'), sTID('adaptCorrectTones'), desc3 );
    executeAction( sTID('smartSharpen'), desc1, DialogModes.NO );
};

function resize(width) {
  function cTID(s) { return app.charIDToTypeID(s); };
  function sTID(s) { return app.stringIDToTypeID(s); };

    var desc19 = new ActionDescriptor();
    desc19.putUnitDouble( cTID('Wdth'), cTID('#Pxl'), width );
    desc19.putBoolean( sTID('scaleStyles'), true );
    desc19.putBoolean( cTID('CnsP'), true );
    desc19.putEnumerated( cTID('Intr'), cTID('Intp'), cTID('Bcbc') );
    executeAction( cTID('ImgS'), desc19, DialogModes.NO );
};

function SaveJPEG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality; //1-12
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}