Sketch updating from a live-edited text file

I am interested in making a sketch that periodically checks an open text file that is being edited live, and updates the screen based on the live changing text contents. For example, as a person types a list of words into a text file, the sketch periodically shows the new words on the screen.

Comparable examples I've seen have been live preview modes for e.g. markdown or livecoding.

Question: how would you recommend implementing a listening / updating mechanism?

For example, consider this starting sketch running on OS X. It is a working sketch that checks the contents of list.txt any time an in-focus key is pressed, and detects new words typed into a file open in TextEdit. This works even without explicitly saving the file -- just type new things and the sketch picks them up when manually refreshed.

/**
 * FileDaemon
 * 2017-10-23
 */
String[] lines;
String filename = "list.txt";
float x, y;

void setup(){
  noLoop();
}
void draw(){
  background(0);
  reload();
  drawText();
}
void drawText(){
  for (int i = 0 ; i < lines.length; i++) {
    x = random(width);
    y = random(height);
    text(lines[i],x,y);
    print(lines[i], ". ");
  }
  println("" + lines.length + " drawn");
}
void reload(){
  lines = loadStrings(filename);
}
void keyReleased(){
  redraw();
}

How to make this automatic? I could imagine adding a clock to this (just running a check periodically without manual intervention) -- but I'm not sure if this approach would work with other editors, or if it is cross platform, or if there is simply a better way, perhaps built in to Java.

Thanks for any advice.

Tagged:
Sign In or Register to comment.