Advertisement
FeastXX

Photoshop:PngGroupSave

Jan 20th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.28 KB | None | 0 0
  1. PTH = "";
  2. PREFIX = "";
  3.  
  4. displayDialogs = DialogModes.NO;
  5.  
  6. if (vbs = File((fN = $.fileName).slice(0, -3) + 'vbs'),
  7. !(eval(p = 'preferences.autoUpdateOpenDocuments'))) {
  8. activeDocument.save()
  9.  
  10.  
  11. var Path = $.getenv('pth') || PTH || "";
  12. var Prefix = $.getenv('pre') || PREFIX || "";
  13.  
  14. // for saving files
  15. var doc = app.activeDocument;
  16.  
  17. var Name = doc.name.replace(/\.[^\.]+$/, '');
  18.  
  19. var Suffix = "";
  20.  
  21. var BlurAmount = [2,4,7];
  22. var DivideScaleBlurAmount = [1.25,1.5,2];
  23.  
  24. var AddBlur = false;
  25. var AddVeg = false;
  26. var AddSP = false;
  27. var AllowNonGroupLayers = false;
  28.  
  29. var Res = 512;
  30.  
  31. var DocTrimmedWidth = 0;
  32. var DocTrimmedHeight = 0;
  33.  
  34. var BlurredTrimmedWidth = 0;
  35. var BlurredTrimmedHeight = 0;
  36.  
  37. var saveFile = File(Path + "/" + Name + Suffix + ".jpg");
  38.  
  39. var dlg = new Window('dialog', 'PNG Batch Save');
  40.  
  41. var G1 = dlg.add('group', undefined, '');
  42.  
  43. //~ dlg.msgSt = G1.add('statictext', undefined,
  44. //~ 'Path:');
  45.  
  46. dlg.path = G1.add('edittext', [0,0,400,20],
  47. Path, {name: "path",multiline:false,noecho:false,readonly:false});
  48.  
  49. var myFile = myFile
  50. btn_localFile = G1.add('button',{x:115, y:20, width:60,height:25},'Browse...');
  51. btn_localFile.onClick = function(){
  52. myFile = File.openDialog ('Select a folder...');
  53. if(myFile !=null){dlg.path.text = myFile.fsName};
  54. };
  55.  
  56.  
  57. dlg.msgSt = G1.add('statictext', undefined,
  58. 'Reso:');
  59. dlg.msgEt = G1.add('edittext', [0,0,40,20],
  60. Res, {name: "Res"});
  61.  
  62.  
  63. // Resolution UI
  64. dlg.msgPnl = dlg.add('panel', undefined, '');
  65. dlg.msgPnl.orientation = "row";
  66. dlg.titleSt = dlg.msgPnl.add('statictext', undefined,
  67. 'Prefix:');
  68. dlg.titleEt = dlg.msgPnl.add('edittext', [0,0,100,20],
  69. Prefix, {name: "Prefix"});
  70.  
  71. // Add a checkbox to control the buttons that dismiss an alert box
  72. dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
  73. 'SP', {name: "SP"});
  74. dlg.hasBtnsCb.value = true;
  75. dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
  76. 'B', {name: "Blur"});
  77. dlg.hasBtnsCb.value = true;
  78. // Add a checkbox to control the buttons that dismiss an alert box
  79. dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
  80. 'VG', {name: "Veg"});
  81. dlg.hasBtnsCb.value = false;
  82. dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
  83. 'Render Ungrouped', {name: "allowLayers"});
  84. dlg.hasBtnsCb.value = false;
  85.  
  86. dlg.msgPnl.btn = dlg.msgPnl.add('button', undefined, 'Export', {name:"export"});
  87. dlg.msgPnl.btn.onClick = Algorithm;
  88.  
  89. dlg.msgPnl.buildBtn = dlg.msgPnl.add('button', undefined, 'Cancel');
  90.  
  91.  
  92.  
  93.  
  94.  
  95. function key_handle(e)
  96. {
  97. switch (e.keyIdentifier)
  98. {
  99. case "Enter":
  100. //~ dlg.btnPnl.btn.notify();
  101. Algorithm();
  102. break;
  103. }
  104. }
  105.  
  106. dlg.msgEt.active = true;
  107.  
  108. dlg.addEventListener ("keydown", key_handle, false);
  109.  
  110. dlg.show();
  111.  
  112.  
  113.  
  114. // Algorithm must be called when clicking on Export button
  115. function Algorithm()
  116. {
  117. GetUIValues();
  118.  
  119. var myFolder = new Folder(Path);
  120.  
  121. if (!myFolder.exists){
  122. preferences.autoUpdateOpenDocuments = false;
  123. alert(Path + " does not exist!");
  124. return;
  125. }
  126.  
  127. SetSuffix();
  128.  
  129. $.setenv('pth', Path.slice(1))
  130. if (PTH != ge = $.getenv('pth')) {
  131. (fle = File($.fileName)).open('r'), jsxFile = fle.read()
  132. .replace(/"(.*)"(?=;)/, '"' + ge + '"'), fle.close()
  133. fle.open('w'), fle.write(jsxFile), fle.close()
  134. }
  135.  
  136. $.setenv('pre', Prefix)
  137. if (PREFIX != ge = $.getenv('pre')) {
  138. (fle = File($.fileName)).open('r'), jsxFile = fle.read()
  139. .replace(/"(.*)"(?=;)/, '"' + ge + '"'), fle.close()
  140. fle.open('w'), fle.write(jsxFile), fle.close()
  141. }
  142.  
  143. dlg.close();
  144.  
  145. var doc = app.activeDocument;
  146. // need an array to know which grp was on or off
  147. var oldLayers = new Array();
  148. for (var i = 0; i < doc.layerSets.length; i++) {
  149. if (doc.layerSets[i].visible == true) {
  150. oldLayers.push(1);
  151. };
  152.  
  153. if (doc.layerSets[i].visible == false) {
  154. oldLayers.push(0); // not visible
  155. };
  156. doc.layerSets[i].visible = false;
  157. };
  158.  
  159. // set all non-groups layers off
  160. var oldNonGroupLayers = new Array();
  161. if (AllowNonGroupLayers == false)
  162. {
  163. for (var i = 0; i < doc.layers.length; i++) {
  164. if (doc.layers[i].visible == true) {
  165. oldNonGroupLayers.push(1);
  166. };
  167.  
  168. if (doc.layers[i].visible == false) {
  169. oldNonGroupLayers.push(0); // not visible
  170. };
  171. doc.layers[i].visible = false;
  172. }
  173. }
  174.  
  175. for (var i = 0; i < doc.layerSets.length; i++) {
  176. // if the layer used to be visible
  177. if (oldLayers[i] == 1) {
  178. // we set it visible
  179. doc.layerSets[i].visible = true;
  180.  
  181. TrimResizeAndSave(doc.layerSets[i]);
  182.  
  183. if (AddBlur)
  184. {
  185. for (var j = 1; j <= BlurAmount.length; j++)
  186. {
  187. var currentBlur = BlurAmount[j-1];
  188.  
  189. CreateBlurredVersion(doc.layerSets[i], currentBlur, j, DivideScaleBlurAmount[j-1]);
  190. }
  191. }
  192.  
  193.  
  194. // we set it invisible again
  195. doc.layerSets[i].visible = false;
  196.  
  197. };
  198. }
  199.  
  200. // set whichever groups were originally on visible again
  201. for (var i = 0; i < doc.layerSets.length; i++) {
  202. if (oldLayers[i] == 1)
  203. doc.layerSets[i].visible = true;
  204. }
  205.  
  206. // set whichever non-group layers were originally on visible again
  207. if (AllowNonGroupLayers == false)
  208. {
  209. for (var i = 0; i < doc.layers.length; i++) {
  210. if (oldNonGroupLayers[i] == 1)
  211. doc.layers[i].visible = true;
  212. }
  213. }
  214.  
  215.  
  216. (fle = activeDocument.fullName).encoding = 'binary', fle.open('r')
  217. var r = fle.read(); fle.open('w'), fle.write(r), fle.close(), eval(p + ' = 1')
  218. documents.add(1,1,1).close(), t = 'WScript.Sleep 250\rCreateObject'
  219. t += '("Shell.Application").ShellExecute """' + app.path.fsName
  220. t += '\\Photoshop.exe""", "' + File(fN).fsName + '"'
  221. vbs.open('w'), vbs.write(t), vbs.close(), vbs.execute()
  222. }
  223.  
  224.  
  225. function GetUIValues() {
  226. Res = dlg.findElement("Res").text;
  227.  
  228. AddBlur = dlg.findElement("Blur").value;
  229. AddVeg = dlg.findElement("Veg").value;
  230. AddSP = dlg.findElement("SP").value;
  231. Prefix = dlg.findElement("Prefix").text;
  232. AllowNonGroupLayers = dlg.findElement("allowLayers").value;
  233.  
  234. Path = dlg.findElement("path").text;
  235.  
  236. var c = /\\/g;
  237. if (Path.match(c)) {
  238. Path = Path.replace(c, "/");
  239. };
  240.  
  241. c = ':';
  242. if (Path.match(c )) {
  243. Path = Path.replace(c , '');
  244. };
  245.  
  246. c = /\"/g;
  247. if (Path.match(c )) {
  248. Path = Path.replace(c , '');
  249. };
  250.  
  251. c = /\'/g;
  252. if (Path.match(c )) {
  253. Path = Path.replace(c , '');
  254. };
  255.  
  256. Path = "/" + Path;
  257. }
  258.  
  259. function SaveJPEG(saveFile, jpegQuality) {
  260. jpgSaveOptions = new JPEGSaveOptions();
  261. jpgSaveOptions.embedColorProfile = true;
  262. jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  263. jpgSaveOptions.matte = MatteType.NONE;
  264. jpgSaveOptions.quality = jpegQuality;
  265. activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
  266. }
  267.  
  268. function SavePNG(saveFile) {
  269. pngSaveOptions = new PNGSaveOptions(9, false);
  270. activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
  271. }
  272.  
  273. function TrimResizeAndSave(group) {
  274. var doc = app.activeDocument;
  275. var savedState = doc.activeHistoryState;
  276.  
  277. // trim
  278. doc.trim(TrimType.TRANSPARENT, true, true, true, true);
  279.  
  280. // we need to find the current width and height of the picture now
  281. // this way we can find the right amount of pixels the picture should be blurred
  282. // to keep the blur even across all the pictures.
  283. // ========== set units to pixels ==========//
  284. var oldPrefs = app.preferences.rulerUnits;
  285. app.preferences.rulerUnits = Units.PIXELS;
  286. DocTrimmedWidth = doc.width;
  287. DocTrimmedHeight = doc.height;
  288.  
  289. // resize algorithm
  290. if (DocTrimmedHeight > DocTrimmedWidth) {
  291. doc.resizeImage(null, UnitValue(Res, "px"),72, ResampleMethod.BICUBIC, 100);
  292. }
  293. else {
  294. doc.resizeImage(UnitValue(Res, "px"), null, 72, ResampleMethod.BICUBIC, 100);
  295. }
  296.  
  297. app.preferences.rulerUnits = oldPrefs;
  298. // we save the ouput
  299. if (AddBlur)
  300. saveFile = File(Path + "/" + "B0-" + Prefix + group.name + Suffix + ".png");
  301. else saveFile = File(Path + "/" + Prefix + group.name + Suffix + ".png");
  302. SavePNG(saveFile);
  303.  
  304. doc.activeHistoryState = savedState;
  305. }
  306.  
  307. function CreateBlurredVersion(group, blurStrength, number, scale)
  308. {
  309. var doc = app.activeDocument;
  310. var savedState = doc.activeHistoryState;
  311.  
  312. var blurredPath = File(Path + "/" + "B" + number + "-" + Prefix + group.name + Suffix + ".png");
  313.  
  314. var theName = group.name;
  315. // we need to find the current width and height of the picture now
  316. // this way we can find the right amount of pixels the picture should be blurred
  317. // to keep the blur even across all the pictures.
  318. // ========== set units to pixels ==========//
  319. var oldPrefs = app.preferences.rulerUnits;
  320. app.preferences.rulerUnits = Units.PIXELS;
  321.  
  322. doc.activeHistoryState = savedState;
  323.  
  324. // transform document to smart object
  325. //~ var result = createSmartObject(group);
  326. var group = group != undefined ? group : doc.activeLayer;
  327. if(doc.activeLayer != group) doc.activeLayer = group;
  328.  
  329. var result = group.merge();
  330.  
  331. // gaussian blur
  332. //~ var v2= DocTrimmedWidth * DocTrimmedHeight;
  333. //~ Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {return v1 / sqrt(512* 512 / v2)}}}
  334. //~ result.applyGaussianBlur(Math.blur(blurStrength, v2));
  335.  
  336. Math.blur = function(v1, v2) {
  337. with(this) {
  338. (b = result.bounds), result.applyGaussianBlur
  339. (v1 * sqrt((b[2] - b[0]) * (b[3] - b[1]) / pow(v2, 2)))
  340. }
  341. }
  342.  
  343. Math.blur(blurStrength, 512)
  344.  
  345.  
  346.  
  347. doc.trim(TrimType.TRANSPARENT, true, true, true, true);
  348. BlurredTrimmedWidth = doc.width;
  349. BlurredTrimmedHeight = doc.height;
  350.  
  351. // resize algorithm
  352. if (BlurredTrimmedHeight > BlurredTrimmedWidth) {
  353. doc.resizeImage(null, UnitValue(Res/ scale, "px"), 72, ResampleMethod.BICUBIC, 100);
  354. }
  355. else {
  356. doc.resizeImage(UnitValue(Res/ scale, "px"), null, 72, ResampleMethod.BICUBIC, 100);
  357. }
  358.  
  359. app.preferences.rulerUnits = oldPrefs;
  360.  
  361. // save
  362. SavePNG(blurredPath);
  363. doc.activeHistoryState = savedState;
  364. }
  365.  
  366. function createSmartObject(layer)
  367. {
  368. var doc = app.activeDocument;
  369. var layer = layer != undefined ? layer : doc.activeLayer;
  370.  
  371. if(doc.activeLayer != layer) doc.activeLayer = layer;
  372.  
  373. try
  374. {
  375. var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
  376. executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
  377. return doc.activeLayer;
  378. }
  379. catch(e)
  380. {
  381. return undefined;
  382. }
  383. }
  384.  
  385.  
  386. function SetSuffix()
  387. {
  388. if (AddSP && AddVeg)
  389. Suffix = "-VG-SP";
  390. else if (AddSP)
  391. Suffix = "-SP";
  392. else if (AddVeg)
  393. Suffix = "-VG";
  394. }
  395.  
  396. }
  397. else eval(p + ' = ' + !p), vbs.remove(), alert("Finished exporting files")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement