-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinStoreHelpers.jsx
More file actions
236 lines (192 loc) · 6.59 KB
/
Copy pathWinStoreHelpers.jsx
File metadata and controls
236 lines (192 loc) · 6.59 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
function centerLayer(layer, doc)
{
app.activeDocument = doc;
var bounds = layer.bounds;
var layerWidth = Number(bounds[2] - bounds[0]);
var layerHeight = Number(bounds[3] - bounds[1]);
var docWidth = Number(doc.width);
var docHeight = Number(doc.height);
// calculate offsets
var dX = (docWidth - layerWidth) / 2 - Number(bounds[0]);
var dY = (docHeight - layerHeight) / 2 - Number(bounds[1]);
// centers the active layer
layer.translate(dX, dY);
}
function resizeAndAddToDoc(image, height, width, doc)
{
app.activeDocument = image;
var startState = image.activeHistoryState; //to use on the redo
image.resizeImage(width, height, null, ResampleMethod.BICUBICSHARPER);
image.artLayers.add();
image.mergeVisibleLayers();
// Add and center layer
app.activeDocument = image;
var layer = image.activeLayer.duplicate(doc, ElementPlacement.PLACEATBEGINNING);
centerLayer(layer, doc);
//redo
app.activeDocument = image;
image.activeHistoryState = startState;
}
function saveImageAndCloseDoc(doc, destfile)
{
var sfw = new ExportOptionsSaveForWeb();
sfw.format = SaveDocumentType.PNG;
sfw.PNG8 = false; // use PNG-24
sfw.transparency = true;
app.activeDocument = doc;
doc.exportDocument(destfile, ExportType.SAVEFORWEB, sfw);
doc.close(SaveOptions.DONOTSAVECHANGES);
}
///////////////////////////////////////////////////////////////////////////////
// findLayer - iterate through layers to find a match
// Source http://morris-photographics.com/photoshop/scripts/find-layer.html
///////////////////////////////////////////////////////////////////////////////
function findLayer(ref, name)
{
// declare local variables
var layers = ref.layers;
var len = layers.length;
var match = false;
// iterate through layers to find a match
for (var i = 0; i < len; i++)
{
// test for matching layer
var layer = layers[i];
if (layer.name.toLowerCase() == name.toLowerCase())
{
// select matching layer
app.activeDocument.activeLayer = layer;
match = true;
break;
}
// handle groups (layer sets)
else if (layer.typename == 'LayerSet')
{
match = findLayer(layer, name);
if (match)
{
break;
}
}
}
return match;
}
function setVisibleOnLayer(doc, name, value)
{
app.activeDocument = doc;
if (findLayer(doc, name))
{
app.activeDocument.activeLayer.visible = value;
}
}
function setBackgroundLayerVisible(doc, value, targetWidth, targetHeight)
{
setVisibleOnLayer(doc, backgroundLayerName, value);
if (value)
{
doc.activeLayer.resize(targetWidth, targetWidth, AnchorPosition.TOPLEFT)
}
}
function setBackgroundColor(doc, color)
{
app.activeDocument = doc;
var theLayers = doc.layers;
doc.activeLayer = theLayers[theLayers.length - 1];
var aLayer = doc.activeLayer;
if (aLayer.isBackgroundLayer)
{
app.activeDocument.selection.fill(color, ColorBlendMode.NORMAL, 100, false);
}
}
function processAssets(appAssets)
{
try
{
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
//
// Layer names for the title and background.
//
var backgroundLayerName = "Background";
var appTitleLayerName = "AppTitle";
//var tile = new File("D:\FAC/Programing/Projects/AdMesh/Design/Tile/Tile.psd");
//var destFolder = new Folder("D:\PSTest/");
//
// Retrieve the psd and the destination folder
//
var destFolder = Folder.selectDialog("Choose an output folder");
var psdFile = File.openDialog("Select a sqaure PNG file that is at least 1024x1024.", "*.psd", false);
var asset;
var initialPrefs = app.preferences.rulerUnits; // will restore at end
//
// Open the psd.
//
var psd = open(psdFile);
//
// A bool to reflect if the generation will use a single background color.
//
var useBackground = false;
//
// If there is a BackgroundLayer
// Hammer the bg color discovery, by cecking the color of the first pixel.
//
if (findLayer(psd, backgroundLayerName))
{
var sampler = psd.colorSamplers.add([0, 0]);
sampler.move([0, 0]);
var bgColor = sampler.color;
useBackground = true;
}
//
// Create the folder if it doesn't exist
//
var appAssetsFolder = destFolder;
if (!appAssetsFolder.exists)
appAssetsFolder.create();
for (i = 0; i < appAssets.length; i++)
{
asset = appAssets[i];
//
// Temporary variables.
//
var assetWidth = asset.width != null ? asset.width : asset.size;
var assetHeight = asset.height != null ? asset.height : asset.size;
var createDocWithBg = asset.background != null && asset.background;
//
// Create a temporary psd to generate the target asset.
//
var newDoc = app.documents.add(assetWidth, assetHeight, 72.0, asset.name, NewDocumentMode.RGB, createDocWithBg ? DocumentFill.BACKGROUNDCOLOR : DocumentFill.TRANSPARENT);
//
// Set as visible the psd layers; Background and Title to the temp psd if the asset requires it and the layer exists.
//
setVisibleOnLayer(psd, backgroundLayerName, asset.background == null ? false : asset.background);
setVisibleOnLayer(psd, appTitleLayerName, asset.title == null ? false : asset.title);
//
// Resize and center everything and copy it to the new psd.
//
resizeAndAddToDoc(psd, asset.size, asset.size, newDoc);
if (useBackground)
setBackgroundColor(newDoc, bgColor)
//
// Finally save the psd in a new file.
//
var destFileName = asset.name + ".png";
saveImageAndCloseDoc(newDoc, new File(appAssetsFolder + "/" + destFileName));
}
//
// Close the original psd without saving any changes.
//
psd.close(SaveOptions.DONOTSAVECHANGES);
}
catch (exception)
{
if ((exception != null))
alert(exception);
}
finally
{
if (psd != null)
psd.close(SaveOptions.DONOTSAVECHANGES);
app.preferences.rulerUnits = initialPrefs;
}
}