/ Library / Application support / com.bohemiancoding.sketch3 / Plugins
{ "name" : "My Plugin", "identifier" : "my.plugin", "version" : "1.0", "description" : "My First Sketch Plugin", "authorEmail" : "your@email.com", "author" : "Your Name", "commands" : [ { "script" : "MyScript.js", "handler" : "onRun", "shortcut" : "command shift y", "name" : "Get Page Names", "identifier" : "my.plugin.pagenames" } ] }
var onRun = function(context) { //reference the Sketch Document var doc = context.document; //reference all the pages in the document in an array var pages = [doc pages]; //loop through the pages of the document for (var i = 0; i < pages.count(); i++){ //reference each page var page = pages[i]; //get the name of the page var pageName = [page name]; //show the page name in the console log(pageName); } }
10:54:42 PM Get Page Names (Sketch Plugin): Page 1
var onRun = function(context) { //reference the Sketch Document var doc = context.document; //reference all the pages in the document in an array var pages = [doc pages]; //loop through the pages of the document for (var i = 0; i < pages.count(); i++){ //reference each page var page = pages[i]; //get the name of the page var pageName = [page name]; //show the page name in the console log(pageName); } }
log(pageName);
doc.showMessage(“MyPlugin Finished!”);
var onRun = function(context) { //reference the Sketch Document var doc = context.document; //reference all the pages in the document in an array var pages = [doc pages]; //loop through the pages of the document for (var i = 0; i < pages.count(); i++){ //reference each page var page = pages[i]; //get the name of the page var pageName = [page name]; //show the page name in the console log(pageName); } //show a message at the bottom of Sketch doc.showMessage("MyPlugin Finished!"); }
var app = [NSApplication sharedApplication];
[app displayDialog:"This is an alert box!" withTitle:"Alert Box Title"];
var onRun = function(context) { //reference the Application var app = [NSApplication sharedApplication]; //reference the Sketch Document var doc = context.document; //reference all the pages in the document in an array var pages = [doc pages]; //loop through the pages of the document for (var i = 0; i < pages.count(); i++){ //reference each page var page = pages[i]; //get the name of the page var pageName = [page name]; //show the page name in the console log(pageName); } //show a message at the bottom of Sketch doc.showMessage("MyPlugin Finished!"); //send an alert message to the application [app displayDialog:"This is an alert box!" withTitle:"Alert Box Title"]; }
[app displayDialog:”This document has “ + pages.count() + “ pages.” withTitle:”Alert Box Title”];
var myName = “Mike”; var thisYear = 2016; var givesHighFives = true;
var daysOfTheWeek = {“Sunday”, “Monday”, “Tuesday”, Wednesday”, “Thursday”, “Friday”, “Saturday”};
var daysOfTheWeek[]; daysOfTheWeek[0] = “Sunday”; daysOfTheWeek[1] = “Monday”; daysOfTheWeek[2] = “Tuesday”; daysOfTheWeek[3] = “Wednesday”; daysOfTheWeek[4] = “Thursday”; daysOfTheWeek[5] = “Friday”; daysOfTheWeek[6] = “Saturday”;
var eyesOpen = true; function closeEyes(){ eyesOpen = false; } function openEyes(){ eyesOpen = true; }
if(daysOfTheWeek == “Sunday”){ watchGameOfThrones(); }else if(daysOfTheWeek == “Wednesday”){ watchMrRobot(); }else{ watchTheNews(); }
log(daysOfTheWeek[0]); log(daysOfTheWeek[1]); log(daysOfTheWeek[2]); log(daysOfTheWeek[3]); log(daysOfTheWeek[4]); log(daysOfTheWeek[5]); log(daysOfTheWeek[6]);
for(var i = 0; i < daysOfTheWeek.count(); i++){ log(daysOfTheWeek[i]; }
[app displayDialog:”This is an alert box!” withTitle:”Alert Box Title”];
function alert(title, message){ var app = [NSApplication sharedApplication]; [app displayDialog:message withTitle:title]; }
var onRun = function(context) { //reference the Application var app = [NSApplication sharedApplication]; //reference the Sketch Document var doc = context.document; //reference all the pages in the document in an array var pages = [doc pages]; [app displayDialog:"This document has " + pages.count() + " pages." withTitle:"Alert Box Title"]; //loop through the pages of the document for (var i = 0; i < pages.count(); i++){ //reference each page var page = pages[i]; //get the name of the page var pageName = [page name]; //show the page name in the console log(pageName); } //show a message in app doc.showMessage("MyPlugin Finished!"); //send an alert message to the application [app displayDialog:"This is an alert box!" withTitle:"Alert Box Title"]; } function alert(title, message){ var app = [NSApplication sharedApplication]; [app displayDialog:message withTitle:title]; }
var onRun = function(context) { //reference the Application var app = [NSApplication sharedApplication]; //reference the Sketch Document var doc = context.document; //reference all the pages in the document in an array var pages = [doc pages]; alert("Number of Pages", "This document has " + pages.count() + " pages."); //loop through the pages of the document for (var i = 0; i < pages.count(); i++){ //reference each page var page = pages[i]; //get the name of the page var pageName = [page name]; //show the page name in the console log(pageName); } //show a message in app doc.showMessage("MyPlugin Finished!"); //send an alert message to the application alert("Plugin Finished!", "This is a message saying the Plugin is finished.") } function alert(title, message){ var app = [NSApplication sharedApplication]; [app displayDialog:message withTitle:title]; }
function alert(title, message){ var app = [NSApplication sharedApplication]; [app displayDialog:message withTitle:title]; }
@import 'common.js'
@import 'common.js' var onRun = function(context) { //reference the Application var app = [NSApplication sharedApplication]; //reference the Sketch Document var doc = context.document; //reference all the pages in the document in an array var pages = [doc pages]; alert("Number of Pages", "This document has " + pages.count() + " pages."); //loop through the pages of the document for (var i = 0; i < pages.count(); i++){ //reference each page var page = pages[i]; //get the name of the page var pageName = [page name]; //show the page name in the console log(pageName); } //show a message in app doc.showMessage("MyPlugin Finished!"); //send an alert message to the application alert("Plugin Finished!", "This is a message saying the Plugin is finished.") }
@import 'common.js' var onRun = function(context) { //reference the Sketch Document var doc = context.document; }
{ "name" : "My Plugin", "identifier" : "my.plugin", "version" : "1.0", "description" : "My First Sketch Plugin", "authorEmail" : "your@email.com", "author" : "Your Name", "commands" : [ { "script" : "MyScript.js", "handler" : "onRun", "shortcut" : "command shift y", "name" : "Get Page Names", "identifier" : "my.plugin.pagenames" }, { "script" : "RotateArtboard.js", "handler" : "onRun", "shortcut" : "command shift u", "name" : "Rotate Artboard", "identifier" : "my.plugin.rotateartboard" } ], }
var selection = context.selection;
if(selection.count() == 0){ doc.showMessage(“Please select something.”); }
if(selection.count() == 0){ doc.showMessage(“Please select something.”); } else { for(var i = 0; i < selection.count(); i++){ if(selection[i].class() == "MSArtboardGroup"){ //do something } } }
var artboard = selection[i]; var artboardFrame = artboard.frame(); var artboardWidth = artboardFrame.width(); var artboardHeight = artboardFrame.height();
var newArtboardWidth = artboardHeight; var newArtboardHeight = artboardWidth;
artboardFrame.setWidth(newArtboardWidth); artboardFrame.setHeight(newArtboardHeight);
var alertMessage = “New Height: “+newArtboardHeight+ “ | New Width: “+newArtboardWidth; alert(“Artboard Rotated!”, alertMessage)
@import 'common.js' var onRun = function(context) { //reference the sketch document var doc = context.document; //reference what is selected var selection = context.selection; //make sure something is selected if(selection.count() == 0){ doc.showMessage("Please select something."); }else{ //loop through the selected layers for(var i = 0; i < selection.count(); i++){ //checks to see if the layer is an artboard if(selection[i].class() == "MSArtboardGroup"){ //reference the selection var artboard = selection[i]; //get the artboard frame for dimensions var artboardFrame = artboard.frame(); //get the width var artboardWidth = artboardFrame.width(); //get the height var artboardHeight = artboardFrame.height(); //set a new width variable to the old height var newArtboardWidth = artboardHeight; //set a new height variable to the old width var newArtboardHeight = artboardWidth; //set the artboard frame with the new dimensions artboardFrame.setWidth(newArtboardWidth); artboardFrame.setHeight(newArtboardHeight); //send an alert message with the new values var alertMessage = "New Height: "+newArtboardHeight+ " | New Width: "+newArtboardWidth; alert("Artboard Rotated!", alertMessage); }else{ doc.showMessage("Please select an artboard."); } } } }
@import 'common.js' var onRun = function(context) { //reference the sketch document var doc = context.document; //reference what is selected var selection = context.selection; //make sure something is selected if(selection.count() == 0){ doc.showMessage("Please select something."); }else{ //loop through the selected layers for(var i = 0; i < selection.count(); i++){ //checks to see if the layer is a Symbol if(selection[i].class() == "MSSymbolInstance"){ //reference the selection var layer = selection[i]; //get the original layer name var layerName = layer.name(); //get the name of the symbol on the layer var symbolName = layer.symbolMaster().name(); //check if layer name is not already symbol name if(layerName != symbolName){ //set the layer name to the symbol name layer.setName(symbolName); var alertMessage = "Layer Name Changed from: "+layerName+ " to: "+symbolName; alert("Layer Name Changed!", alertMessage); }else{ doc.showMessage("Layer name is already Symbol Name."); } }else{ doc.showMessage("Please select a Symbol Layer."); } } } }
var pages = [doc pages];
var pages = [doc pages]; for (var i = 0; i < pages.count(); i++){ var page = pages[i]; var artboards = [page artboards]; }
var pages = [doc pages]; for (var i = 0; i < pages.count(); i++){ var page = pages[i]; var artboards = [page artboards]; for (var z = 0; z < artboards.count(); z++){ var artboard = artboards[z]; var layers = [artboard layers]; } }
var pages = [doc pages]; for (var i = 0; i < pages.count(); i++){ var page = pages[i]; var artboards = [page artboards]; for (var z = 0; z < artboards.count(); z++){ var artboard = artboards[z]; var layers = [artboard layers]; for(var k = 0; k < layers.count(); k++){ var layer = layers[k]; if(layer.class() == "MSSymbolInstance"){ //do something } } } }
@import 'common.js' var onRun = function(context) { //reference the sketch document var doc = context.document; //reference the pages array in the document var pages = [doc pages]; //create a variable to hold how many symbol layers we changed var symbolCount = 0; //loop through the pages array for (var i = 0; i < pages.count(); i++){ //reference each page var page = pages[i]; //reference the artboards array of each page var artboards = [page artboards]; //loop through the artboards of each page for (var z = 0; z < artboards.count(); z++){ //reference each artboard of each page var artboard = artboards[z]; //reference the layers array of each artboard var layers = [artboard layers]; //loop through the layers array for(var k = 0; k < layers.count(); k++){ //reference each layer of each artboard var layer = layers[k]; //check to see if the layer is a Symbol if(layer.class() == "MSSymbolInstance"){ //get the original layer name var layerName = layer.name(); //get the name of the symbol on the layer var symbolName = layer.symbolMaster().name(); //only change the name of layers that don't match the symbol name if(layerName != symbolName){ //set the layer name to the symbol name layer.setName(symbolName); symbolCount = symbolCount + 1; } } } } } var alertMessage = symbolCount + " symbol layer name changed."; alert("Symbol Layer Names Reset!", alertMessage); }
var onRun = function(context) { var doc = context.document; var selection = context.selection; if(selection.count() == 0){ doc.showMessage("Please select something."); }else{ for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; } } };
var layerClass = layer.class();
var layerFrame = layer.frame(); var layerWidth = layerFrame.width(); var layerHeight = layerFrame.height(); var layerXpos = layerFrame.x(); var layerYpos = layerFrame.y();
var layerStyle = layer.style(); // var layerFills = layer.style().fills(); // for(var z = 0; z < layerFills.count(); z++){ var fillColor = layerFills[z].colorGeneric(); }
var layerName = layer.name();
var layerIsVisible = layer.isVisible();
var layerIsLocked = layer.isLocked();
// var layerIsFlippedHorizontal = layer.isFlippedHorizontal(); // var layerIsFlippedVertical = layer.isFlippedVertical();
var layerRotation = layer.rotation();
var layerParent = layer.parentGroup();
var layerIsSelected = layer.isSelected();
var layerAbsoluteRect = layer.absoluteRect(); var layerAbsoluteWidth = layerAbsoluteRect.width(); var layerAbsoluteHeight = layerAbsoluteRect.height(); var layerAbsoluteXpos = layerAbsoluteRect.x(); var layerAbsoluteYpos = layerAbsoluteRect.y();
var layerCSSAttributeString = layer.CSSAttributeString();
var layerCSSAttributes = layer.CSSAttributes();
var onRun = function(context) { var doc = context.document; var selection = context.selection; if(selection.count() == 0){ doc.showMessage("Please select something."); }else{ for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; var layerClass = layer.class(); var layerFrame = layer.frame(); var layerStyle = layer.style(); var layerName = layer.name(); var layerIsVisible = layer.isVisible(); var layerIsLocked = layer.isLocked(); var layerIsFlippedHorizontal = layer.isFlippedHorizontal(); var layerIsVertical = layer.isFlippedVertical(); var layerRotation = layer.rotation(); var layerParent = layer.parentGroup(); var layerIsSelected = layer.isSelected(); var layerAbsoluteRect = layer.absoluteRect(); var layerUserInfo = layer.userInfo(); var layerCSSAttributeString = layer.CSSAttributeString(); var layerCSSAttributes = layer.CSSAttributes(); } } };
log(“Layer Rotation: “ + layerRotation);
var newLayerName = "New Layer Name"; layer.setName(newLayerName);
// layer.setIsVisible(true); // layer.setIsVisible(false); // layer.setIsVisible(!layer.isVisible())
// layer.setIsLocked(true); // layer.setIsLocked(false); // layer.setIsLocked(!layer.isLocked());
var newLayerRotation = 180; layer.setRotation(newLayerRotation);
// layer.setIsFlippedHorizontal(true); // layer.setIsFlippedHorizontal(false); // layer.setIsFlippedHorizontal(!layer.isFlippedHorizontal());
// layer.setIsFlippedVertical(true); // layer.setIsFlippedVertical(false); // layer.setIsFlippedVertical(!layer.isFlippedVertical());
// layer.setIsSelected(true); // layer.setIsSelected(false); // layer.setIsSelected(!layer.isSelected());
layer.duplicate();
var onRun = function(context) { var doc = context.document; var selection = context.selection; if(selection.count() == 0){ doc.showMessage("Please select something."); }else{ for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; var layerIsLocked = layer.isLocked(); if(layerIsLocked == true){ layer.setIsLocked(false); } } } };
var onRun = function(context) { var doc = context.document; var selection = context.selection; if(selection.count() == 0){ doc.showMessage("Please select something."); }else{ for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; var layerName = layer.name(); var layerPrefix = "prefix_"; layer.setName(layerPrefix + layerName); } } }
var onRun = function(context) { var doc = context.document; var selection = context.selection; if(selection.count() == 0){ doc.showMessage("Please select something."); }else{ for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; var layerFrame = layer.frame(); var newLayerWidth = 100; layerFrame.setWidth(newLayerWidth); } } }
var onRun = function(context) { var doc = context.document; var selection = context.selection; if(selection.count() == 0){ doc.showMessage("Please select something."); }else{ for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; var layerStyle = layer.style(); layerStyle.addStylePartOfType(0); } } }
8/28/16 9:07:18.993 AM Show Layer Type (Sketch Plugin)[46515]: Background is a: MSShapeGroup 8/28/16 9:07:18.997 AM Show Layer Type (Sketch Plugin)[46515]: Line is a: MSShapeGroup 8/28/16 9:07:18.999 AM Show Layer Type (Sketch Plugin)[46515]: Author is a: MSTextLayer 8/28/16 9:07:18.999 AM Show Layer Type (Sketch Plugin)[46515]: Icon is a: MSBitmapLayer 8/28/16 9:07:19.000 AM Show Layer Type (Sketch Plugin)[46515]: Oval is a: MSSymbolInstance 8/28/16 9:07:19.001 AM Show Layer Type (Sketch Plugin)[46515]: Title is a: MSTextLayer 8/28/16 9:07:19.002 AM Show Layer Type (Sketch Plugin)[46515]: Line is a: MSShapeGroup
8/28/16 9:10:08.600 AM Show Layer Type (Sketch Plugin)[46515]: Oval is a: MSSymbolMaster
8/28/16 9:10:48.226 AM Show Layer Type (Sketch Plugin)[46515]: Artboard 1 is a: MSArtboardGroup
8/28/16 9:11:24.234 AM Show Layer Type (Sketch Plugin)[46515]: Group is a: MSLayerGroup
MSShapeGroup
MSBitmapLayer
MSTextLayer
MSSymbolInstance
MSSymbolMaster
MSArtboardGroup
MSLayerGroup
var onRun = function(context) { var doc = context.document; var selection = context.selection; if(selection.count() == 0){ doc.showMessage("Please select something."); }else{ for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; var layerClass = layer.class(); if(layerClass == "MSShapeGroup"){ //do something } else if (layerClass == "MSBitmapLayer"){ //do something } else if (layerClass == "MSTextLayer"){ //do something } else if (layerClass == "MSSymbolInstance"){ //do something } else if (layerClass == "MSSymbolMaster"){ //do something } else if (layerClass == "MSArtboardGroup"){ //do something } else if (layerClass == "MSLayerGroup"){ //do something } } } };
@import 'common.js' var onRun = function(context) { var doc = context.document; var selection = context.selection; //make sure something is selected if(selection.count() == 0){ doc.showMessage("Please select a layer."); }else{ //do something } };
//allow xml to be written to the folder var fileTypes = [NSArray arrayWithObjects:@"xml", nil]; //create select folder window var panel = [NSOpenPanel openPanel]; [panel setCanChooseDirectories:true]; [panel setCanCreateDirectories:true]; [panel setAllowedFileTypes:fileTypes];
//create variable to check if clicked var clicked = [panel runModal]; //check if clicked if (clicked == NSFileHandlingPanelOKButton) { var isDirectory = true; //get the folder path var firstURL = [[panel URLs] objectAtIndex:0]; //format it to a string var file_path = [NSString stringWithFormat:@"%@", firstURL]; //remove the file:// path from string if (0 === file_path.indexOf("file://")) { file_path = file_path.substring(7); } }
@import 'common.js' var onRun = function(context) { var doc = context.document; var selection = context.selection; //make sure something is selected if(selection.count() == 0){ doc.showMessage("Please select a layer."); }else{ //allow xml to be written to the folder var fileTypes = [NSArray arrayWithObjects:@"xml", nil]; //create select folder window var panel = [NSOpenPanel openPanel]; [panel setCanChooseDirectories:true]; [panel setCanCreateDirectories:true]; [panel setAllowedFileTypes:fileTypes]; //create variable to check if clicked var clicked = [panel runModal]; //check if clicked if (clicked == NSFileHandlingPanelOKButton) { var isDirectory = true; //get the folder path var firstURL = [[panel URLs] objectAtIndex:0]; //format it to a string var file_path = [NSString stringWithFormat:@"%@", firstURL]; //remove the file:// path from string if (0 === file_path.indexOf("file://")) { file_path = file_path.substring(7); } } //loop through the selected layers and export the XML for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; exportXML(layer, file_path); } } };
//initialize the root xml element var root = [NSXMLElement elementWithName:@"document"]; //initialize the xml object with the root element var xmlObj = [[NSXMLDocument document] initWithRootElement:root];
//create the variables var layerName = layer.name(); var layerFrame = layer.absoluteRect(); var layerXpos = String(layerFrame.x()); var layerYpos = String(layerFrame.y()); var layerHeight = String(layerFrame.height()); var layerWidth = String(layerFrame.width());
//create the first child element and add it to the root var layerElement = [NSXMLElement elementWithName:@"layer"]; [root addChild:layerElement]; //add elements based on variables to the first child var layerNameElement = [NSXMLElement elementWithName:@"name" stringValue:layerName]; [layerElement addChild:layerNameElement]; var layerXPosElement = [NSXMLElement elementWithName:@"xPos" stringValue:layerXpos]; [layerElement addChild:layerXPosElement]; var layerYPosElement = [NSXMLElement elementWithName:@"yPox" stringValue:layerYpos]; [layerElement addChild:layerYPosElement]; var layerHeightElement = [NSXMLElement elementWithName:@"height" stringValue:layerHeight]; [layerElement addChild:layerHeightElement]; var layerWidthElement = [NSXMLElement elementWithName:@"width" stringValue:layerWidth]; [layerElement addChild:layerWidthElement];
//create the xml file var xmlData = [xmlObj XMLDataWithOptions:NSXMLNodePrettyPrint]; //name the xml file the name of the layer and save it to the folder [xmlData writeToFile:file_path+layerName+".xml"]; var alertMessage = layerName+".xml saved to: " + file_path; alert("Layer XML Exported!", alertMessage);
@import 'common.js' var onRun = function(context) { var doc = context.document; var selection = context.selection; //make sure something is selected if(selection.count() == 0){ doc.showMessage("Please select a layer."); }else{ //allow xml to be written to the folder var fileTypes = [NSArray arrayWithObjects:@"xml", nil]; //create select folder window var panel = [NSOpenPanel openPanel]; [panel setCanChooseDirectories:true]; [panel setCanCreateDirectories:true]; [panel setAllowedFileTypes:fileTypes]; //create variable to check if clicked var clicked = [panel runModal]; //check if clicked if (clicked == NSFileHandlingPanelOKButton) { var isDirectory = true; //get the folder path var firstURL = [[panel URLs] objectAtIndex:0]; //format it to a string var file_path = [NSString stringWithFormat:@"%@", firstURL]; //remove the file:// path from string if (0 === file_path.indexOf("file://")) { file_path = file_path.substring(7); } } //loop through the selected layers and export the XML for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; exportXML(layer, file_path); } } }; function exportXML(layer, file_path){ //initialize the root xml element var root = [NSXMLElement elementWithName:@"document"]; //initialize the xml object with the root element var xmlObj = [[NSXMLDocument document] initWithRootElement:root]; //create the variables var layerName = layer.name(); var layerFrame = layer.absoluteRect(); var layerXpos = String(layerFrame.x()); var layerYpos = String(layerFrame.y()); var layerHeight = String(layerFrame.height()); var layerWidth = String(layerFrame.width()); //create the first child element and add it to the root var layerElement = [NSXMLElement elementWithName:@"layer"]; [root addChild:layerElement]; //add elements based on variables to the first child var layerNameElement = [NSXMLElement elementWithName:@"name" stringValue:layerName]; [layerElement addChild:layerNameElement]; var layerXPosElement = [NSXMLElement elementWithName:@"xPos" stringValue:layerXpos]; [layerElement addChild:layerXPosElement]; var layerYPosElement = [NSXMLElement elementWithName:@"yPos" stringValue:layerYpos]; [layerElement addChild:layerYPosElement]; var layerHeightElement = [NSXMLElement elementWithName:@"height" stringValue:layerHeight]; [layerElement addChild:layerHeightElement]; var layerWidthElement = [NSXMLElement elementWithName:@"width" stringValue:layerWidth]; [layerElement addChild:layerWidthElement]; //create the xml file var xmlData = [xmlObj XMLDataWithOptions:NSXMLNodePrettyPrint]; //name the xml file the name of the layer and save it to the folder [xmlData writeToFile:file_path+layerName+".xml"]; var alertMessage = layerName+".xml saved to: " + file_path; alert("Layer XML Exported!", alertMessage); }
<document> <layer> <name>Rectangle</name> <xPos>550</xPos> <yPos>258</yPos> <height>234</height> <width>235</width> </layer> </document>
function exportJSON(layer, file_path){ //initialize the layer array var layerArray = []; //create the variables var layerName = String(layer.name()); var layerFrame = layer.absoluteRect(); var layerXpos = String(layerFrame.x()); var layerYpos = String(layerFrame.y()); var layerHeight = String(layerFrame.height()); var layerWidth = String(layerFrame.width()); // add the strings to the array layerArray.push({ name: layerName, xPos: layerXpos, yPos: layerYpos, height: layerHeight, width: layerWidth, }); // Create the JSON object from the layer array var jsonObj = { "layer": layerArray }; // Convert the object to a json string var file = NSString.stringWithString(JSON.stringify(jsonObj, null, "\t")); // Save the file [file writeToFile:file_path+layerName+".json" atomically:true encoding:NSUTF8StringEncoding error:null]; var alertMessage = layerName+".json saved to: " + file_path; alert("Layer JSON Exported!", alertMessage); }
@import 'common.js' var onRun = function(context) { var doc = context.document; var selection = context.selection; //make sure something is selected if(selection.count() == 0){ doc.showMessage("Please select a layer."); }else{ //allow xml to be written to the folder var fileTypes = [NSArray arrayWithObjects:@"json", nil]; //create select folder window var panel = [NSOpenPanel openPanel]; [panel setCanChooseDirectories:true]; [panel setCanCreateDirectories:true]; [panel setAllowedFileTypes:fileTypes]; var clicked = [panel runModal]; //check if Ok has been clicked if (clicked == NSFileHandlingPanelOKButton) { var isDirectory = true; //get the folder path var firstURL = [[panel URLs] objectAtIndex:0]; //format it to a string var file_path = [NSString stringWithFormat:@"%@", firstURL]; //remove the file:// path from string if (0 === file_path.indexOf("file://")) { file_path = file_path.substring(7); } } //loop through the selected layers and export the XML for(var i = 0; i < selection.count(); i++){ var layer = selection[i]; exportJSON(layer, file_path); } } }; function exportJSON(layer, file_path){ //initialize the layer array var layerArray = []; //create the variables var layerName = String(layer.name()); var layerFrame = layer.absoluteRect(); var layerXpos = String(layerFrame.x()); var layerYpos = String(layerFrame.y()); var layerHeight = String(layerFrame.height()); var layerWidth = String(layerFrame.width()); // add the strings to the array layerArray.push({ name: layerName, xPos: layerXpos, yPos: layerYpos, height: layerHeight, width: layerWidth, }); // Create the JSON object from the layer array var jsonObj = { "layer": layerArray }; // Convert the object to a json string var file = NSString.stringWithString(JSON.stringify(jsonObj, null, "\t")); // Save the file [file writeToFile:file_path+layerName+".json" atomically:true encoding:NSUTF8StringEncoding error:null]; var alertMessage = layerName+".json saved to: " + file_path; alert("Layer JSON Exported!", alertMessage); }
{“layer”:[{“name”:”Rectangle”,”xPos”:”550",”yPos”:”258",”height”:”234",”width”:”235"}]}
Source: https://habr.com/ru/post/310968/
All Articles