Created
May 16, 2015 10:14
-
-
Save naoyashiga/118efc572122eb51b462 to your computer and use it in GitHub Desktop.
Convert Photoshop Textlayer text to json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
"use strict"; | |
var PLUGIN_ID = require("./package.json").name, | |
MENU_ID = "tutorial", | |
MENU_LABEL = "$$$/JavaScripts/Generator/Tutorial/Menu=Tutorial"; | |
var _generator = null, | |
_currentDocumentId = null, | |
_config = null; | |
function init(generator){ | |
_generator = generator; | |
console.log("ohayo"); | |
_generator.addMenuItem(MENU_ID,MENU_LABEL,true,false).then( | |
function(){ | |
console.log("Menu created",MENU_ID); | |
},function(){ | |
console.error("Menu creation failed",MENU_ID); | |
} | |
); | |
_generator.onPhotoshopEvent("generatorMenuChanged",handleGeneratorMenuClicked); | |
function initLater() { | |
_generator.onPhotoshopEvent("currentDocumentChanged", handleCurrentDocumentChanged); | |
_generator.onPhotoshopEvent("imageChanged", handleImageChanged); | |
_generator.onPhotoshopEvent("toolChanged", handleToolChanged); | |
requestEntireDocument(); | |
} | |
process.nextTick(initLater); | |
} | |
/*********** EVENTS ***********/ | |
function handleCurrentDocumentChanged(id) { | |
console.log("handleCurrentDocumentChanged: "+id) | |
setCurrentDocumentId(id); | |
} | |
function handleImageChanged(document) { | |
console.log("Image " + document.id + " was changed:");//, stringify(document)); | |
console.log("Received document:", stringify(document)); | |
} | |
function handleToolChanged(document){ | |
console.log("Tool changed " + document.id + " was changed:");//, stringify(document)); | |
console.log("Received tool document:", stringify(document)); | |
} | |
function handleGeneratorMenuClicked(event) { | |
// Ignore changes to other menus | |
var menu = event.generatorMenuChanged; | |
if (!menu || menu.name !== MENU_ID) { | |
return; | |
} | |
var startingMenuState = _generator.getMenuState(menu.name); | |
console.log("Menu event %s, starting state %s", stringify(event), stringify(startingMenuState)); | |
} | |
/*********** CALLS ***********/ | |
function requestEntireDocument(documentId) { | |
if (!documentId) { | |
console.log("Determining the current document ID"); | |
} | |
_generator.getDocumentInfo(documentId).then( | |
function (document) { | |
// console.log("Received complete document:", stringify(document)); | |
jsonParse(stringify(document)); | |
}, | |
function (err) { | |
console.error("[Tutorial] Error in getDocumentInfo:", err); | |
} | |
).done(); | |
} | |
function updateMenuState(enabled) { | |
console.log("Setting menu state to", enabled); | |
_generator.toggleMenu(MENU_ID, true, enabled); | |
} | |
/*********** HELPERS ***********/ | |
function jsonParse(jsonString){ | |
var obj = JSON.parse(jsonString); | |
var layers = obj.layers; | |
var textLayers = {}; | |
var textLayerData = {}; | |
var fs = require("fs"); | |
// console.log("obj.layers",obj.layers); | |
for(var i = 0;i < layers.length;i++){ | |
var layer = layers[i]; | |
//find textLayer | |
if(layer.type == "textLayer"){ | |
//set textLayer name and textKey | |
var textObj = {}; | |
textObj["text"] = layer.text.textKey; | |
textLayers[layer.name] = textObj; | |
} | |
} | |
textLayerData["textLayerData"] = textLayers; | |
fs.writeFile("./test/plugins/naoyaTest/config.json", JSON.stringify(textLayerData, null, ' ')); | |
} | |
function sendJavascript(str){ | |
_generator.evaluateJSXString(str).then( | |
function(result){ | |
console.log(result); | |
}, | |
function(err){ | |
console.log(err); | |
}); | |
} | |
function setCurrentDocumentId(id) { | |
if (_currentDocumentId === id) { | |
return; | |
} | |
console.log("Current document ID:", id); | |
_currentDocumentId = id; | |
} | |
function stringify(object) { | |
try { | |
return JSON.stringify(object, null, " "); | |
} catch (e) { | |
console.error(e); | |
} | |
return String(object); | |
} | |
exports.init = init; | |
// Unit test function exports | |
exports._setConfig = function (config) { _config = config; }; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment