How to make the selectFolder() function open using a default path

here is an Editor with 3 Mouse Buttons that shows
save and load


// Editor
// from https : // forum.processing.org/two/discussion/comment/112902/#Comment_112902

// editor path and file extension
final String pathFolder    = "texts";
final String fileExtension = ".txt";

// editor content
String strText = "Test ";

// states of the program:
// unique constants:
final int NORMAL = 0;
final int SAVE   = 1;
final int LOAD   = 2;
///current state (must be one of them)
int state=NORMAL;

// blinking cursor:
boolean blinkIsOn=true;

// Paths
String savePath="";
String loadPath="";

// ------------------------------------------------
// Core functions of processing

void setup() {
  size(900, 900);
}//func

void draw() {

  switch (state) {

  case NORMAL:
    drawForStateNormal() ;
    break;

  case SAVE:
    // wait for Save Dialog
    waitForSaveDialog();
    break;

  case LOAD:
    // wait for Load Dialog
    waitForLoadDialog();
    break;

  default:
    //Error
    println("Fail");
    exit();
    break;
    //
  }//switch
}//func

// ------------------------------------------------

void drawForStateNormal() {

  background(0);

  textSize(14);

  // title
  fill(255, 2, 2);
  text("My little Editor",
    width-123, 20, 100, 422);

  // show the text the user entered
  fill(255);
  text(strText+blink(),
    20, 20, width-170, height-20);

  // ----------------------
  // buttons
  textSize(11);
  fill(128);
  if ( overSave() ) {
    fill(196);
  }
  rect(width-40, height-20, 40, 20);
  fill(255);
  text("Save",
    width-40+7, height-9+5);

  // ---
  fill(128);
  if ( overLoad() ) {
    fill(196);
  }
  rect(width-40, height-50, 40, 20);
  fill(255);
  text("Load",
    width-40+7, height-50+9+5);

  // ---
  fill(128);
  if ( overNew() ) {
    fill(196);
  }
  rect(width-40, height-80, 40, 20);
  fill(255);
  text("New",
    width-40+7, height-80+9+5);
}

//----------------------------------------------------------------------------
// functions to register if mouse is over buttons

boolean overSave() {
  return( mouseX > width-40 &&
    mouseY > height-20 );
}

boolean overLoad() {
  return( mouseX > width-40 &&
    mouseY > height-50  &&
    mouseY < height-50+25 );
}

boolean overNew() {
  return( mouseX > width-40 &&
    mouseY > height-80  &&
    mouseY < height-80+25 );
}

// ---------------------------------------------------------------------------
// Inputs

void keyPressed() {

  if (state!=NORMAL)
    return;  // !!!

  // for the editor: ---------------------------------------

  if (key == CODED) {

    if ( keyCode == DELETE || keyCode == BACKSPACE ) {
      if ( strText.length() > 0 ) {
        strText = strText.substring(0, strText.length()-1);
      }
    }

    return; // !!!
  }

  // key != CODED -------------------------------

  if (key==ESC) {
    key=0; // kill ESC
  } else {
    strText += key; // add to text
  }// else
  //
}//func

void mousePressed() {

  if (state!=NORMAL)
    return;

  // for the buttons
  if ( overSave() ) {
    initSave();
  }
  //---
  else if ( overLoad() ) {
    initLoad();
  }
  //---
  else if ( overNew() ) {
    strText="";
  }
  //
}//func

// -------------------------------------------------
// Save and load

void initSave() {
  // init save process
  // reset
  savePath="";
  // make date time stamp (the expression nf(n,2) means leading zero: 2 becomes 02)
  String dateTimeStamp = year()
    + nf(month(), 2)
    + nf(day(), 2)
    + "-"
    + nf(hour(), 2)
    + nf(minute(), 2)
    + nf(second(), 2);
  // prepare fileDescription which occurs in the dialogue
  File fileDescription = new File( sketchPath()
    + "//"
    + pathFolder
    + "//"
    + dateTimeStamp
    + fileExtension);
  // open the dialog
  selectOutput("Select a file to write to", "fileSelectedSave", fileDescription);
  // set state to wait
  state=SAVE;
}

void initLoad() {
  // init load process
  // reset
  loadPath="";
  // prepare fileDescription which occurs in the dialogue
  File fileDescription = new File( sketchPath()+"//"+pathFolder+"//"+"*" + fileExtension );
  // open the dialog
  selectInput("Select a file to load", "fileSelectedLoad", fileDescription);
  // set state to wait
  state=LOAD;
}

void fileSelectedSave(File selection) {
  // the 'callback' function
  if (selection == null) {
    // println("Window was closed or the user hit cancel.");
    // go back
    state=NORMAL;
  } else {
    // println("User selected " + selection.getAbsolutePath());
    savePath=selection.getAbsolutePath();
  }
}

void fileSelectedLoad(File selection) {
  // the 'callback' function
  if (selection == null) {
    // println("Window was closed or the user hit cancel.");
    // go back
    state=NORMAL;
  } else {
    // println("User selected " + selection.getAbsolutePath());
    loadPath=selection.getAbsolutePath();
  }
}

void waitForSaveDialog() {
  if (!savePath.equals("")) {
    // waiting is over
    saveIt();
    // go back
    state=NORMAL;
  }
}

void waitForLoadDialog() {
  if (!loadPath.equals("")) {
    // waiting is over
    loadIt();
    // go back
    state=NORMAL;
  }
}

void saveIt() {
  // save
  // split at line break and make array (to save it)
  String[] strs = split ( strText, "\n" );
  // check if file extension (fileExtension, e.g. .txt) is there
  int len = savePath.length();
  if (len<4 || !savePath.substring( len-4 ).equals(fileExtension)) {
    // file Extension is not present, we have to add it
    savePath += fileExtension; // add the file Extension
  }
  // save
  println("Saved: " + savePath);
  saveStrings( savePath, strs );
}

void loadIt() {
  // load
  String[] strs = loadStrings( loadPath );
  strText = join(strs, "\n");
}

// -------------------------------------------------
// Misc

String blink() {
  // toggle blinkIsOn
  if (frameCount%17 == 0)
    blinkIsOn=!blinkIsOn;

  // depending from blinkIsOn
  if (blinkIsOn)
    return "|";
  else return "";
}
//

2 Likes