Skip to content

Instantly share code, notes, and snippets.

View marianomike's full-sized avatar

Mike Mariano marianomike

View GitHub Profile
//create panel for user to select file
var open = NSOpenPanel.openPanel();
var fileTypes = [NSArray arrayWithObjects:@"json",nil];
open.setAllowedFileTypes(fileTypes);
open.setCanChooseDirectories(true);
open.setCanChooseFiles(true);
open.setCanCreateDirectories(true);
open.setTitle("Import a Color Palette");
open.setPrompt("Import Palette");
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function colorFromString(value) {
var immutable = MSImmutableColor.colorWithSVGString_(value);
return MSColor.alloc().initWithImmutableObject_(immutable);
}
function alert(title, message){
@import 'common.js'
var onRun = function(context) {
var sketch = context.api();
var doc = sketch.selectedDocument;
//get the name of the document and remove the file extension if there is one
var documentName = removeFileExtension(doc.sketchObject.displayName());
for (var i = 0; i < sharedStyles.numberOfSharedStyles(); i++){
layerStyle = sharedStyles.objects().objectAtIndex(i);
var colorName = String(layerStyle.name());
var colorHex = "#" + layerStyle.value().firstEnabledFill().color().immutableModelObject().hexValue();
}
@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{
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());
@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{
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();
//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);
//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];