Skip to content

Instantly share code, notes, and snippets.

@MrPeak
Last active June 13, 2017 12:30
Show Gist options
  • Save MrPeak/8ddd419bdb77fb9a3bf3b850a6d52927 to your computer and use it in GitHub Desktop.
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
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