Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PTH = "";
- PREFIX = "";
- displayDialogs = DialogModes.NO;
- if (vbs = File((fN = $.fileName).slice(0, -3) + 'vbs'),
- !(eval(p = 'preferences.autoUpdateOpenDocuments'))) {
- activeDocument.save()
- var Path = $.getenv('pth') || PTH || "";
- var Prefix = $.getenv('pre') || PREFIX || "";
- // for saving files
- var doc = app.activeDocument;
- var Name = doc.name.replace(/\.[^\.]+$/, '');
- var Suffix = "";
- var BlurAmount = [2,4,7];
- 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.path = G1.add('edittext', [0,0,400,20],
- Path, {name: "path",multiline:false,noecho:false,readonly:false});
- var myFile = myFile
- btn_localFile = G1.add('button',{x:115, y:20, width:60,height:25},'Browse...');
- btn_localFile.onClick = function(){
- myFile = File.openDialog ('Select a folder...');
- if(myFile !=null){dlg.path.text = myFile.fsName};
- };
- 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();
- var myFolder = new Folder(Path);
- if (!myFolder.exists){
- preferences.autoUpdateOpenDocuments = false;
- alert(Path + " does not exist!");
- return;
- }
- 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()
- }
- $.setenv('pre', Prefix)
- if (PREFIX != ge = $.getenv('pre')) {
- (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) {
- // 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;
- }
- }
- (fle = activeDocument.fullName).encoding = 'binary', fle.open('r')
- var r = fle.read(); fle.open('w'), fle.write(r), fle.close(), eval(p + ' = 1')
- documents.add(1,1,1).close(), t = 'WScript.Sleep 250\rCreateObject'
- t += '("Shell.Application").ShellExecute """' + app.path.fsName
- t += '\\Photoshop.exe""", "' + File(fN).fsName + '"'
- vbs.open('w'), vbs.write(t), vbs.close(), vbs.execute()
- }
- 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)) {
- Path = Path.replace(c, "/");
- };
- c = ':';
- if (Path.match(c )) {
- Path = Path.replace(c , '');
- };
- c = /\"/g;
- if (Path.match(c )) {
- Path = Path.replace(c , '');
- };
- c = /\'/g;
- if (Path.match(c )) {
- 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 group = group != undefined ? group : doc.activeLayer;
- if(doc.activeLayer != group) doc.activeLayer = group;
- var result = group.merge();
- // gaussian blur
- //~ var v2= DocTrimmedWidth * DocTrimmedHeight;
- //~ Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {return v1 / sqrt(512* 512 / v2)}}}
- //~ result.applyGaussianBlur(Math.blur(blurStrength, v2));
- Math.blur = function(v1, v2) {
- with(this) {
- (b = result.bounds), result.applyGaussianBlur
- (v1 * sqrt((b[2] - b[0]) * (b[3] - b[1]) / pow(v2, 2)))
- }
- }
- Math.blur(blurStrength, 512)
- 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;
- }
- 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;
- }
- }
- function SetSuffix()
- {
- if (AddSP && AddVeg)
- Suffix = "-VG-SP";
- else if (AddSP)
- Suffix = "-SP";
- else if (AddVeg)
- Suffix = "-VG";
- }
- }
- else eval(p + ' = ' + !p), vbs.remove(), alert("Finished exporting files")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement