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.

Revisions

  1. MrPeak renamed this gist Jun 13, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. MrPeak created this gist May 22, 2017.
    36 changes: 36 additions & 0 deletions js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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');
    });
    });
    });
    });