Skip to content

Instantly share code, notes, and snippets.

@nbqx
Created April 15, 2011 06:37

Revisions

  1. nbqx revised this gist Apr 15, 2011. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions capture_glitch.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    #!/usr/bin/env node

    //http://vimeo.com/22429969

    var fs = require('fs');
    var spawn = require('child_process').spawn;

  2. nbqx created this gist Apr 15, 2011.
    54 changes: 54 additions & 0 deletions capture_glitch.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    #!/usr/bin/env node
    var fs = require('fs');
    var spawn = require('child_process').spawn;

    var cap = "/path/to/cap.jpg";
    var glt = "/path/to/cap_glitch.jpg";

    function changeDesktopPicture(){
    var scrt = (function(){/*
    tell application "Finder"
    set org to POSIX file "/path/to/cap.jpg" as string
    set desktop picture to file org
    set pic to POSIX file "/path/to/cap_glitch.jpg" as string
    set desktop picture to file pic
    end tell
    */}).toString().replace(/^.*?\n/,'').replace(/\n.*?$/,'');

    var doing = spawn('osascript',['-e',scrt]);
    }

    function glitch(f){
    var v = (Math.random()>0.5)? 0.95 : Math.random()*1.5;
    var file = f;
    var buf = fs.readFileSync(file);

    for(var i=0; i<buf.length; i++){
    if(Math.random()>v){
    if(buf[i]==18){
    if(Math.random()>0.5){
    buf[i] = Math.floor(Math.random()*10);
    }else{
    buf[i] = buf[i+1];
    }
    }
    }
    }

    return buf
    }

    function capture(){
    var cmd = spawn('screencapture',['-W','-tjpeg',cap]);
    cmd.on('exit',function(){
    var buf = glitch(cap);
    fs.writeFile(glt,buf,function(err){
    if(err) throw err;
    changeDesktopPicture();
    });
    });
    }

    capture();