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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
var sceneData; var sceneDataParts; var duppedPsd; var tmpDoc; var tmpDocBaseState; var destinationFolder; var uuid; var sourcePsdName; var layerCount = 0; var layerSetsCount = 0; var getLayerRec1Count = 0; var dirpath; var pngSaveOptions; var pngSaveAsOptions;
var FAST_PNG8 = true;
var USE_SAVE_FOR_WEB = false;
var ENABLE_PROFILING = true; var prof = { dup_ms: 0, switch_ms: 0, raster_ms: 0, crop_ms: 0, export_ms: 0 };
var RASTERIZE_BEFORE_EXPORT = false;
main();
function main() { try { if (app.documents.length <= 0) { if (app.playbackDisplayDialogs != DialogModes.NO) { alert("You must have a document open to export!"); } return 'cancel'; }
destinationFolder = Folder.selectDialog("Choose the destination for export."); if (!destinationFolder) return;
uuid = 1; sourcePsdName = app.activeDocument.name; var layerCount1 = app.documents[sourcePsdName].layers.length; var layerSetsCount1 = app.documents[sourcePsdName].layerSets.length; if ((layerCount1 <= 1) && (layerSetsCount1 <= 0)) { if (app.playbackDisplayDialogs != DialogModes.NO) { alert("You need a document with multiple layers to export!"); return 'cancel'; } }
var startTime = new Date();
var savedRulerUnits = app.preferences.rulerUnits; var savedTypeUnits = app.preferences.typeUnits; app.preferences.rulerUnits = Units.PIXELS; app.preferences.typeUnits = TypeUnits.PIXELS;
duppedPsd = app.activeDocument.duplicate(); duppedPsd.activeLayer = duppedPsd.layers[duppedPsd.layers.length - 1];
tmpDoc = app.documents.add(duppedPsd.width, duppedPsd.height, duppedPsd.resolution, "tmp_export_psdui", NewDocumentMode.RGB, DocumentFill.TRANSPARENT); tmpDocBaseState = tmpDoc.activeHistoryState;
pngSaveOptions = new ExportOptionsSaveForWeb(); pngSaveOptions.format = SaveDocumentType.PNG; pngSaveOptions.PNG8 = FAST_PNG8; pngSaveOptions.transparency = true; pngSaveOptions.interlaced = false; try { pngSaveOptions.includeProfile = false; } catch (ignoredProfile) { }
pngSaveAsOptions = new PNGSaveOptions(); pngSaveAsOptions.interlaced = false;
sceneDataParts = []; sceneDataParts.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sceneDataParts.push("<PSDUI>"); sceneDataParts.push("<psdSize>"); sceneDataParts.push("<width>" + duppedPsd.width.value + "</width>"); sceneDataParts.push("<height>" + duppedPsd.height.value + "</height>"); sceneDataParts.push("</psdSize>");
exportLayerSet(duppedPsd);
sceneDataParts.push("</PSDUI>"); sceneData = sceneDataParts.join("");
SumObj(duppedPsd); try { tmpDoc.close(SaveOptions.DONOTSAVECHANGES); } catch (ignoredClose) { } duppedPsd.close(SaveOptions.DONOTSAVECHANGES);
var sceneFile = new File(destinationFolder + "/" + destinationFolder.name + ".xml"); sceneFile.open('w'); sceneFile.writeln(sceneData); sceneFile.close();
app.preferences.rulerUnits = savedRulerUnits; app.preferences.typeUnits = savedTypeUnits;
var totalTime = (new Date() - startTime) / 1000; var minutes = Math.floor(totalTime / 60); var seconds = Math.floor(totalTime % 60);
var cnt = "总图层:" + layerCount + " 总图层组:" + layerSetsCount + " 耗时: " + minutes + " 分 " + seconds + " 秒" + " 计算大小的次数:" + getLayerRec1Count; if (ENABLE_PROFILING) { cnt += "\n复制图层: " + Math.round(prof.dup_ms) + "ms" + " 切换文档: " + Math.round(prof.switch_ms) + "ms" + " 栅格化: " + Math.round(prof.raster_ms) + "ms" + " 裁切: " + Math.round(prof.crop_ms) + "ms" + " 导出写盘: " + Math.round(prof.export_ms) + "ms"; } $.writeln(cnt); alert(cnt); } finally { } }
function exportLayerSet(obj) { if (obj.name.search("#") >= 0) return;
sceneDataParts.push("<layers>"); for (var i = obj.layers.length - 1; 0 <= i; i--) { if (obj.layers[i].typename == "LayerSet") { sceneDataParts.push("<Layer>"); sceneDataParts.push("<type>Normal</type>"); sceneDataParts.push("<name>" + obj.layers[i].name + "</name>"); exportLayerSet(obj.layers[i]); sceneDataParts.push("<images>"); for (var j = obj.layers[i].artLayers.length - 1; 0 <= j; j--) { exportArtLayer(obj.layers[i].artLayers[j]); } sceneDataParts.push("</images>"); sceneDataParts.push("</Layer>"); } } sceneDataParts.push("</layers>"); }
function SumObj(obj) { sunLayerCount(obj); }
function sunLayerCount(obj) { for (var i = obj.layers.length - 1; 0 <= i; i--) { if (obj.layers[i].typename == "LayerSet") { layerSetsCount++; sunLayerCount(obj.layers[i]); } else { layerCount++; } } }
function exportArtLayer(obj) { if (obj.name.search("#") >= 0) return;
sceneDataParts.push("<Image>\n"); if (LayerKind.TEXT == obj.kind) { exportLabel(obj); } else { exportImage(obj); } sceneDataParts.push("</Image>\n"); }
function exportLabel(obj) { sceneDataParts.push("<imageType>Label</imageType>\n"); var validFileName = makeValidFileName(obj.name); sceneDataParts.push("<name>" + validFileName + "</name>\n");
saveSingleLayerPng(obj, validFileName, false);
sceneDataParts.push("<arguments>"); sceneDataParts.push("<string>" + obj.textItem.color.rgb.hexValue + "</string>"); sceneDataParts.push("<string>" + obj.textItem.font + "</string>"); sceneDataParts.push("<string>" + obj.textItem.size.value + "</string>"); sceneDataParts.push("<string>" + obj.textItem.contents + "</string>"); sceneDataParts.push("</arguments>"); }
function exportImage(obj) { var validFileName = makeValidFileName(obj.name); sceneDataParts.push("<name>" + validFileName + "</name>\n");
if (obj.name.search("Common") >= 0) { sceneDataParts.push("<imageSource>Common</imageSource>\n"); saveSingleLayerPng(obj, validFileName, false); } else { sceneDataParts.push("<imageSource>Custom</imageSource>\n"); saveSingleLayerPng(obj, validFileName, true); }
if (obj.name.search("9Slice") >= 0) { sceneDataParts.push("<imageType>SliceImage</imageType>\n"); } else { sceneDataParts.push("<imageType>Image</imageType>\n"); } }
function saveSingleLayerPng(layer, fileName, writeToDisk) { getLayerRec1Count++;
var b = layer.bounds; var l = b[0].value, t = b[1].value, r = b[2].value, bt = b[3].value; var width = Math.max(0, r - l); var height = Math.max(0, bt - t);
if (width === 0 || height === 0) { sceneDataParts.push("<position><x>0</x><y>0</y></position>"); sceneDataParts.push("<size><width>0</width><height>0</height></size>"); return; }
var docW = duppedPsd.width.value; var docH = duppedPsd.height.value; var centerX = (l + r) / 2; var centerY = (t + bt) / 2; var x = centerX - (docW / 2); var y = -(centerY - (docH / 2));
if (writeToDisk) { var ts = ENABLE_PROFILING ? new Date().getTime() : 0; app.activeDocument = tmpDoc; tmpDoc.activeHistoryState = tmpDocBaseState; if (ENABLE_PROFILING) prof.switch_ms += (new Date().getTime() - ts);
ts = ENABLE_PROFILING ? new Date().getTime() : 0; app.activeDocument = duppedPsd; if (ENABLE_PROFILING) prof.switch_ms += (new Date().getTime() - ts);
var t0 = ENABLE_PROFILING ? new Date().getTime() : 0; layer.duplicate(tmpDoc, ElementPlacement.PLACEATBEGINNING); if (ENABLE_PROFILING) prof.dup_ms += (new Date().getTime() - t0);
ts = ENABLE_PROFILING ? new Date().getTime() : 0; app.activeDocument = tmpDoc; if (ENABLE_PROFILING) prof.switch_ms += (new Date().getTime() - ts); tmpDoc.activeLayer = tmpDoc.layers[0];
if (RASTERIZE_BEFORE_EXPORT) { var tr = ENABLE_PROFILING ? new Date().getTime() : 0; try { tmpDoc.activeLayer.rasterize(RasterizeType.ENTIRELAYER); } catch (ignoredRaster) { } if (ENABLE_PROFILING) prof.raster_ms += (new Date().getTime() - tr); }
var t1 = ENABLE_PROFILING ? new Date().getTime() : 0; tmpDoc.crop([UnitValue(l, "px"), UnitValue(t, "px"), UnitValue(r, "px"), UnitValue(bt, "px")]); if (ENABLE_PROFILING) prof.crop_ms += (new Date().getTime() - t1);
var pngFile = new File(destinationFolder + "/" + fileName + ".png"); var t2 = ENABLE_PROFILING ? new Date().getTime() : 0; if (USE_SAVE_FOR_WEB) { tmpDoc.exportDocument(pngFile, ExportType.SAVEFORWEB, pngSaveOptions); } else { tmpDoc.saveAs(pngFile, pngSaveAsOptions, true, Extension.LOWERCASE); } if (ENABLE_PROFILING) prof.export_ms += (new Date().getTime() - t2); }
sceneDataParts.push("<position>"); sceneDataParts.push("<x>" + x + "</x>"); sceneDataParts.push("<y>" + y + "</y>"); sceneDataParts.push("</position>");
sceneDataParts.push("<size>"); sceneDataParts.push("<width>" + width + "</width>"); sceneDataParts.push("<height>" + height + "</height>"); sceneDataParts.push("</size>"); }
function makeValidFileName(fileName) { var validName = fileName.replace(/^\s+|\s+$/gm, ''); validName = validName.replace(/[\\\*\/\?:"\|<>]/g, ''); validName = validName.replace(/[ ]/g, '_');
if (validName.match("Common")) { validName = validName.substring(validName.lastIndexOf("@") + 1); } else if (!sourcePsdName.match("Common")) { validName += "_" + uuid++; }
return validName; }
|