├── README.md ├── counter-sketch-plugin_volorf.gif └── counter.sketchplugin └── Contents └── Sketch ├── counter.js └── manifest.json /README.md: -------------------------------------------------------------------------------- 1 | # Counter — counting characters and words in a text layer 2 | 3 | 4 | ![Counter](/counter-sketch-plugin_volorf.gif) 5 | 6 | 7 | ## How it works 8 | 1. Select a text layer; 9 | 2. Click menu item ```Plugin > Counter > Count```. 10 | 11 | 12 | ## How install the plugin 13 | 1. [Download the zip file with the Counter](https://github.com/Volorf/Counter/archive/master.zip). 14 | 2. Copy the contents to the Plugin Folder (Plugin > Manage Plugins... > [Show Plugins Folder](http://frolovoleg.ru/images/sketch-plugin-folder.png)). 15 | -------------------------------------------------------------------------------- /counter-sketch-plugin_volorf.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Volorf/Counter/beb9eb6ff9246ab662a77f44b9352ba988676980/counter-sketch-plugin_volorf.gif -------------------------------------------------------------------------------- /counter.sketchplugin/Contents/Sketch/counter.js: -------------------------------------------------------------------------------- 1 | function countCharatersAndWordsInTheTextLayer(context) { 2 | 3 | var doc = context.document; 4 | 5 | // Array for UTF-16 6 | var arrUtf16 = [] 7 | 8 | // Convenient func 9 | function checkForChar(charUtf16) { 10 | var isItChar = false 11 | if ((65 <= charUtf16) && (charUtf16 <= 90)) { 12 | isItChar = true 13 | } else if ((97 <= charUtf16) && (charUtf16 <= 122)) { 14 | isItChar = true 15 | } else if ((192 <= charUtf16) && (charUtf16 <= 382)) { 16 | isItChar = true 17 | } else if ((1024 <= charUtf16) && (charUtf16 <= 1327)) { 18 | isItChar = true 19 | } 20 | return isItChar 21 | } 22 | 23 | // Default amount of words 24 | var amountWords = 0; 25 | 26 | // Default amount of characters 27 | var amountOfCharactersInLayer = 0; 28 | 29 | // Alert 30 | var app = NSApplication.sharedApplication() 31 | 32 | // Get selected elements from your artboard 33 | var selection = context.selection; 34 | 35 | // Check amount of selected layers 36 | if (selection.length == 0) { 37 | doc.showMessage("You didnt select any layers") 38 | } if (selection.length > 1){ 39 | doc.showMessage("You selected more than one layer") 40 | } else { 41 | 42 | // Define first layer 43 | var layer = selection[0] 44 | 45 | // Check type of the layer 46 | if (!(layer.className() == "MSTextLayer")) { 47 | doc.showMessage("Wrong type of layer") 48 | } else { 49 | 50 | // Get data from the layer 51 | var dataOfLayer = layer.stringValue() 52 | 53 | // Contert text layer data to String 54 | var stringWeNeed = String(dataOfLayer) 55 | 56 | // Super hack 57 | stringWeNeed += "." 58 | 59 | // Define amount of characters of the layer's data 60 | amountOfCharactersInLayer = stringWeNeed.length 61 | 62 | // Fill out arrUtf16 63 | for (var i = 0; i < amountOfCharactersInLayer; i++) { 64 | var utf16 = stringWeNeed.charCodeAt(i) 65 | arrUtf16.push(Number(utf16)) 66 | } 67 | 68 | // Count amount of words in stringWeNeed 69 | for (var j = 1; j < amountOfCharactersInLayer; j++) { 70 | if ( (!(checkForChar(arrUtf16[j]))) && (checkForChar(arrUtf16[j-1])) ) { 71 | amountWords++ 72 | } 73 | } 74 | } 75 | } 76 | 77 | // Show a result. Remember about the Super Hack 78 | var result = "Characters: " + (amountOfCharactersInLayer - 1) + "\n" + "Words: " + amountWords 79 | app.displayDialog_withTitle(result, "Counter") 80 | 81 | } 82 | -------------------------------------------------------------------------------- /counter.sketchplugin/Contents/Sketch/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Counter", 3 | "description": "Counting characters and words in the text layer", 4 | "author": "Oleg Frolov", 5 | "authorEmail": "frolololeg@gmail.com", 6 | "homepage": "https://github.com/Volorf/Counter", 7 | "version": 1.0, 8 | "identifier": "com.example.sketch.counter", 9 | "compatibleVersion": 1, 10 | "bundleVersion": 1, 11 | "commands": [ 12 | { 13 | "name": "Count", 14 | "identifier": "count", 15 | "shortcut": "ctrl shift c", 16 | "script": "counter.js", 17 | "handler": "countCharatersAndWordsInTheTextLayer" 18 | } 19 | ], 20 | "menu": { 21 | "items": [ 22 | "count" 23 | ] 24 | } 25 | } 26 | --------------------------------------------------------------------------------