├── ExportSymbols.sketchplugin └── Contents │ └── Sketch │ ├── common.js │ ├── manifest.json │ └── ExportSymbols.js └── README.md /ExportSymbols.sketchplugin/Contents/Sketch/common.js: -------------------------------------------------------------------------------- 1 | 2 | function alert(title, message){ 3 | var app = [NSApplication sharedApplication]; 4 | [app displayDialog:message withTitle:title]; 5 | } 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Export Symbols Plugin For Sketch 2 | ============================= 3 | 4 | ## Export one or export them all 5 | Export all symbols in a document to a specified folder, or select symbols and export individually. 6 | 7 | You can use this two ways: 8 | 9 | - Have nothing selected and it will export all the Symbols from the Symbol page 10 | 11 | or 12 | 13 | - Select individual Symbols from the Symbol page and export only those 14 | 15 | ## Install 16 | 17 | Copy **`ExportSymbols.sketchplugin`** to **Sketch** plugins folder. 18 | 19 | ## Shortcut 20 | 21 | *`ctrl` + `shift` +* **`e`** 22 | 23 | 24 | Symbol page must be named "Symbols" 25 | 26 | ## Author 27 | 28 | Mike Mariano 29 | 30 | mike@uiuxartist.com 31 | 32 | http://uiuxartist.com 33 | -------------------------------------------------------------------------------- /ExportSymbols.sketchplugin/Contents/Sketch/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "author" : "Mike Mariano", 3 | "commands" : [ 4 | { 5 | "script" : "ExportSymbols.js", 6 | "handler" : "onRun", 7 | "shortcut" : "ctrl shift e", 8 | "name" : "Export Symbols", 9 | "identifier" : "export-symbols", 10 | }, 11 | ], 12 | "menu" : { 13 | "title" : "Export Symbols", 14 | "isRoot" : true, 15 | "items" : [ 16 | "export-symbols", 17 | ], 18 | }, 19 | "identifier" : "com.marianomike.sketch-plugin", 20 | "description": "Exports all or selected symbols as pngs to a specified folder.", 21 | "authorEmail" : "mike@uiuxartist.com", 22 | "name" : "Export Symbols", 23 | "homepage": "http://uiuxartist.com", 24 | "version": "1.0", 25 | "compatibleVersion": 3.5, 26 | "bundleVersion": 1 27 | } 28 | -------------------------------------------------------------------------------- /ExportSymbols.sketchplugin/Contents/Sketch/ExportSymbols.js: -------------------------------------------------------------------------------- 1 | @import 'common.js' 2 | 3 | var onRun = function(context) { 4 | //reference the sketch document 5 | var doc = context.document; 6 | //reference the pages array in the document 7 | var pages = [doc pages]; 8 | //name of the Symbols page 9 | var symbolsPageName = "Symbols"; 10 | 11 | //if the doc has a symbol page store it in here 12 | var hasSymbolsPage = false; 13 | hasSymbolsPage = checkIfHasSymbolsPage(pages, symbolsPageName); 14 | 15 | //reference a selected layer 16 | var selection = context.selection; 17 | 18 | //first check if something is selected 19 | if(selection.count() == 0){ 20 | 21 | //if nothing is selected, then check if the document has a Symbols page 22 | if(hasSymbolsPage == true){ 23 | 24 | //select the folder to save the pngs to 25 | var file_path = selectFolder(); 26 | 27 | //export all the pngs to the folder 28 | exportAllSymbols(doc, pages, symbolsPageName, file_path); 29 | 30 | //if user doesn't press cancel, alert that the export is done 31 | if(file_path != undefined){ 32 | alert("Symbols Exported!", "Symbols exported to : "+file_path); 33 | } 34 | 35 | }else{ 36 | //alert user if no symbols page found 37 | alert("No Symbols Page Found!", "There is no page in this document named: "+symbolsPageName); 38 | } 39 | }else{ 40 | //checks to see if selected layers are a MSSymbolMaster 41 | var isSymbolMaster = checkSelected(selection); 42 | 43 | //if they are, then loop through selection and export 44 | if(isSymbolMaster == true){ 45 | 46 | //select the folder to save the pngs to 47 | var file_path = selectFolder(); 48 | 49 | //export all MSSymbolMaster layers selected to folder 50 | for(var i = 0; i < selection.count(); i++){ 51 | if(selection[i].class() == "MSSymbolMaster"){ 52 | var artboard = selection[i]; 53 | var artboardName = [artboard name]; 54 | doc.saveArtboardOrSlice_toFile(artboard,file_path+"/"+artboardName+".png"); 55 | 56 | if(file_path != undefined){ 57 | alert("Symbol Exported!", artboardName+".png exported to : "+file_path); 58 | } 59 | } 60 | } 61 | }else{ 62 | //tell user they can only select MSSymbolMaster layers 63 | doc.showMessage("Please only select Symbol Masters."); 64 | } 65 | } 66 | } 67 | 68 | function checkSelected(selection){ 69 | for(var i = 0; i < selection.count(); i++){ 70 | if(selection[i].class() == "MSSymbolMaster"){ 71 | return true; 72 | }else{ 73 | return false; 74 | } 75 | } 76 | } 77 | 78 | function selectFolder(){ 79 | //open a window to select a folder to save to 80 | var panel = [NSOpenPanel openPanel]; 81 | [panel setCanChooseDirectories:true]; 82 | [panel setCanCreateDirectories:true]; 83 | 84 | //checks if user clicks open in window 85 | var clicked = [panel runModal]; 86 | if (clicked == NSFileHandlingPanelOKButton) { 87 | 88 | var isDirectory = true; 89 | var firstURL = [[panel URLs] objectAtIndex:0]; 90 | var unformattedURL = [NSString stringWithFormat:@"%@", firstURL]; 91 | 92 | //makes sure spaces aren't formatted to %20 93 | var file_path = [unformattedURL stringByRemovingPercentEncoding]; 94 | 95 | //removes file:// from path 96 | if (0 === file_path.indexOf("file://")) { 97 | file_path = file_path.substring(7); 98 | return file_path; 99 | } 100 | } 101 | } 102 | 103 | function checkIfHasSymbolsPage(pages, symbolsPageName){ 104 | 105 | var symbolPageCount = 0; 106 | 107 | for (var i = 0; i < pages.count(); i++){ 108 | 109 | //reference each page 110 | var page = pages[i]; 111 | 112 | //get the page name 113 | var pageName = [page name]; 114 | 115 | //checks if the doc has a page with the name symbolsPageName 116 | if (pageName == symbolsPageName){ 117 | symbolPageCount = symbolPageCount + 1; 118 | } 119 | } 120 | if(symbolPageCount > 0){ 121 | return true; 122 | }else{ 123 | return false; 124 | } 125 | } 126 | 127 | function exportAllSymbols(doc, pages, symbolsPageName, file_path){ 128 | //loop through the pages array 129 | for (var i = 0; i < pages.count(); i++){ 130 | 131 | //reference each page 132 | var page = pages[i]; 133 | 134 | //get the page name 135 | var pageName = [page name]; 136 | 137 | //checks if the page name is Symbols 138 | if (pageName == symbolsPageName){ 139 | 140 | //reference the artboards of each symbol 141 | var artboards = [page artboards]; 142 | for (var z = 0; z < artboards.count(); z++){ 143 | 144 | //reference each artboard of each page 145 | var artboard = artboards[z]; 146 | 147 | //get the name of the artboard and uses it as the file name 148 | var artboardName = [artboard name]; 149 | 150 | //export the artboard 151 | doc.saveArtboardOrSlice_toFile(artboard,file_path+"/"+artboardName+".png"); 152 | 153 | } 154 | } 155 | } 156 | } 157 | --------------------------------------------------------------------------------