├── demo.gif ├── Dollars.sketchplugin ├── Pure numbers.sketchplugin ├── Percentages.sketchplugin ├── sketchpack.json ├── js ├── loadNumbers.js ├── loadPercentages.js ├── loadDollars.js ├── replaceTextFitting.js └── utility.js └── README.md /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/auxdesigner/Sketch-Plugin---NumGen/HEAD/demo.gif -------------------------------------------------------------------------------- /Dollars.sketchplugin: -------------------------------------------------------------------------------- 1 | @import '../js/utility.js' 2 | @import '../js/loadDollars.js' 3 | 4 | loadText(); -------------------------------------------------------------------------------- /Pure numbers.sketchplugin: -------------------------------------------------------------------------------- 1 | @import '../js/utility.js' 2 | @import '../js/loadNumbers.js' 3 | 4 | loadText(); -------------------------------------------------------------------------------- /Percentages.sketchplugin: -------------------------------------------------------------------------------- 1 | @import '../js/utility.js' 2 | @import '../js/loadPercentages.js' 3 | 4 | loadText(); -------------------------------------------------------------------------------- /sketchpack.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "NumGen", 3 | "description": "Generate random numbers that are CTR, clicks, costs, and impr.", 4 | "author": "Liu Liu", 5 | "version": "0.0.3" 6 | } 7 | -------------------------------------------------------------------------------- /js/loadNumbers.js: -------------------------------------------------------------------------------- 1 | var loadText = function(){ 2 | for (var i = 0, l = [selection count]; i < l; i++) { 3 | var layer = selection[i]; 4 | var randomNum = (Math.random() * (1000.00 - 10.00) + 10.00).toFixed(0); 5 | [layer setStringValue: "" + randomNum]; 6 | [layer setName: "" + randomNum]; 7 | [layer adjustFrameToFit]; 8 | } 9 | tools.checkPluginUpdate(); 10 | }; -------------------------------------------------------------------------------- /js/loadPercentages.js: -------------------------------------------------------------------------------- 1 | var loadText = function(){ 2 | for (var i = 0, l = [selection count]; i < l; i++) { 3 | var layer = selection[i]; 4 | var randomNum = (Math.random() * (4.00 - 0.10) + 0.10).toFixed(2); 5 | [layer setStringValue: randomNum + "%"]; 6 | [layer setName: randomNum + "%"]; 7 | [layer adjustFrameToFit]; 8 | } 9 | tools.checkPluginUpdate(); 10 | }; -------------------------------------------------------------------------------- /js/loadDollars.js: -------------------------------------------------------------------------------- 1 | var loadText = function(){ 2 | for (var i = 0, l = [selection count]; i < l; i++) { 3 | var layer = selection[i]; 4 | var randomNum = (Math.random() * (1500.00 - 10.00) + 10.00).toFixed(2); 5 | 6 | function numberWithCommas(randomNum) { 7 | var parts = randomNum.toString().split("."); 8 | parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); 9 | return parts.join("."); 10 | } 11 | 12 | 13 | [layer setStringValue: "$" + numberWithCommas(randomNum)]; 14 | [layer setName: "$" + randomNum]; 15 | [layer adjustFrameToFit]; 16 | } 17 | tools.checkPluginUpdate(); 18 | }; -------------------------------------------------------------------------------- /js/replaceTextFitting.js: -------------------------------------------------------------------------------- 1 | var loadText = function(data, label){ 2 | for (var i = 0, l = [selection count]; i < l; i++) { 3 | var layer = selection[i], 4 | stringLength = [[layer stringValue] length], 5 | randomText = data[ Math.floor(Math.random() * data.length)]; 6 | 7 | while(randomText.length < stringLength){ 8 | randomText += data[ Math.floor(Math.random() * data.length)]; 9 | } 10 | 11 | randomText = randomText.slice(0, stringLength).replace(/[^\.]$/,".").replace(/ $/,"").replace(/ .$/,'.').replace(/ \w\.$/,"."); 12 | randomText = randomText.slice(0, stringLength).replace(/ $/).replace(/ .$/,'.'); 13 | 14 | if(randomText){ 15 | [layer setStringValue: randomText]; 16 | if(label === String) [layer setName: label]; 17 | } 18 | } 19 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sketch-Plugin---NumGen 2 | 3 | [Updates on 10/30/2015] 4 | Updated code to support Sketch 3.4 5 | 6 | [Updates on 09/24/2015] 7 | - I merged this plugin into the more extensive Content Generator plugin: https://github.com/timuric/Content-generator-sketch-plugin 8 | - Check out the Medium blog post "I made a Sketch Plugin. You can too.": https://medium.com/sketch-app-sources/i-made-a-sketch-plugin-you-can-too-58a28b7277f1 9 | 10 | 11 | When you work with repetitive data, (e.g. data table), if you simply copy and paste the rows, all the numbers will be the same, which doesn't make your design look realistic. But if you manually change each of them, that could be quite tedious. 12 | 13 | This plugin generates random formatted numbers. For now it includes CTR, clicks, costs, Impr. But you can modify the js files to include whatever kind of number you need. 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /js/utility.js: -------------------------------------------------------------------------------- 1 | var tools = { 2 | appVersion: "0.6.4", 3 | versionComponents : function() { 4 | var info = [[NSBundle mainBundle] infoDictionary]; 5 | var items = [[(info["CFBundleShortVersionString"]) componentsSeparatedByString:"."] mutableCopy]; 6 | 7 | while([items count] < 3) 8 | [items addObject:"0"]; 9 | 10 | return items; 11 | }, 12 | majorVersion : function() { 13 | var items = tools.versionComponents(); 14 | 15 | return items[0]; 16 | }, 17 | minorVersion : function() { 18 | var items = tools.versionComponents(); 19 | 20 | return items[1]; 21 | }, 22 | convertToString : function(objectString){ 23 | var i = 0; 24 | normalString = ""; 25 | while(objectString[i] !== null) normalString += objectString[i]; 26 | return normalString; 27 | }, 28 | 29 | saveFile : function(path,data){ 30 | var someContent = NSString.stringWithString_(data) 31 | var path = path 32 | someContent.dataUsingEncoding_(NSUTF8StringEncoding).writeToFile_atomically_(path, true) 33 | }, 34 | pluginPath : function(){ 35 | if(tools.majorVersion() == 3){ 36 | var pluginFolder = scriptPath.match(/Plugins\/([\w -])*/)[0] + "/"; 37 | var sketchPluginsPath = scriptPath.replace(/Plugins([\w \/ -])*.sketchplugin$/, ""); 38 | return pluginFolder; 39 | } 40 | }, 41 | getJSONFromURL: function(url) { 42 | var request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]], 43 | response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil], 44 | responseObj = [NSJSONSerialization JSONObjectWithData:response options:nil error:nil] 45 | return responseObj 46 | }, 47 | checkPluginUpdate: function(){ 48 | try{ 49 | var response = this.getJSONFromURL('https://raw.githubusercontent.com/timuric/Content-generator-sketch-plugin/master/sketchpack.json') 50 | if(response && response.version){ 51 | var rgx = new RegExp("\\d","g"); 52 | var removeVersion = parseFloat(response.version.match(rgx).join("")) 53 | var installedVersion = parseFloat(this.appVersion.match(rgx).join("")) 54 | if (removeVersion > installedVersion) [doc showMessage:"New plugin update is available! Visit github.com/timuric/Content-generator-sketch-plugin"] 55 | } 56 | }catch(e){ 57 | log(e); 58 | } 59 | } 60 | }; 61 | 62 | function alert(msg, title) { 63 | title = title || "Whoops"; 64 | var app = [NSApplication sharedApplication]; 65 | [app displayDialog:msg withTitle:title]; 66 | } 67 | 68 | function deleteLayer(layer){ 69 | var parent = [layer parentGroup]; 70 | if(parent) [parent removeLayer: layer]; 71 | } 72 | 73 | function capitalize(str) { 74 | return str.slice(0, 1).toUpperCase() + str.slice(1); 75 | } 76 | 77 | 78 | 79 | --------------------------------------------------------------------------------