├── README.md ├── prefixManager.sketchplugin └── Contents │ └── Sketch │ ├── manifest.json │ ├── prefixManager.js │ └── stripPathPrefixes.js └── screenshots ├── menu.png ├── prefixManager.png └── screenshots.sketch /README.md: -------------------------------------------------------------------------------- 1 | # Prefixation Sketch Plugin 2 | 3 | Manage the prefixes of your layers, artboards or symbols. 4 | 5 | - No more re-typing prefixes for every layer 6 | - Keep naming very consistent 7 | - Organize all your Symbols into folders, consistently and quickly - yes you can do batch renaming of prefixes! 8 | 9 | 10 | ## How it works 11 | 12 | **Path Prefix Manager** - Gathers all the layer names in your document that contain a "/" and generates a prefix list. You may then apply those prefixes to other layers, or create new one by typing it. 13 | 14 | **Remove Path Prefixes** - Strips all the path prefixes in your selection, leaving just the layer names. 15 | For example, "Folder 1 / Folder 2 / Layername" will become "Layername". 16 | 17 | 18 | ## Screenshots 19 | 20 | ![Example](screenshots/prefixManager.png?raw=true "Example") 21 | 22 | ![Example](screenshots/menu.png?raw=true "Example") 23 | 24 | 25 | ## Keyboard Shortcuts 26 | 27 | This plugin is especially useful when assigned to a keyboard shortcut. You must assign this yourself in _Apple > Preferences > Keyboard > Shortcuts_. [How to set up Keyboard Shortcuts](http://www.sketchtips.info/articles/custom-shortcuts). 28 | 29 | 30 | ## Installation 31 | 32 | [Install from Sketch Toolbox](http://sketchtoolbox.com/) 33 | -------------------------------------------------------------------------------- /prefixManager.sketchplugin/Contents/Sketch/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "Path Prefix Manager", 3 | "identifier" : "com.pathprefixmanager.sketchplugin", 4 | "version" : "1.0", 5 | "description" : "Manage prefixes", 6 | "authorEmail" : "peterberrecloth@gmail.com", 7 | "author" : "Peter Berrecloth", 8 | "commands" : [ 9 | { 10 | "script" : "prefixManager.js", 11 | "handler" : "onRun", 12 | "shortcut" : "", 13 | "name" : "Path Prefix Manager", 14 | "identifier" : "managePrefix" 15 | }, 16 | { 17 | "script" : "stripPathPrefixes.js", 18 | "handler" : "onRun", 19 | "shortcut" : "", 20 | "name" : "Remove Path Prefixes", 21 | "identifier" : "stripPathPrefixes" 22 | } 23 | ], 24 | "menu": { 25 | "isRoot": true, 26 | "items": [ 27 | { 28 | "title" : "Prefixation", 29 | "items" : [ 30 | "managePrefix", 31 | "stripPathPrefixes" 32 | ] 33 | } 34 | ] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /prefixManager.sketchplugin/Contents/Sketch/prefixManager.js: -------------------------------------------------------------------------------- 1 | 2 | // Replace path prefix 3 | var onRun = function(context) { 4 | var doc = context.document 5 | selection = context.selection 6 | selectionCount = selection.count() 7 | 8 | if (selectionCount <= 0) { 9 | doc.showMessage("Select at least one layer.") 10 | return 11 | } 12 | 13 | var options = collatePrefixes(context) 14 | 15 | var selectedLayers = context.selection; 16 | var loop = selectedLayers.objectEnumerator(); 17 | 18 | var prefix = createSelect('Choose or add a path prefix to apply',options, 1) 19 | if (prefix[0]==1001){ // Cancel button was pressed 20 | return 21 | } 22 | var prefix = prefix[1] 23 | 24 | while (layer = loop.nextObject()) { 25 | layerName = layer.name().split("/").pop().trim(); 26 | layer.setName(prefix + layerName); 27 | } 28 | }; 29 | 30 | 31 | function createSelect(msg, items, selectedItemIndex){ 32 | selectedItemIndex = selectedItemIndex || 0 33 | var itemsCount = items.length 34 | 35 | var accessory = NSComboBox.alloc().initWithFrame(NSMakeRect(0,0,200,25)) 36 | accessory.addItemsWithObjectValues(items) 37 | accessory.selectItemAtIndex(selectedItemIndex) 38 | 39 | var alert = NSAlert.alloc().init() 40 | alert.setMessageText(msg) 41 | alert.setInformativeText(itemsCount + " prefix(es) found in this document") 42 | alert.addButtonWithTitle('OK') 43 | alert.addButtonWithTitle('Cancel') 44 | alert.setAccessoryView(accessory) 45 | 46 | var responseCode = alert.runModal() 47 | var sel = accessory.indexOfSelectedItem() 48 | var value = accessory.objectValue() 49 | 50 | return [responseCode, value] 51 | } 52 | 53 | // Look at all layers in doc and spit out a list of prefixes 54 | function collatePrefixes(context){ 55 | var doc = context.document 56 | var pages = [doc pages]; 57 | var prefixList = [] 58 | 59 | // Loop through pages 60 | for (var i=0; i<[pages count]; i++) { 61 | var page = pages[i]; 62 | var layers = [page children]; 63 | // Loop through layers 64 | for (var j=0; j<[layers count]; j++) { 65 | var layer = layers[j]; 66 | var layerPrefix = layer.name().split("/").slice(0, -1).join("/").trim(); 67 | if (layerPrefix) { 68 | prefixList.push(layerPrefix + " / "); 69 | } 70 | } 71 | } 72 | 73 | // Removes duplicates and returns list 74 | return prefixList.reduce(function(a,b){if(a.indexOf(b)<0)a.push(b);return a;},[]); 75 | } 76 | -------------------------------------------------------------------------------- /prefixManager.sketchplugin/Contents/Sketch/stripPathPrefixes.js: -------------------------------------------------------------------------------- 1 | var onRun = function(context) { 2 | var selectedLayers = context.selection; 3 | var loop = selectedLayers.objectEnumerator(); 4 | while (layer = loop.nextObject()) { 5 | var layerName = layer.name(); 6 | layerName = layerName.split("/").pop().trim(); 7 | layer.setName(layerName); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /screenshots/menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pberrecloth/prefixation-sketch-plugin/16e3269aaa02a45e41389a64125b8098545ab0fb/screenshots/menu.png -------------------------------------------------------------------------------- /screenshots/prefixManager.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pberrecloth/prefixation-sketch-plugin/16e3269aaa02a45e41389a64125b8098545ab0fb/screenshots/prefixManager.png -------------------------------------------------------------------------------- /screenshots/screenshots.sketch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pberrecloth/prefixation-sketch-plugin/16e3269aaa02a45e41389a64125b8098545ab0fb/screenshots/screenshots.sketch --------------------------------------------------------------------------------