Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PTH = "C/Users/Feast/Desktop/Test"; displayDialogs = DialogModes.NO
- var Path = $.getenv('pth') || PTH || "d/";
- // for saving files
- var doc = app.activeDocument;
- var Name = doc.name.replace(/\.[^\.]+$/, '');
- var Prefix = "";
- var Suffix = "";
- var BlurAmount = [25,50,100];
- var DivideScaleBlurAmount = [1.25,1.5,2];
- var AddBlur = false;
- var AddVeg = false;
- var AddSP = false;
- var AllowNonGroupLayers = false;
- var Res = 512;
- var DocTrimmedWidth = 0;
- var DocTrimmedHeight = 0;
- var BlurredTrimmedWidth = 0;
- var BlurredTrimmedHeight = 0;
- var saveFile = File(Path + "/" + Name + Suffix + ".jpg");
- var dlg = new Window('dialog', 'PNG Batch Save');
- var G1 = dlg.add('group', undefined, '');
- dlg.msgSt = G1.add('statictext', undefined,
- 'Path:');
- dlg.msgSt.alignment = [ScriptUI.Alignment.LEFT,
- ScriptUI.Alignment.TOP]
- dlg.msgEt = G1.add('edittext', [0,0,500,20],
- Path, {name: "path",multiline:false,noecho:false,readonly:false});
- dlg.msgSt = G1.add('statictext', undefined,
- 'Reso:');
- dlg.msgEt = G1.add('edittext', [0,0,40,20],
- Res, {name: "Res"});
- // Resolution UI
- dlg.msgPnl = dlg.add('panel', undefined, '');
- dlg.msgPnl.orientation = "row";
- dlg.titleSt = dlg.msgPnl.add('statictext', undefined,
- 'Prefix:');
- dlg.titleEt = dlg.msgPnl.add('edittext', [0,0,100,20],
- Prefix, {name: "Prefix"});
- // Add a checkbox to control the buttons that dismiss an alert box
- dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
- 'SP', {name: "SP"});
- dlg.hasBtnsCb.value = true;
- dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
- 'B', {name: "Blur"});
- dlg.hasBtnsCb.value = true;
- // Add a checkbox to control the buttons that dismiss an alert box
- dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
- 'VG', {name: "Veg"});
- dlg.hasBtnsCb.value = false;
- dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
- 'Render Ungrouped', {name: "allowLayers"});
- dlg.hasBtnsCb.value = false;
- dlg.msgPnl.btn = dlg.msgPnl.add('button', undefined, 'Export', {name:"export"});
- dlg.msgPnl.btn.onClick = Algorithm;
- dlg.msgPnl.buildBtn = dlg.msgPnl.add('button', undefined, 'Cancel');
- function key_handle(e)
- {
- switch (e.keyIdentifier)
- {
- case "Enter":
- //~ dlg.btnPnl.btn.notify();
- Algorithm();
- break;
- }
- }
- dlg.msgEt.active = true;
- dlg.addEventListener ("keydown", key_handle, false);
- dlg.show();
- // Algorithm must be called when clicking on Export button
- function Algorithm()
- {
- GetUIValues();
- SetSuffix();
- $.setenv('pth', Path.slice(1))
- if (PTH != ge = $.getenv('pth')) {
- (fle = File($.fileName)).open('r'), jsxFile = fle.read()
- .replace(/"(.*)"(?=;)/, '"' + ge + '"'), fle.close()
- fle.open('w'), fle.write(jsxFile), fle.close()
- }
- dlg.close();
- var doc = app.activeDocument;
- // need an array to know which grp was on or off
- var oldLayers = new Array();
- for (var i = 0; i < doc.layerSets.length; i++) {
- if (doc.layerSets[i].visible == true) {
- oldLayers.push(1);
- };
- if (doc.layerSets[i].visible == false) {
- oldLayers.push(0); // not visible
- };
- doc.layerSets[i].visible = false;
- };
- // set all non-groups layers off
- var oldNonGroupLayers = new Array();
- if (AllowNonGroupLayers == false)
- {
- for (var i = 0; i < doc.layers.length; i++) {
- if (doc.layers[i].visible == true) {
- oldNonGroupLayers.push(1);
- };
- if (doc.layers[i].visible == false) {
- oldNonGroupLayers.push(0); // not visible
- };
- doc.layers[i].visible = false;
- }
- }
- for (var i = 0; i < doc.layerSets.length; i++) {
- // if the layer used to be visible
- if (oldLayers[i] == 1) {
- //alert("Found a visible grp!");
- // we set it visible
- doc.layerSets[i].visible = true;
- TrimResizeAndSave(doc.layerSets[i]);
- if (AddBlur)
- {
- for (var j = 1; j <= BlurAmount.length; j++)
- {
- var currentBlur = BlurAmount[j-1];
- CreateBlurredVersion(doc.layerSets[i], currentBlur, j, DivideScaleBlurAmount[j-1]);
- }
- }
- // we set it invisible again
- doc.layerSets[i].visible = false;
- };
- }
- // set whichever groups were originally on visible again
- for (var i = 0; i < doc.layerSets.length; i++) {
- if (oldLayers[i] == 1)
- doc.layerSets[i].visible = true;
- }
- // set whichever non-group layers were originally on visible again
- if (AllowNonGroupLayers == false)
- {
- for (var i = 0; i < doc.layers.length; i++) {
- if (oldNonGroupLayers[i] == 1)
- doc.layers[i].visible = true;
- }
- }
- //~ alert("Finished exporting files");
- }
- function GetUIValues() {
- Res = dlg.findElement("Res").text;
- AddBlur = dlg.findElement("Blur").value;
- AddVeg = dlg.findElement("Veg").value;
- AddSP = dlg.findElement("SP").value;
- Prefix = dlg.findElement("Prefix").text;
- AllowNonGroupLayers = dlg.findElement("allowLayers").value;
- Path = dlg.findElement("path").text;
- var c = /\\/g;
- if (Path.match(c)) {
- //~ alert("found \\");
- Path = Path.replace(c, "/");
- };
- c = ':';
- if (Path.match(c )) {
- //~ alert("found :");
- Path = Path.replace(c , '');
- };
- Path = "/" + Path;
- }
- function SaveJPEG(saveFile, jpegQuality) {
- jpgSaveOptions = new JPEGSaveOptions();
- jpgSaveOptions.embedColorProfile = true;
- jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
- jpgSaveOptions.matte = MatteType.NONE;
- jpgSaveOptions.quality = jpegQuality;
- activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
- }
- function SavePNG(saveFile) {
- pngSaveOptions = new PNGSaveOptions(9, false);
- activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
- }
- function TrimResizeAndSave(group) {
- var doc = app.activeDocument;
- var savedState = doc.activeHistoryState;
- // trim
- doc.trim(TrimType.TRANSPARENT, true, true, true, true);
- // we need to find the current width and height of the picture now
- // this way we can find the right amount of pixels the picture should be blurred
- // to keep the blur even across all the pictures.
- // ========== set units to pixels ==========//
- var oldPrefs = app.preferences.rulerUnits;
- app.preferences.rulerUnits = Units.PIXELS;
- DocTrimmedWidth = doc.width;
- DocTrimmedHeight = doc.height;
- // resize algorithm
- if (DocTrimmedHeight > DocTrimmedWidth) {
- doc.resizeImage(null, UnitValue(Res, "px"),72, ResampleMethod.BICUBIC, 100);
- }
- else {
- doc.resizeImage(UnitValue(Res, "px"), null, 72, ResampleMethod.BICUBIC, 100);
- }
- app.preferences.rulerUnits = oldPrefs;
- // we save the ouput
- if (AddBlur)
- saveFile = File(Path + "/" + "-B0-" + Prefix + group.name + Suffix + ".png");
- else saveFile = File(Path + "/" + Prefix + group.name + Suffix + ".png");
- SavePNG(saveFile);
- doc.activeHistoryState = savedState;
- }
- function CreateBlurredVersion(group, blurStrength, number, scale)
- {
- var doc = app.activeDocument;
- var savedState = doc.activeHistoryState;
- var blurredPath = File(Path + "/" + "-B" + number + "-" + Prefix + group.name + Suffix + ".png");
- var theName = group.name;
- // we need to find the current width and height of the picture now
- // this way we can find the right amount of pixels the picture should be blurred
- // to keep the blur even across all the pictures.
- // ========== set units to pixels ==========//
- var oldPrefs = app.preferences.rulerUnits;
- app.preferences.rulerUnits = Units.PIXELS;
- doc.activeHistoryState = savedState;
- // transform document to smart object
- //~ var result = createSmartObject(group);
- var result = group.merge();
- // gaussian blur
- //result.applyGaussianBlur(blurStrength, blurStrength);
- //~ (aD = activeDocument).activeLayer.applyGaussianBlur(blurStrength * (sqrt = Math.sqrt(aD.width * aD.height / Math.pow(512, 2))) + sqrt / Math.PI / aD.resolution * 72)
- //~ Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {activeLayer.applyGaussianBlur(v1 * sqrt(width * height / pow(v2, 2)))}}}
- //~ Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {activeLayer.applyGaussianBlur(v1 * sqrt(width * height / pow(v2, 2)))}}}
- var v2= DocTrimmedWidth * DocTrimmedHeight;
- Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {return v1 / sqrt(5000* 5000 / v2)}}}
- //~ if (number==3) alert("blurAmount of "+ theName + ": " + Math.blur(blurStrength, v2));
- result.applyGaussianBlur(Math.blur(blurStrength, v2));
- //~ var v1= Math.sqrt (doc.width * doc.height);
- //~ var blurFactor = v1 / v2;
- //~ alert("DocTrimmedWidth: " + DocTrimmedWidth);
- //~ alert("DocTrimmedWidth: " + DocTrimmedHeight);
- //~ alert("doc.width: " + doc.width);
- //~ alert("doc.height: " + doc.height);
- //~ alert("blurFactor: " + blurFactor);
- //~ result.applyGaussianBlur(blurStrength * blurFactor);
- //~ var blurCalc = blurStrength * Math.sqrt(DocTrimmedWidth * DocTrimmedHeight / Math.pow((DocTrimmedWidth + DocTrimmedHeight)/2, 2));
- //~ result.applyGaussianBlur(blurCalc);
- //~ Math.blur(blurStrength, (doc.width + doc.height)/2);
- doc.trim(TrimType.TRANSPARENT, true, true, true, true);
- BlurredTrimmedWidth = doc.width;
- BlurredTrimmedHeight = doc.height;
- // resize algorithm
- if (BlurredTrimmedHeight > BlurredTrimmedWidth) {
- doc.resizeImage(null, UnitValue(Res/ scale, "px"), 72, ResampleMethod.BICUBIC, 100);
- }
- else {
- doc.resizeImage(UnitValue(Res/ scale, "px"), null, 72, ResampleMethod.BICUBIC, 100);
- }
- app.preferences.rulerUnits = oldPrefs;
- // save
- SavePNG(blurredPath);
- doc.activeHistoryState = savedState;
- }
- // create smartobject from specified layer or layerSet/group
- function createSmartObject(layer)
- {
- var doc = app.activeDocument;
- var layer = layer != undefined ? layer : doc.activeLayer;
- if(doc.activeLayer != layer) doc.activeLayer = layer;
- try
- {
- var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
- executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
- return doc.activeLayer;
- }
- catch(e)
- {
- return undefined;
- }
- }// JavaScript Document
- function SetSuffix()
- {
- if (AddSP && AddVeg)
- Suffix = "-VG-SP";
- else if (AddSP)
- Suffix = "-SP";
- else if (AddVeg)
- Suffix = "-VG";
- }
Advertisement
Add Comment
Please, Sign In to add comment