Last active
June 13, 2017 12:30
-
-
Save MrPeak/8ddd419bdb77fb9a3bf3b850a6d52927 to your computer and use it in GitHub Desktop.
A small node script that reads a sketch file, changes a layer name on the page, and saves back to that file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| // lib to read and write to the zip | |
| const JSZip = require('jszip'); | |
| fs.readFile('Untitled3.sketch', function(err, data) { | |
| if(err) throw err; | |
| JSZip.loadAsync(data).then(function(zip) { | |
| // zip contains a json file that describes all the directory & files in the zip file | |
| // read the top level page | |
| // hardcoding page because im lazy ;) | |
| const pagePath = Object.keys(zip.files)[1]; | |
| zip.file(pagePath) | |
| .async('string') | |
| .then(function(str) { | |
| const json = JSON.parse(str); | |
| // grab the first layer which in my case is a text layer | |
| const text = json.layers[0]; | |
| text.name = 'Changing the layer name'; | |
| // write the page json back to the file in the zip | |
| zip.file(pagePath, JSON.stringify(json)) | |
| // override the original file | |
| zip | |
| .generateNodeStream({ type:'nodebuffer', streamFiles:true }) | |
| .pipe(fs.createWriteStream('Untitled3.sketch')) | |
| .on('finish', () => { | |
| console.log('yay saved file. Open me in sketch to see the changes'); | |
| }); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment