forked from fierayan/PNG-2x-Export
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPNG_2x_Export.jsx
63 lines (53 loc) · 2.17 KB
/
PNG_2x_Export.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#target photoshop
app.bringToFront();
function main(){
var doc = app.activeDocument;
var psdPath = app.activeDocument.path;
var outFolderName="pngExport";
var outFolder = new Folder(psdPath + "/"+outFolderName);
if (!outFolder.exists) {
outFolder.create();
}
var lname = doc.activeLayer.name;
saveLayer(doc.activeLayer, lname, psdPath, false);
function saveLayer(layer, lname, path, shouldMerge) {
dupLayers();
if (shouldMerge === undefined || shouldMerge === true) {
activeDocument.mergeVisibleLayers();
}
activeDocument.trim(TrimType.TRANSPARENT,true,true,true,true);
if(activeDocument.width % 2 != 0) {
activeDocument.resizeCanvas(activeDocument.width + 1, activeDocument.height, AnchorPosition.MIDDLECENTER);
}
if(activeDocument.height % 2 != 0) {
activeDocument.resizeCanvas(activeDocument.width, activeDocument.height+ 1, AnchorPosition.MIDDLECENTER);
}
var saveFile= File(path +"/"+outFolderName+"/"+lname+"@2x.png");
SavePNG(saveFile);
activeDocument.resizeImage(undefined,undefined, app.activeDocument.resolution / 2, ResampleMethod.BICUBICSHARPER);
saveFile=File(path +"/"+outFolderName+"/"+lname+".png");
SavePNG(saveFile);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
main();
function dupLayers() {
var descac = new ActionDescriptor();
var refac1 = new ActionReference();
refac1.putClass( charIDToTypeID('Dcmn') );
descac.putReference( charIDToTypeID('null'), refac1 );
descac.putString( charIDToTypeID('Nm '), activeDocument.activeLayer.name );
var refac2 = new ActionReference();
refac2.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
descac.putReference( charIDToTypeID('Usng'), refac2 );
executeAction( charIDToTypeID('Mk '), descac, DialogModes.NO );
}
function SavePNG(saveFile){
var pngOpts = new ExportOptionsSaveForWeb;
pngOpts.format = SaveDocumentType.PNG
pngOpts.PNG8 = false;
pngOpts.transparency = true;
pngOpts.interlaced = false;
pngOpts.quality = 100;
activeDocument.exportDocument(new File(saveFile),ExportType.SAVEFORWEB,pngOpts);
}