forked from hooraygith/photoshop-slicer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slicer.jsx
67 lines (49 loc) · 1.85 KB
/
slicer.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
64
65
66
67
#target photoshop
// PROMPT FOR USER INPUT
var fileSuffix = prompt( "Input the file name of HTML", "" );
if( !fileSuffix ){
fileSuffix = "export";
}
function getPngOptions() {
var pngExportOptions = new ExportOptionsSaveForWeb();
pngExportOptions.format = SaveDocumentType.PNG;
pngExportOptions.optimized = true;
pngExportOptions.PNG8 = false;
return pngExportOptions;
}
function slicesExporter() {
// the history before merge
var historyBefore = app.activeDocument.historyStates.length;
// all layers
var layers = app.activeDocument.artLayers;
// merge all layers
if (layers.length > 1) {
app.activeDocument.mergeVisibleLayers();
}
// GET ACTIVE DOCUMENT
var currentDocument = app.activeDocument;
// GET ACTIVE DOCUMENT FILENAME
var currentFilename = currentDocument.fullName;
// GET ACTIVE DOCUMENT FOLDER
var folder = currentFilename.parent.selectDlg("Select a directory of HTML");
// GET PNG EXPORT OPTIONS
var options = getPngOptions();
// SETS AN HISTORY STATE TO RECOVER AFTER THE SCRIPT RUNS
var historySavedState = currentDocument.activeHistoryState;
// RESET SLICES LAYER GROUP EXISTENCE
var layer = layers[0];
// slice
activeDocument.crop( layer.bounds, 0, layer.bounds[2] - layer.bounds[0], layer.bounds[3] - layer.bounds[1] );
// CREATES NEW PNG FILE
var file = new File(folder.fsName+'/'+ fileSuffix + '.png');
// EXPORTS FILE
currentDocument.exportDocument( file, ExportType.SAVEFORWEB, options );
// REWINDS HISTORY STATE TO A STATE BEFORE THE SCRIPT RUNS
currentDocument.activeHistoryState = historySavedState;
// back to history
var history = currentDocument.historyStates;
currentDocument.activeHistoryState = history[historyBefore - 1];
alert("Export success!");
}
// slicesExporter();
slicesExporter();