// 日本語認識用 "object"!=typeof JSON&&(JSON={}),function(){"use strict";function f(t){return 10>t?"0"+t:t}function this_value(){return this.valueOf()}function quote(t){return rx_escapable.lastIndex=0,rx_escapable.test(t)?'"'+t.replace(rx_escapable,function(t){var e=meta[t];return"string"==typeof e?e:"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+t+'"'}function str(t,e){var r,n,o,u,f,a=gap,i=e[t];switch(i&&"object"==typeof i&&"function"==typeof i.toJSON&&(i=i.toJSON(t)),"function"==typeof rep&&(i=rep.call(e,t,i)),typeof i){case"string":return quote(i);case"number":return isFinite(i)?i+"":"null";case"boolean":case"null":return i+"";case"object":if(!i)return"null";if(gap+=indent,f=[],"[object Array]"===Object.prototype.toString.apply(i)){for(u=i.length,r=0;u>r;r+=1)f[r]=str(r,i)||"null";return o=0===f.length?"[]":gap?"[\n"+gap+f.join(",\n"+gap)+"\n"+a+"]":"["+f.join(",")+"]",gap=a,o}if(rep&&"object"==typeof rep)for(u=rep.length,r=0;u>r;r+=1)"string"==typeof rep[r]&&(n=rep[r],o=str(n,i),o&&f.push(quote(n)+(gap?": ":":")+o));else for(n in i)Object.prototype.hasOwnProperty.call(i,n)&&(o=str(n,i),o&&f.push(quote(n)+(gap?": ":":")+o));return o=0===f.length?"{}":gap?"{\n"+gap+f.join(",\n"+gap)+"\n"+a+"}":"{"+f.join(",")+"}",gap=a,o}}var rx_one=/^[\],:{}\s]*$/,rx_two=/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,rx_three=/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,rx_four=/(?:^|:|,)(?:\s*\[)+/g,rx_escapable=/[\\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,rx_dangerous=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;"function"!=typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null},Boolean.prototype.toJSON=this_value,Number.prototype.toJSON=this_value,String.prototype.toJSON=this_value);var gap,indent,meta,rep;"function"!=typeof JSON.stringify&&(meta={"\b":"\\b"," ":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},JSON.stringify=function(t,e,r){var n;if(gap="",indent="","number"==typeof r)for(n=0;r>n;n+=1)indent+=" ";else"string"==typeof r&&(indent=r);if(rep=e,e&&"function"!=typeof e&&("object"!=typeof e||"number"!=typeof e.length))throw Error("JSON.stringify");return str("",{"":t})}),"function"!=typeof JSON.parse&&(JSON.parse=function(text,reviver){function walk(t,e){var r,n,o=t[e];if(o&&"object"==typeof o)for(r in o)Object.prototype.hasOwnProperty.call(o,r)&&(n=walk(o,r),void 0!==n?o[r]=n:delete o[r]);return reviver.call(t,e,o)}var j;if(text+="",rx_dangerous.lastIndex=0,rx_dangerous.test(text)&&(text=text.replace(rx_dangerous,function(t){return"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})),rx_one.test(text.replace(rx_two,"@").replace(rx_three,"]").replace(rx_four,"")))return j=eval("("+text+")"),"function"==typeof reviver?walk({"":j},""):j;throw new SyntaxError("JSON.parse")})}(); // Generated by CoffeeScript 1.12.7 (function() { var Baum, PsdToImage, PsdToJson, Util, baum, setup, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; Baum = (function() { function Baum() { this.runOneFile = bind(this.runOneFile, this); } Baum.version = '0.6.1'; Baum.maxLength = 1334; Baum.prototype.run = function() { var filePath, filePaths, j, len; this.saveFolder = null; if (app.documents.length === 0) { filePaths = File.openDialog("Select a file", "*", true); for (j = 0, len = filePaths.length; j < len; j++) { filePath = filePaths[j]; app.activeDocument = app.open(File(filePath)); this.runOneFile(true); } } else { this.runOneFile(false); } return alert('complete!'); }; Baum.prototype.runOneFile = function(after_close) { var copiedDoc; if (this.saveFolder === null) { this.saveFolder = Folder.selectDialog("保存先フォルダの選択"); } if (this.saveFolder === null) { return; } this.documentName = app.activeDocument.name.slice(0, -4); copiedDoc = app.activeDocument.duplicate(app.activeDocument.name.slice(0, -4) + '.copy.psd'); copiedDoc.quickMaskMode = false; Util.deselectLayers(); this.removeUnvisibleLayers(copiedDoc); this.unlockAll(copiedDoc); this.rasterizeAll(copiedDoc); this.unvisibleAll(copiedDoc); this.layerBlendAll(copiedDoc, copiedDoc); this.removeCommentoutLayers(copiedDoc, copiedDoc); this.cropLayers(copiedDoc); this.resizePsd(copiedDoc); this.selectDocumentArea(copiedDoc); this.ungroupArtboard(copiedDoc); this.clipping(copiedDoc, copiedDoc); copiedDoc.selection.deselect(); this.psdToJson(copiedDoc); this.psdToImage(copiedDoc); copiedDoc.close(SaveOptions.DONOTSAVECHANGES); if (after_close) { return app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); } }; Baum.prototype.selectDocumentArea = function(document) { var selReg, x1, x2, y1, y2; x1 = 0; y1 = 0; x2 = document.width.value; y2 = document.height.value; selReg = [[x1, y1], [x2, y1], [x2, y2], [x1, y2]]; return document.selection.select(selReg); }; Baum.prototype.clipping = function(document, root) { var h, w, x1, x2, y1, y2; document.resizeImage(document.width, document.height, 72, ResampleMethod.BICUBICAUTOMATIC); if (document.selection.bounds[0].value === 0 && document.selection.bounds[1].value === 0 && document.selection.bounds[2].value === document.width.value && document.selection.bounds[3].value === document.height.value) { return; } document.selection.invert(); this.clearAll(document, root); document.selection.invert(); x1 = document.selection.bounds[0]; y1 = document.selection.bounds[1]; x2 = document.selection.bounds[2]; y2 = document.selection.bounds[3]; document.resizeCanvas(x2, y2, AnchorPosition.TOPLEFT); w = x2 - x1; h = y2 - y1; return activeDocument.resizeCanvas(w, h, AnchorPosition.BOTTOMRIGHT); }; Baum.prototype.clearAll = function(document, root) { var j, layer, len, ref1, results; ref1 = root.layers; results = []; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (layer.typename === 'LayerSet') { results.push(this.clearAll(document, layer)); } else if (layer.typename === 'ArtLayer') { if (layer.kind !== LayerKind.TEXT) { document.activeLayer = layer; results.push(document.selection.clear()); } else { results.push(void 0); } } else { results.push(alert(layer)); } } return results; }; Baum.prototype.resizePsd = function(doc) { var height, tmp, width; width = doc.width; height = doc.height; if (width < Baum.maxLength && height < Baum.maxLength) { return; } tmp = 0; if (width > height) { tmp = width / Baum.maxLength; } else { tmp = height / Baum.maxLength; } width = width / tmp; height = height / tmp; return doc.resizeImage(width, height, doc.resolution, ResampleMethod.BICUBICAUTOMATIC); }; Baum.prototype.removeUnvisibleLayers = function(root) { var i, j, k, layer, len, ref1, ref2, removeLayers, results; removeLayers = []; ref1 = root.layers; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (layer.visible === false) { layer.visible = true; } if (layer.bounds[0].value === 0 && layer.bounds[1].value === 0 && layer.bounds[2].value === 0 && layer.bounds[3].value === 0) { removeLayers.push(layer); continue; } if (layer.typename === 'LayerSet') { this.removeUnvisibleLayers(layer); } } if (removeLayers.length > 0) { results = []; for (i = k = ref2 = removeLayers.length - 1; ref2 <= 0 ? k <= 0 : k >= 0; i = ref2 <= 0 ? ++k : --k) { results.push(removeLayers[i].remove()); } return results; } }; Baum.prototype.removeCommentoutLayers = function(document, root) { var i, j, k, layer, len, ref1, ref2, removeLayers, results; removeLayers = []; ref1 = root.layers; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (layer.name.startsWith('#')) { removeLayers.push(layer); continue; } if (layer.typename === 'LayerSet') { this.removeCommentoutLayers(document, layer); } } if (root.typename === 'LayerSet') { document.activeLayer = root; } if (removeLayers.length > 0) { results = []; for (i = k = ref2 = removeLayers.length - 1; ref2 <= 0 ? k <= 0 : k >= 0; i = ref2 <= 0 ? ++k : --k) { results.push(removeLayers[i].remove()); } return results; } }; Baum.prototype.cropLayers = function(root) { var bounds; bounds = [0, 0, root.width, root.height]; return root.crop(bounds); }; Baum.prototype.rasterizeAll = function(root) { var j, layer, len, ref1, results, t; ref1 = root.layers; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (layer.name.startsWith('*')) { layer.name = layer.name.slice(1).strip(); if (layer.typename === 'LayerSet') { Util.mergeGroup(layer); } else { this.rasterize(layer); } } else if (layer.typename === 'LayerSet') { this.rasterizeAll(layer); } else if (layer.typename === 'ArtLayer') { if (layer.kind !== LayerKind.TEXT) { this.rasterize(layer); } } else { alert(layer); } } t = 0; results = []; while (t < root.layers.length) { if (root.layers[t].visible && root.layers[t].grouped) { results.push(root.layers[t].merge()); } else { results.push(t += 1); } } return results; }; Baum.prototype.rasterize = function(layer) { var tmp; tmp = app.activeDocument.activeLayer; app.activeDocument.activeLayer = layer; if (layer.blendMode !== BlendMode.OVERLAY && layer.kind !== LayerKind.HUESATURATION && layer.opacity > 1) { Util.rasterizeLayerStyle(layer); } layer.rasterize(RasterizeType.ENTIRELAYER); Util.rasterizeLayerMask(layer); return app.activeDocument.activeLayer = tmp; }; Baum.prototype.ungroupArtboard = function(document) { var j, layer, len, ref1, results; ref1 = document.layers; results = []; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (layer.name.startsWith('Artboard') && layer.typename === 'LayerSet') { results.push(this.ungroup(layer)); } else { results.push(void 0); } } return results; }; Baum.prototype.ungroup = function(root) { var i, j, layer, layers, ref1; layers = (function() { var j, len, ref1, results; ref1 = root.layers; results = []; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; results.push(layer); } return results; })(); for (i = j = 0, ref1 = layers.length; 0 <= ref1 ? j < ref1 : j > ref1; i = 0 <= ref1 ? ++j : --j) { layers[i].moveBefore(root); } return root.remove(); }; Baum.prototype.unlockAll = function(root) { var j, layer, len, ref1, results; ref1 = root.layers; results = []; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (layer.typename === 'LayerSet') { results.push(this.unlockAll(layer)); } else { if (layer.allLocked) { results.push(layer.allLocked = false); } else { results.push(void 0); } } } return results; }; Baum.prototype.unvisibleAll = function(root) { var j, layer, len, ref1, results; ref1 = root.layers; results = []; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (layer.typename === 'LayerSet') { results.push(this.unvisibleAll(layer)); } else { results.push(layer.visible = false); } } return results; }; Baum.prototype.layerBlendAll = function(document, root) { var i, j, layer, newLayer, ref1, results; if (root.layers.length === 0) { return; } results = []; for (i = j = ref1 = root.layers.length - 1; ref1 <= 0 ? j <= 0 : j >= 0; i = ref1 <= 0 ? ++j : --j) { layer = root.layers[i]; if (layer.typename === 'LayerSet') { results.push(this.layerBlendAll(document, layer)); } else { layer.visible = true; if (layer.blendMode !== BlendMode.OVERLAY && layer.kind !== LayerKind.HUESATURATION) { continue; } document.activeLayer = layer; try { Util.selectTransparency(); document.selection.bounds; document.selection.copy(true); } catch (error) { layer.copy(true); } document.paste(); newLayer = document.activeLayer; newLayer.name = layer.name; document.activeLayer = layer; Util.selectTransparency(); document.selection.invert(); document.activeLayer = newLayer; try { document.selection.bounds; document.selection.cut(); } catch (error) {} results.push(layer.remove()); } } return results; }; Baum.prototype.psdToJson = function(targetDocument) { var json, toJson; toJson = new PsdToJson(); json = toJson.run(targetDocument, this.documentName); return Util.saveText(this.saveFolder + "/" + this.documentName + ".layout.txt", json); }; Baum.prototype.psdToImage = function(targetDocument) { var json, toImage; toImage = new PsdToImage(); return json = toImage.run(targetDocument, this.saveFolder, this.documentName); }; return Baum; })(); PsdToJson = (function() { function PsdToJson() {} PsdToJson.prototype.run = function(document, documentName) { var bounds, canvasBase, canvasLayer, canvasSize, imageSize, json, layers; layers = this.allLayers(document, document); imageSize = [document.width.value, document.height.value]; canvasSize = [document.width.value, document.height.value]; canvasBase = [document.width.value / 2, document.height.value / 2]; canvasLayer = this.findLayer(document, '#Canvas'); if (canvasLayer) { bounds = canvasLayer.bounds; canvasSize = [bounds[2].value - bounds[0].value, bounds[3].value - bounds[1].value]; canvasBase = [(bounds[2].value + bounds[0].value) / 2, (bounds[3].value + bounds[1].value) / 2]; } json = JSON.stringify({ info: { version: Baum.version, canvas: { image: { w: imageSize[0], h: imageSize[1] }, size: { w: canvasSize[0], h: canvasSize[1] }, base: { x: canvasBase[0], y: canvasBase[1] } } }, root: { type: 'Root', name: documentName, elements: layers } }); return json; }; PsdToJson.prototype.findLayer = function(root, name) { var j, layer, len, ref1; ref1 = root.layers; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (layer.name === name) { return layer; } } return null; }; PsdToJson.prototype.allLayers = function(document, root) { var hash, j, layer, layers, len, name, opt, ref1; layers = []; ref1 = root.layers; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (!layer.visible) { continue; } hash = null; name = layer.name.split("@")[0]; opt = Util.parseOption(layer.name.split("@")[1]); if (layer.typename === 'ArtLayer') { hash = this.layerToHash(document, name, opt, layer); } else { hash = this.groupToHash(document, name, opt, layer); } if (hash) { hash['name'] = name; layers.push(hash); } } return layers; }; PsdToJson.prototype.parseOption = function(text) { var elements, j, len, opt, optText, ref1; if (!text) { return {}; } opt = {}; ref1 = text.split(","); for (j = 0, len = ref1.length; j < len; j++) { optText = ref1[j]; elements = optText.split("="); if (elements.length === 1) { elements[1] = 'true'; } opt[elements[0].toLowerCase()] = elements[1].toLowerCase(); } return opt; }; PsdToJson.prototype.layerToHash = function(document, name, opt, layer) { var align, bounds, e, hash, hh, originalText, pos, scale, text, textCenterOffset, textColor, textSize, textType, vh, vx, vy, ww; document.activeLayer = layer; hash = {}; if (layer.kind === LayerKind.TEXT) { text = layer.textItem; textSize = parseFloat(this.getTextSize()); textType = 'paragraph'; scale = Util.getTextYScale(text) / 0.9; if (text.kind !== TextType.PARAGRAPHTEXT) { text.kind = TextType.PARAGRAPHTEXT; textType = 'point'; text.height = textSize * (2.0 / scale); textCenterOffset = text.size.value; pos = [text.position[0].value, text.position[1].value]; pos[1] = pos[1] - (textCenterOffset / (2.0 / scale)); text.position = pos; } originalText = text.contents.replace(/\r\n/g, '__CRLF__').replace(/\r/g, '__CRLF__').replace(/\n/g, '__CRLF__').replace(/__CRLF__/g, '\r\n'); text.contents = "Z"; bounds = Util.getTextExtents(text); vx = bounds.x; vy = bounds.y; ww = bounds.width; hh = bounds.height; vh = bounds.height; align = ''; textColor = 0x000000; try { align = text.justification.toString().slice(14).toLowerCase(); textColor = text.color.rgb.hexValue; } catch (error) { e = error; align = 'left'; } hash = { type: 'Text', text: originalText, textType: textType, font: text.font, size: textSize, color: textColor, align: align, x: Math.round(vx * 100.0) / 100.0, y: Math.round(vy * 100.0) / 100.0, w: Math.round(ww * 100.0) / 100.0, h: Math.round(hh * 100.0) / 100.0, vh: Math.round(vh * 100.0) / 100.0, opacity: Math.round(layer.opacity * 10.0) / 10.0 }; if (Util.hasStroke(document, layer)) { hash['strokeSize'] = Util.getStrokeSize(document, layer); hash['strokeColor'] = Util.getStrokeColor(document, layer).rgb.hexValue; } } else if (opt['mask']) { hash = { type: 'Mask', image: Util.layerToImageName(layer), x: layer.bounds[0].value, y: layer.bounds[1].value, w: layer.bounds[2].value - layer.bounds[0].value, h: layer.bounds[3].value - layer.bounds[1].value, opacity: Math.round(layer.opacity * 10.0) / 10.0 }; } else { hash = { type: 'Image', image: Util.layerToImageName(layer), x: layer.bounds[0].value, y: layer.bounds[1].value, w: layer.bounds[2].value - layer.bounds[0].value, h: layer.bounds[3].value - layer.bounds[1].value, opacity: Math.round(layer.opacity * 10.0) / 10.0 }; if (opt['prefab']) { hash['prefab'] = opt['prefab']; } if (opt['background']) { hash['background'] = true; } if (opt['slice']) { hash['slice'] = opt['slice']; } } if (opt['pivot']) { hash['pivot'] = opt['pivot']; } if (opt['stretchx']) { hash['stretchx'] = opt['stretchx']; } if (opt['stretchy']) { hash['stretchy'] = opt['stretchy']; } if (opt['stretchxy']) { hash['stretchxy'] = opt['stretchxy']; } return hash; }; PsdToJson.prototype.angleFromMatrix = function(yy, xy) { var toDegs; toDegs = 180 / Math.PI; return Math.atan2(yy, xy) * toDegs - 90; }; PsdToJson.prototype.getActiveLayerTransform = function() { var desc, ref, xx, xy, yx, yy; ref = new ActionReference(); ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey')); if (desc.hasKey(stringIDToTypeID('transform'))) { desc = desc.getObjectValue(stringIDToTypeID('transform')); xx = desc.getDouble(stringIDToTypeID('xx')); xy = desc.getDouble(stringIDToTypeID('xy')); yy = desc.getDouble(stringIDToTypeID('yy')); yx = desc.getDouble(stringIDToTypeID('yx')); return { xx: xx, xy: xy, yy: yy, yx: yx }; } return { xx: 0, xy: 0, yy: 0, yx: 0 }; }; PsdToJson.prototype.getTextSize = function() { var desc, mFactor, ref, textSize; ref = new ActionReference(); ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey')); textSize = desc.getList(stringIDToTypeID('textStyleRange')).getObjectValue(0).getObjectValue(stringIDToTypeID('textStyle')).getDouble(stringIDToTypeID('size')); if (desc.hasKey(stringIDToTypeID('transform'))) { mFactor = desc.getObjectValue(stringIDToTypeID('transform')).getUnitDoubleValue(stringIDToTypeID("yy")); textSize = (textSize * mFactor).toFixed(2); } return textSize; }; PsdToJson.prototype.groupToHash = function(document, name, opt, layer) { var hash; hash = {}; if (name.endsWith('Button')) { hash = { type: 'Button' }; } else if (name.endsWith('List')) { hash = { type: 'List' }; if (opt['scroll']) { hash['scroll'] = opt['scroll']; } } else if (name.endsWith('Slider')) { hash = { type: 'Slider' }; if (opt['scroll']) { hash['scroll'] = opt['scroll']; } } else if (name.endsWith('Scrollbar')) { hash = { type: 'Scrollbar' }; if (opt['scroll']) { hash['scroll'] = opt['scroll']; } } else if (name.endsWith('Toggle')) { hash = { type: 'Toggle' }; } else { hash = { type: 'Group' }; } if (opt['pivot']) { hash['pivot'] = opt['pivot']; } if (opt['stretchx']) { hash['stretchx'] = opt['stretchx']; } if (opt['stretchy']) { hash['stretchy'] = opt['stretchy']; } if (opt['stretchxy']) { hash['stretchxy'] = opt['stretchxy']; } hash['elements'] = this.allLayers(document, layer); return hash; }; return PsdToJson; })(); PsdToImage = (function() { var baseFolder, fileNames; function PsdToImage() {} baseFolder = null; fileNames = []; PsdToImage.prototype.run = function(document, saveFolder, documentName) { var i, j, k, len, ref1, removeFiles, results, snapShotId, target, targets; this.baseFolder = Folder(saveFolder + "/" + documentName); if (this.baseFolder.exists) { removeFiles = this.baseFolder.getFiles(); for (i = j = 0, ref1 = removeFiles.length; 0 <= ref1 ? j < ref1 : j > ref1; i = 0 <= ref1 ? ++j : --j) { if (removeFiles[i].name.startsWith(documentName) && removeFiles[i].name.endsWith('.png')) { removeFiles[i].remove(); } } this.baseFolder.remove(); } this.baseFolder.create(); targets = this.allLayers(document); snapShotId = Util.takeSnapshot(document); results = []; for (k = 0, len = targets.length; k < len; k++) { target = targets[k]; target.visible = true; this.outputLayer(document, target); results.push(Util.revertToSnapshot(document, snapShotId)); } return results; }; PsdToImage.prototype.allLayers = function(root) { var j, layer, len, list, ref1; ref1 = root.layers; for (j = 0, len = ref1.length; j < len; j++) { layer = ref1[j]; if (layer.kind === LayerKind.TEXT) { layer.visible = false; } } list = (function() { var k, len1, ref2, results; ref2 = root.layers; results = []; for (k = 0, len1 = ref2.length; k < len1; k++) { layer = ref2[k]; if (layer.visible) { if (layer.typename === 'ArtLayer') { layer.visible = false; results.push(layer); } else { results.push(this.allLayers(layer)); } } } return results; }).call(this); return Array.prototype.concat.apply([], list); }; PsdToImage.prototype.outputLayer = function(doc, layer) { var fileName, opt, options, saveFile; if (!layer.isBackgroundLayer) { layer.translate(-layer.bounds[0], -layer.bounds[1]); doc.resizeCanvas(layer.bounds[2] - layer.bounds[0], layer.bounds[3] - layer.bounds[1], AnchorPosition.TOPLEFT); doc.trim(TrimType.TRANSPARENT); } layer.opacity = 100.0; fileName = Util.layerToImageName(layer); opt = Util.parseOption(layer.name.split("@")[1]); if (indexOf.call(fileNames, fileName) >= 0) { alert(fileName + "と同名のレイヤーが存在します。レイヤー名を変更してください。"); } if (opt['slice'] === 'false') { fileName += "-noslice"; } fileNames.push(fileName); saveFile = new File(this.baseFolder.fsName + "/" + fileName + ".png"); options = new ExportOptionsSaveForWeb(); options.format = SaveDocumentType.PNG; options.PNG8 = false; options.optimized = true; options.interlaced = false; return doc.exportDocument(saveFile, ExportType.SAVEFORWEB, options); }; return PsdToImage; })(); Util = (function() { function Util() {} Util.saveText = function(filePath, text) { var file; file = File(filePath); file.encoding = "UTF8"; file.open("w", "TEXT"); file.write(text); return file.close(); }; Util.layerToImageName = function(layer) { return encodeURI(Util.layerToImageNameLoop(layer)).replace(/%/g, ''); }; Util.layerToImageNameLoop = function(layer) { var image, imageName; if (layer instanceof Document) { return ""; } image = Util.layerToImageName(layer.parent); imageName = image; if (imageName !== "") { imageName = imageName + "_"; } return imageName + layer.name.split("@")[0].replace('_', '').replace(' ', '-').toLowerCase(); }; Util.getLastSnapshotID = function(doc) { var hsLength, hsObj, i, j, ref1; hsObj = doc.historyStates; hsLength = hsObj.length; for (i = j = ref1 = hsLength - 1; ref1 <= -1 ? j <= -1 : j >= -1; i = ref1 <= -1 ? ++j : --j) { if (hsObj[i].snapshot) { return i; } } }; Util.takeSnapshot = function(doc) { var desc153, ref119, ref120; desc153 = new ActionDescriptor(); ref119 = new ActionReference(); ref119.putClass(charIDToTypeID("SnpS")); desc153.putReference(charIDToTypeID("null"), ref119); ref120 = new ActionReference(); ref120.putProperty(charIDToTypeID("HstS"), charIDToTypeID("CrnH")); desc153.putReference(charIDToTypeID("From"), ref120); executeAction(charIDToTypeID("Mk "), desc153, DialogModes.NO); return Util.getLastSnapshotID(doc); }; Util.revertToSnapshot = function(doc, snapshotID) { return doc.activeHistoryState = doc.historyStates[snapshotID]; }; Util.hasStroke = function(doc, layer) { var desc1, desc2, hasFX, hasStroke, ref, res; doc.activeLayer = layer; res = false; ref = new ActionReference(); ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); hasFX = executeActionGet(ref).hasKey(stringIDToTypeID('layerEffects')); if (hasFX) { hasStroke = executeActionGet(ref).getObjectValue(stringIDToTypeID('layerEffects')).hasKey(stringIDToTypeID('frameFX')); if (hasStroke) { desc1 = executeActionGet(ref); desc2 = executeActionGet(ref).getObjectValue(stringIDToTypeID('layerEffects')).getObjectValue(stringIDToTypeID('frameFX')); if (desc1.getBoolean(stringIDToTypeID('layerFXVisible')) && desc2.getBoolean(stringIDToTypeID('enabled'))) { res = true; } } } return res; }; Util.getStrokeSize = function(doc, layer) { var desc, ref; doc.activeLayer = layer; ref = new ActionReference(); ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('layerEffects')).getObjectValue(stringIDToTypeID('frameFX')); return desc.getUnitDoubleValue(stringIDToTypeID('size')); }; Util.getStrokeColor = function(doc, layer) { var desc, ref; doc.activeLayer = layer; ref = new ActionReference(); ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('layerEffects')).getObjectValue(stringIDToTypeID('frameFX')); return Util.getColorFromDescriptor(desc.getObjectValue(stringIDToTypeID("color")), typeIDToCharID(desc.getClass(stringIDToTypeID("color")))); }; Util.getColorFromDescriptor = function(colorDesc, keyClass) { var colorObject; colorObject = new SolidColor(); if (keyClass === "Grsc") { colorObject.grey.grey = color.getDouble(charIDToTypeID('Gry ')); } if (keyClass === "RGBC") { colorObject.rgb.red = colorDesc.getDouble(charIDToTypeID('Rd ')); colorObject.rgb.green = colorDesc.getDouble(charIDToTypeID('Grn ')); colorObject.rgb.blue = colorDesc.getDouble(charIDToTypeID('Bl ')); } if (keyClass === "CMYC") { colorObject.cmyk.cyan = colorDesc.getDouble(charIDToTypeID('Cyn ')); colorObject.cmyk.magenta = colorDesc.getDouble(charIDToTypeID('Mgnt')); colorObject.cmyk.yellow = colorDesc.getDouble(charIDToTypeID('Ylw ')); colorObject.cmyk.black = colorDesc.getDouble(charIDToTypeID('Blck')); } if (keyClass === "LbCl") { colorObject.lab.l = colorDesc.getDouble(charIDToTypeID('Lmnc')); colorObject.lab.a = colorDesc.getDouble(charIDToTypeID('A ')); colorObject.lab.b = colorDesc.getDouble(charIDToTypeID('B ')); } return colorObject; }; Util.deselectLayers = function() { var desc01, ref01; desc01 = new ActionDescriptor(); ref01 = new ActionReference(); ref01.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt')); desc01.putReference(charIDToTypeID('null'), ref01); return executeAction(stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO); }; Util.selectTransparency = function() { var actionDesc, actionSelect, actionTransparent, idChnl; idChnl = charIDToTypeID("Chnl"); actionSelect = new ActionReference(); actionSelect.putProperty(idChnl, charIDToTypeID("fsel")); actionTransparent = new ActionReference(); actionTransparent.putEnumerated(idChnl, idChnl, charIDToTypeID("Trsp")); actionDesc = new ActionDescriptor(); actionDesc.putReference(charIDToTypeID("null"), actionSelect); actionDesc.putReference(charIDToTypeID("T "), actionTransparent); return executeAction(charIDToTypeID("setd"), actionDesc, DialogModes.NO); }; Util.getTextExtents = function(text_item) { var bounds, desc, height, ref, transform, width, x_scale, y_scale; app.activeDocument.activeLayer = text_item.parent; ref = new ActionReference(); ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey')); bounds = desc.getObjectValue(stringIDToTypeID('bounds')); width = bounds.getUnitDoubleValue(stringIDToTypeID('right')); height = bounds.getUnitDoubleValue(stringIDToTypeID('bottom')); x_scale = 1; y_scale = 1; if (desc.hasKey(stringIDToTypeID('transform'))) { transform = desc.getObjectValue(stringIDToTypeID('transform')); x_scale = transform.getUnitDoubleValue(stringIDToTypeID('xx')); y_scale = transform.getUnitDoubleValue(stringIDToTypeID('yy')); } return { x: Math.round(text_item.position[0]), y: Math.round(text_item.position[1]), width: Math.round(width * x_scale), height: Math.round(height * y_scale) }; }; Util.getTextYScale = function(text_item) { var bounds, desc, height, ref, transform, width, x_scale, y_scale; app.activeDocument.activeLayer = text_item.parent; ref = new ActionReference(); ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey')); bounds = desc.getObjectValue(stringIDToTypeID('bounds')); width = bounds.getUnitDoubleValue(stringIDToTypeID('right')); height = bounds.getUnitDoubleValue(stringIDToTypeID('bottom')); x_scale = 1; y_scale = 1; if (desc.hasKey(stringIDToTypeID('transform'))) { transform = desc.getObjectValue(stringIDToTypeID('transform')); x_scale = transform.getUnitDoubleValue(stringIDToTypeID('xx')); y_scale = transform.getUnitDoubleValue(stringIDToTypeID('yy')); } return y_scale; }; Util.rasterizeLayerStyle = function(layer) { var desc5, idLyr, idOrdn, idTrgt, idWhat, idlayerStyle, idnull, idrasterizeItem, idrasterizeLayer, ref4; app.activeDocument.activeLayer = layer; idrasterizeLayer = stringIDToTypeID("rasterizeLayer"); desc5 = new ActionDescriptor(); idnull = charIDToTypeID("null"); ref4 = new ActionReference(); idLyr = charIDToTypeID("Lyr "); idOrdn = charIDToTypeID("Ordn"); idTrgt = charIDToTypeID("Trgt"); ref4.putEnumerated(idLyr, idOrdn, idTrgt); desc5.putReference(idnull, ref4); idWhat = charIDToTypeID("What"); idrasterizeItem = stringIDToTypeID("rasterizeItem"); idlayerStyle = stringIDToTypeID("layerStyle"); desc5.putEnumerated(idWhat, idrasterizeItem, idlayerStyle); return executeAction(idrasterizeLayer, desc5, DialogModes.NO); }; Util.rasterizeLayerMask = function(layer) { app.activeDocument.activeLayer = layer; if (Util.hasVectorMask()) { Util.rasterizeLayer(); Util.selectVectorMask(); Util.rasterizeVectorMask(); Util.applyLayerMask(); } if (Util.hasLayerMask()) { Util.rasterizeLayer(); Util.selectLayerMask(); return Util.applyLayerMask(); } }; Util.hasVectorMask = function() { var desc, e, hasVectorMask, keyKind, keyVectorMaskEnabled, kindValue, ref; hasVectorMask = false; try { ref = new ActionReference(); keyVectorMaskEnabled = app.stringIDToTypeID('vectorMask'); keyKind = app.charIDToTypeID('Knd '); ref.putEnumerated(app.charIDToTypeID('Path'), app.charIDToTypeID('Ordn'), keyVectorMaskEnabled); desc = executeActionGet(ref); if (desc.hasKey(keyKind)) { kindValue = desc.getEnumerationValue(keyKind); if (kindValue === keyVectorMaskEnabled) { hasVectorMask = true; } } } catch (error) { e = error; hasVectorMask = false; } return hasVectorMask; }; Util.hasLayerMask = function() { var desc, e, hasLayerMask, keyUserMaskEnabled, ref; hasLayerMask = false; try { ref = new ActionReference(); keyUserMaskEnabled = app.charIDToTypeID('UsrM'); ref.putProperty(app.charIDToTypeID('Prpr'), keyUserMaskEnabled); ref.putEnumerated(app.charIDToTypeID('Lyr '), app.charIDToTypeID('Ordn'), app.charIDToTypeID('Trgt')); desc = executeActionGet(ref); if (desc.hasKey(keyUserMaskEnabled)) { hasLayerMask = true; } } catch (error) { e = error; hasLayerMask = false; } return hasLayerMask; }; Util.rasterizeLayer = function() { var desc245, id1242, id1243, id1244, id1245, id1246, ref184; try { id1242 = stringIDToTypeID("rasterizeLayer"); desc245 = new ActionDescriptor(); id1243 = charIDToTypeID("null"); ref184 = new ActionReference(); id1244 = charIDToTypeID("Lyr "); id1245 = charIDToTypeID("Ordn"); id1246 = charIDToTypeID("Trgt"); ref184.putEnumerated(id1244, id1245, id1246); desc245.putReference(id1243, ref184); return executeAction(id1242, desc245, DialogModes.NO); } catch (error) { } }; Util.selectVectorMask = function() { var desc15, e, id55, id56, id57, id58, id59, id60, id61, id62, ref13; try { id55 = charIDToTypeID("slct"); desc15 = new ActionDescriptor(); id56 = charIDToTypeID("null"); ref13 = new ActionReference(); id57 = charIDToTypeID("Path"); id58 = charIDToTypeID("Path"); id59 = stringIDToTypeID("vectorMask"); ref13.putEnumerated(id57, id58, id59); id60 = charIDToTypeID("Lyr "); id61 = charIDToTypeID("Ordn"); id62 = charIDToTypeID("Trgt"); ref13.putEnumerated(id60, id61, id62); desc15.putReference(id56, ref13); return executeAction(id55, desc15, DialogModes.NO); } catch (error) { e = error; } }; Util.selectLayerMask = function() { var desc153, e, id759, id760, id761, id762, id763, id764, ref92; try { id759 = charIDToTypeID("slct"); desc153 = new ActionDescriptor(); id760 = charIDToTypeID("null"); ref92 = new ActionReference(); id761 = charIDToTypeID("Chnl"); id762 = charIDToTypeID("Chnl"); id763 = charIDToTypeID("Msk "); ref92.putEnumerated(id761, id762, id763); desc153.putReference(id760, ref92); id764 = charIDToTypeID("MkVs"); desc153.putBoolean(id764, false); return executeAction(id759, desc153, DialogModes.NO); } catch (error) { e = error; } }; Util.rasterizeVectorMask = function() { var desc44, e, id488, id489, id490, id491, id492, id493, id494, id495, ref29; try { id488 = stringIDToTypeID("rasterizeLayer"); desc44 = new ActionDescriptor(); id489 = charIDToTypeID("null"); ref29 = new ActionReference(); id490 = charIDToTypeID("Lyr "); id491 = charIDToTypeID("Ordn"); id492 = charIDToTypeID("Trgt"); ref29.putEnumerated(id490, id491, id492); desc44.putReference(id489, ref29); id493 = charIDToTypeID("What"); id494 = stringIDToTypeID("rasterizeItem"); id495 = stringIDToTypeID("vectorMask"); desc44.putEnumerated(id493, id494, id495); return executeAction(id488, desc44, DialogModes.NO); } catch (error) { e = error; } }; Util.applyLayerMask = function() { var desc154, e, id765, id766, id767, id768, id769, id770, ref93; try { id765 = charIDToTypeID("Dlt "); desc154 = new ActionDescriptor(); id766 = charIDToTypeID("null"); ref93 = new ActionReference(); id767 = charIDToTypeID("Chnl"); id768 = charIDToTypeID("Ordn"); id769 = charIDToTypeID("Trgt"); ref93.putEnumerated(id767, id768, id769); desc154.putReference(id766, ref93); id770 = charIDToTypeID("Aply"); desc154.putBoolean(id770, true); return executeAction(id765, desc154, DialogModes.NO); } catch (error) { e = error; } }; Util.mergeGroup = function(layer) { var desc15, e, idMrgtwo; app.activeDocument.activeLayer = layer; try { idMrgtwo = charIDToTypeID("Mrg2"); desc15 = new ActionDescriptor(); return executeAction(idMrgtwo, desc15, DialogModes.NO); } catch (error) { e = error; } }; Util.parseOption = function(text) { var elements, j, len, opt, optText, ref1; if (!text) { return {}; } opt = {}; ref1 = text.split(","); for (j = 0, len = ref1.length; j < len; j++) { optText = ref1[j]; elements = optText.split("="); if (elements.length === 1) { elements[1] = 'true'; } opt[elements[0].toLowerCase()] = elements[1].toLowerCase(); } return opt; }; return Util; })(); String.prototype.startsWith = function(str) { return this.slice(0, str.length) === str; }; String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; String.prototype.strip = function() { if (String.prototype.trim != null) { return this.trim(); } else { return this.replace(/^\s+|\s+$/g, ""); } }; setup = function() { preferences.rulerUnits = Units.PIXELS; return preferences.typeUnits = TypeUnits.PIXELS; }; setup(); baum = new Baum(); baum.run(); }).call(this);