├── .appcast.xml ├── .gitignore ├── LICENCE ├── README.md ├── assets └── icon.png ├── package-lock.json ├── package.json └── src ├── command.js └── manifest.json /.appcast.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # build artefacts 2 | *.sketchplugin 3 | 4 | # npm 5 | node_modules 6 | .npm 7 | npm-debug.log 8 | 9 | # mac 10 | .DS_Store 11 | 12 | # WebStorm 13 | .idea 14 | *.iml 15 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Bohemian B.V 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Date and Time Data Plugin 2 | 3 | [Sketch](https://sketch.com) Data plugin that allows you to generate random dates and 4 | times. 5 | 6 | ![Date and Time Data Plugin Screenshot](https://user-images.githubusercontent.com/69443/54697255-264d8b00-4b25-11e9-8fe8-68789616ae82.png) 7 | 8 | ## Installation 9 | 10 | ### From a release (simplest) 11 | 12 | - [Download](https://github.com/BohemianCoding/date-time-sketchplugin/releases/latest) the latest 13 | release of the plugin 14 | - Double-click the .zip archive file to extract the plugin 15 | - Double-click the `date-time.sketchplugin` to install the plugin 16 | 17 | ### From source 18 | 19 | - Clone the repo 20 | - Install the dependencies with `npm install` 21 | 22 | ## Using the plugin 23 | 24 | - Select one or more text layers. 25 | - Click on the Data button in the top left of the document's toolbar. 26 | - Under _Text_, select _Date & Time_ and then select a date or time format to 27 | generate. 28 | - Press _SHIFT + COMMAND + D_ to generate another random date or time. 29 | 30 | The date or time is localized to your current language and region system 31 | settings e.g. in the US the date will be in the format _month/day/year_ while in Europe it will be in the format _day/month/year_. 32 | 33 | ## Create your own Data plugin for Sketch 34 | 35 | To find out more about making your own Data plugin check out this [blog post](https://blog.sketchapp.com/do-more-with-data-2b765e870e4f) with a step by step guide. 36 | -------------------------------------------------------------------------------- /assets/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sketch-hq/date-time-sketchplugin/3803aec919d893f2bf01675e4a61b3b6e70f850f/assets/icon.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "date-time", 3 | "version": "1.1.0", 4 | "description": "A data plugin that generates random dates and times", 5 | "engines": { 6 | "sketch": ">=3.0" 7 | }, 8 | "skpm": { 9 | "name": "Date & Time", 10 | "manifest": "src/manifest.json", 11 | "main": "date-time.sketchplugin", 12 | "assets": [ 13 | "assets/**/*" 14 | ] 15 | }, 16 | "scripts": { 17 | "build": "skpm-build", 18 | "watch": "skpm-build --watch", 19 | "start": "skpm-build --watch --run", 20 | "postinstall": "npm run build && skpm-link" 21 | }, 22 | "repository": { 23 | "url": "https://github.com/BohemianCoding/date-time-sketchplugin" 24 | }, 25 | "author": "Sketch", 26 | "license": "MIT", 27 | "devDependencies": { 28 | "@skpm/builder": "^0.7.11" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/command.js: -------------------------------------------------------------------------------- 1 | const sketch = require('sketch') 2 | const { DataSupplier } = sketch 3 | 4 | const locale = currentLocale() 5 | 6 | const dateFormats = { 7 | dayMonthYear: {day: 'numeric', month: 'numeric', year: 'numeric'}, 8 | dayMonthYearPadded: {day: '2-digit', month: '2-digit', year: 'numeric'}, 9 | dayShortMonthYear: {day: 'numeric', month: 'short', year: 'numeric'}, 10 | dayMonth: {day: 'numeric', month: 'numeric'}, 11 | dayMonthPadded: {day: '2-digit', month: '2-digit'}, 12 | dayShortMonth: {day: 'numeric', month: 'short'}, 13 | dayLongMonth: {day: 'numeric', month: 'long'}, 14 | dayLongMonthYear: {day: 'numeric', month: 'long', year: 'numeric'} 15 | } 16 | 17 | const timeFormats = { 18 | hour12: {hour12: true, hour: 'numeric', minute: '2-digit'}, 19 | hour24: {hour12: false, hour: '2-digit', minute: '2-digit'}, 20 | hour24Seconds: {hour12: false, hour: '2-digit', minute:'2-digit', second:'2-digit'} 21 | } 22 | 23 | export function onStartup () { 24 | const names = new Set() 25 | registerDataSupplier(true, dateFormats.dayMonthYear, 'SupplyDateDayMonthYear', names) 26 | registerDataSupplier(true, dateFormats.dayMonthYearPadded, 'SupplyDateDayMonthYearPadded', names) 27 | registerDataSupplier(true, dateFormats.dayShortMonthYear, 'SupplyDateDayShortMonthYear', names) 28 | registerDataSupplier(true, dateFormats.dayMonth, 'SupplyDateDayMonth', names) 29 | registerDataSupplier(true, dateFormats.dayMonthPadded, 'SupplyDateDayMonthPadded', names) 30 | registerDataSupplier(true, dateFormats.dayShortMonth, 'SupplyDateDayShortMonth', names) 31 | registerDataSupplier(true, dateFormats.dayLongMonth, 'SupplyDateDayLongMonth', names) 32 | registerDataSupplier(true, dateFormats.dayLongMonthYear, 'SupplyDateDayLongMonthYear', names) 33 | registerDataSupplier(false, timeFormats.hour12, 'SupplyTime12Hour', names) 34 | registerDataSupplier(false, timeFormats.hour24, 'SupplyTime24Hour', names) 35 | registerDataSupplier(false, timeFormats.hour24Seconds, 'SupplyTime24HourSeconds', names) 36 | } 37 | 38 | function registerDataSupplier(isDate, format, action, names) { 39 | // Sketch was officially released on 7 Sep 2010 19:13:21 40 | // https://web.archive.org/web/20110711124304/http://www.bohemiancoding.com/about/blog/feed/ 41 | const date = new Date(2010, 8, 7, 19, 13, 21) 42 | const name = isDate ? 43 | `Date: ${date.toLocaleDateString(locale, format)}` : 44 | `Time: ${date.toLocaleTimeString(locale, format)}` 45 | 46 | // Some locales numbers always use padded format, avoid duplicates 47 | if (names.has(name)) return 48 | 49 | DataSupplier.registerDataSupplier('public.text', name, action) 50 | names.add(name) 51 | } 52 | 53 | export function onShutdown () { 54 | DataSupplier.deregisterDataSuppliers() 55 | } 56 | 57 | export function onSupplyDateDayMonthYear(context) { 58 | supplyDate(dateFormats.dayMonthYear, context) 59 | } 60 | 61 | export function onSupplyDateDayMonthYearPadded(context) { 62 | supplyDate(dateFormats.dayMonthYearPadded, context) 63 | } 64 | 65 | export function onSupplyDateDayShortMonthYear(context) { 66 | supplyDate(dateFormats.dayShortMonthYear, context) 67 | } 68 | 69 | export function onSupplyDateDayMonth(context) { 70 | supplyDate(dateFormats.dayMonth, context) 71 | } 72 | 73 | export function onSupplyDateDayMonthPadded(context) { 74 | supplyDate(dateFormats.dayMonthPadded, context) 75 | } 76 | 77 | export function onSupplyDateDayShortMonth(context) { 78 | supplyDate(dateFormats.dayShortMonth, context) 79 | } 80 | 81 | export function onSupplyDateDayLongMonth(context) { 82 | supplyDate(dateFormats.dayLongMonth, context) 83 | } 84 | 85 | export function onSupplyDateDayLongMonthYear(context) { 86 | supplyDate(dateFormats.dayLongMonthYear, context) 87 | } 88 | 89 | export function onSupplyTime12Hour(context) { 90 | supplyTime(timeFormats.hour12, context) 91 | } 92 | 93 | export function onSupplyTime24Hour(context) { 94 | supplyTime(timeFormats.hour24, context) 95 | } 96 | 97 | export function onSupplyTime24HourSeconds(context) { 98 | supplyTime(timeFormats.hour24Seconds, context) 99 | } 100 | 101 | function supplyDate(format, context) { 102 | const dataKey = context.data.key 103 | const dataCount = context.data.requestedCount 104 | for (let i = 0; i < dataCount; i++) { 105 | const dateString = randomDate().toLocaleDateString(locale, format) 106 | DataSupplier.supplyDataAtIndex(dataKey, dateString, i) 107 | } 108 | } 109 | 110 | function supplyTime(format, context) { 111 | const dataKey = context.data.key 112 | const dataCount = context.data.requestedCount 113 | for (let i = 0; i < dataCount; i++) { 114 | const timeString = randomTime().toLocaleTimeString(locale, format) 115 | DataSupplier.supplyDataAtIndex(dataKey, timeString, i) 116 | } 117 | } 118 | 119 | function randomDate() { 120 | const monthIndex = randomInteger(0, 11); 121 | const year = currentYear() 122 | const day = randomInteger(1, daysInMonth(monthIndex, year)) 123 | return new Date(year, monthIndex, day) 124 | } 125 | 126 | function randomTime() { 127 | const hours = randomInteger(0, 23) 128 | const minutes = randomInteger(0, 59) 129 | const seconds = randomInteger(0, 59) 130 | const today = new Date() 131 | return new Date(today.getFullYear(), today.getMonth(), today.getDate(), hours, minutes, seconds) 132 | } 133 | 134 | function randomInteger(min, max) { 135 | min = Math.ceil(min) 136 | max = Math.floor(max) 137 | return Math.floor(Math.random() * (max - min + 1)) + min 138 | } 139 | 140 | function daysInMonth (monthIndex, year) { 141 | return new Date(year, monthIndex + 1, 0).getDate(); 142 | } 143 | 144 | function currentYear() { 145 | return new Date().getFullYear() 146 | } 147 | 148 | function currentLocale() { 149 | // NSLocale.currentLocale() only returns the language that is supported by the host application 150 | const countryCode = NSLocale.currentLocale().localeIdentifier().split('_')[1] 151 | const languageCode = NSLocale.preferredLanguages()[0].split('-')[0] 152 | return `${languageCode}-${countryCode}` 153 | } -------------------------------------------------------------------------------- /src/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "compatibleVersion": 3, 3 | "bundleVersion": 1, 4 | "name": "Date and Time Data Plugin", 5 | "icon": "icon.png", 6 | "suppliesData": true, 7 | "identifier": "com.sketchapp.data-date-time", 8 | "description": "A data plugin that generates random dates and times", 9 | "commands": [ 10 | { 11 | "script": "command.js", 12 | "handlers": { 13 | "actions": { 14 | "Startup": "onStartup", 15 | "Shutdown": "onShutdown", 16 | "SupplyDateDayMonthYear": "onSupplyDateDayMonthYear", 17 | "SupplyDateDayMonthYearPadded": "onSupplyDateDayMonthYearPadded", 18 | "SupplyDateDayShortMonthYear": "onSupplyDateDayShortMonthYear", 19 | "SupplyDateDayShortMonth": "onSupplyDateDayShortMonth", 20 | "SupplyDateDayMonth": "onSupplyDateDayMonth", 21 | "SupplyDateDayMonthPadded": "onSupplyDateDayMonthPadded", 22 | "SupplyDateDayLongMonth": "onSupplyDateDayLongMonth", 23 | "SupplyDateDayLongMonthYear": "onSupplyDateDayLongMonthYear", 24 | "SupplyTime12Hour": "onSupplyTime12Hour", 25 | "SupplyTime24Hour": "onSupplyTime24Hour", 26 | "SupplyTime24HourSeconds": "onSupplyTime24HourSeconds" 27 | } 28 | } 29 | } 30 | ] 31 | } 32 | --------------------------------------------------------------------------------