# Is there a "file -\> open" possibility inside of a sketch?

**URL:** <https://discourse.processing.org/t/is-there-a-file-open-possibility-inside-of-a-sketch/29102>\
**Category:** Coding Questions\
**Created:** [April 3, 2021, 11:55pm UTC](https://discourse.processing.org/t/is-there-a-file-open-possibility-inside-of-a-sketch/29102 "2021-04-03T23:55:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [April 3, 2021, 11:55pm UTC](https://discourse.processing.org/t/is-there-a-file-open-possibility-inside-of-a-sketch/29102/1 "2021-04-03T23:55:48Z")

</div>

I am using the sketch to read various CSV files that have a date in them.  
Then I interpret the data and make graphs out of it.

I use  
table = loadTable(“Infinity\_Report\_3\_31\_2021.csv”, “header”);  
Then I populate arrays with my data.

I would like to browse inside the sketch to different files I want to open.  
Like you go in “word” and browse for files to open.

Long term I would want to select a date range and open multiple files and see trends…

Is this possible?

Also I am trying to open a file online using the hugely popular “OneDrive” from microsoft.  
In sharing, One drive gave me a URL  
[https://onedrive.live.com/Edit.aspx?resid=B15E9906640B6318!83888&wd=cpe&authkey=!AK6-OP68Ew\_FfGE](https://onedrive.live.com/Edit.aspx?resid=B15E9906640B6318!83888&wd=cpe&authkey=!AK6-OP68Ew_FfGE)

the link will go to my file OK. So I tried using it in Processing.

```auto
 table = loadTable("https://onedrive.live.com/Edit.aspx?resid=B15E9906640B6318!83888&wd=cpe&authkey=!AK6-OP68Ew_FfGE", "csv");

```

but it did not work out, I get a runtime exception

Please help,  
Would be great to use OneDrive

Thanks

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [April 4, 2021, 8:05am UTC](https://discourse.processing.org/t/is-there-a-file-open-possibility-inside-of-a-sketch/29102/2 "2021-04-04T08:05:33Z")

</div>

> [@laptophead](#):
>
> interpret

Yes there is a possibility with the command

```auto
selectInput("Window_name", "fileSelected");

```

When a file gets selected the method

```auto
void fileSelected(File selection){
}

```

You can call the path with

```auto
selection.getPath()

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 4, 2021, 7:31pm UTC](https://discourse.processing.org/t/is-there-a-file-open-possibility-inside-of-a-sketch/29102/3 "2021-04-04T19:31:21Z")

</div>

> [@NumericPrime](#):
>
> `fileSelected`

that’s of course the right approach when you have files on your hard drive

but `onedrive` is different I guess - it’s probably a kind of API where you need to figure out whether there is a way to get a file list and then browse the file list.

Did you check the documentation?

> **[OneDrive for Developers - OneDrive dev center](https://learn.microsoft.com/en-us/onedrive/developer/?view=odsp-graph-online)**

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [April 5, 2021, 5:16pm UTC](https://discourse.processing.org/t/is-there-a-file-open-possibility-inside-of-a-sketch/29102/4 "2021-04-05T17:16:09Z")

</div>

Thank you everyone. Lets focus on loading the local file first.  
I have a hard time making loadTable accept the file name I select.  
I browsed the whole internet, no examples… Please help

Here is what I got.

```auto
String Mach_Name []= {"00", "31", "200", "700", "POD1", "POD2", "BHX1", "BHX2", "EDG1", "EDG2", "10", "M_11"};// 11 vals
int [] Cycles = new int [100];
float [] Pr = new float [100]; // percentage of Uptime
Table table;
int incr=1;
File filename;
void setup() {
  size(1900, 1000, JAVA2D);
selectInput("Select a file to process:", "fileSelected", filename);

  //table = loadTable("https://onedrive.live.com/Edit.aspx?resid=B15E9906640B6318!83888&wd=cpe&authkey=!AK6-OP68Ew_FfGE", "csv");

 
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
    println ("Filename: "+ filename);
  }
  
/* 
    table = loadTable(filename, "header");
    println(("Row COunt is " + table.getRowCount()));
   for (TableRow row : table.rows()) 
  {
    Mach_Name [incr] = row.getString("MACH ID");
    Pr[incr] = row.getInt("UPTIME %");
    Cycles [incr] = row.getInt("CYCLES TODAY");

    incr++;
    // println(name + " (" + species + ") has an ID of " + id);
  }
  for (int i=1; i<=10; i++)
  {
    println (i + " Mach_Name; "+Mach_Name[i]+ " %="+ Pr[i] );
  }
  */
}

void mouseClicked() 
{

  selectInput("Window_name", "fileSelected");
  println(filename);
}

```

Also,  
The selectInput( function works only in setup?  
I tried it under mouseClicked and nothing executes.

Thats a burmmer. The user should be able to load various files without restarting the sketch.

Thanks

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [April 5, 2021, 6:13pm UTC](https://discourse.processing.org/t/is-there-a-file-open-possibility-inside-of-a-sketch/29102/5 "2021-04-05T18:13:30Z")

</div>

heres a class that can handle file selection or folder selection

```auto
class fileInput{
  String value;
  boolean click = false;
  
  fileInput(){
    
  };
  
 String listen(){
   String s = null;
  if(click())selectInput("Select a file to process:", "fileSelected");
  s = value;
  return s;
 }

  boolean click(){
      boolean k = false;
      if (pos()&&mousePressed&&!click){
        click = true;
        k = false;
      }else if(click&&!mousePressed){
        k = true;
        click = false;
      }
      
      return k;
  };
    
  boolean pos(){
    return mouseX>0&&mouseX<width&&mouseY>0&&mouseY<height;
  };
  
};

void fileSelected(File selection) {
  String s = null;
    if (selection != null) {
      
      file.value = selection.getAbsolutePath();
      //println("User selected " + file.value);
    }
    //return s;
  };
  
class folderInput extends fileInput{
   
  folderInput(){
    
  };
 // @Override
 // public void listen(){
 // if(click())selectInput("Select a file to process:", "folderSelected");
 //}
  
};
  
//void folderSelected(File selection) {
// if (selection == null) {
// println("Window was closed or the user hit cancel.");
// } else {
// println("User selected " + selection.getAbsolutePath());
// }
//};

String folderSelected(File selection) {
  String s = null;
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
    s = selection.getAbsolutePath();
  }
  return s;
};

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 5, 2021, 6:17pm UTC](https://discourse.processing.org/t/is-there-a-file-open-possibility-inside-of-a-sketch/29102/6 "2021-04-05T18:17:43Z")

</div>

you beat me to it

here is an example with buttons

```auto
// Demo for selectInput

//states 
final int stateWaitForFile = 0; // consts  
final int stateDone = 1;
final int stateBreak = 2;  
final int stateWaitForButton = 3;                
int state = stateWaitForButton; // current

String fileGlobal = "";  

Table tableTest ; 

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

void setup() {
  size (1100, 700); 
  background(111);
}

void draw() {
  // 
  // switch 
  switch (state) {

  case stateWaitForFile:
    // waiting until file has been selected
    background(111);
    text("Please choose a file. Hit Cancel to abort.", 
      33, 33);    

    // THE WAIT IS OVER   
    if (!fileGlobal.equals("")) {
      state = stateDone;
      // here you would load the table
      tableTest = loadTable(fileGlobal); 
      //
    } // if 
    break;

  case stateDone:
    // here you would have the table and display it 
    background(111);
    text("File is there now: "
      +fileGlobal, 100, 100); 
    showButtons();
    text(tableTest.getColumnCount(), 30, height-177); //
    break;

  case stateWaitForButton:
    background(111);
    text("Please selecr a button.", 100, 100); 
    showButtons(); 
    break; 

  case stateBreak:
    background(111);
    text("Window was closed or the user hit cancel.", 33, 33); 
    showButtons(); 
    break; 

  default:
    // error
    println ("error 91"); 
    exit();
    break;
    //
  } // switch

  //debug
  fill(255);
  text(state, 30, height-30);
} // func 

// -----------------------------------------------------------------
// file handling 

void fileSelected(File selection) {
  //
  // the 'callback' function.
  //
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
    state = stateBreak;
  } else {
    println("User selected " + selection.getAbsolutePath());
    fileGlobal = selection.getAbsolutePath();
  } // else
}//func 

// ----------------------------------------------------------------
// Input 

void mousePressed() {
  // check Buttons
  if (state==stateDone||state==stateBreak||state==stateWaitForButton) {
    if (dist(mouseX, mouseY, 133, 133) < 66) {
      // reset / restart
      fileGlobal=""; 
      selectInput("Select a file (Cancel to abort):", 
        "fileSelected");
      state=stateWaitForFile;
    }//if
    else if (dist(mouseX, mouseY, 233, 133) < 66) {
      exit();
    }//else if
  } //if
  //
} //func 

//-------------------------------------------------------------------
//Tools

void showButtons() {
  // set mode for text and rect 
  textAlign(CENTER, CENTER);
  rectMode(CENTER);

  // rects 
  noFill();  
  rect(133-5, 133+3, 84, 23);
  rect(233-1, 133+3, 84, 23);

  // text 
  fill(255); 
  text ( "Choose file", 133, 133);
  text ( "Quit", 233, 133);

  // reset 
  textAlign(LEFT);
  rectMode(CORNER); // The default mode is rectMode(CORNER)
}//func 
//

```

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [April 5, 2021, 7:06pm UTC](https://discourse.processing.org/t/is-there-a-file-open-possibility-inside-of-a-sketch/29102/7 "2021-04-05T19:06:52Z")

</div>

> [@paulgoux](#):
>
> ```auto
> String folderSelected(File selection) {
> String s = null;
> if (selection == null) {
> println("Window was closed or the user hit cancel.");
> } else {
> println("User selected " + selection.getAbsolutePath());
> s = selection.getAbsolutePath();
> }
> return s;
> };
> 
> ```

Paul,  
Thanks for the example. I ran it but I get “file cannot be resolved as a variable” and this line is highlighted

```
  file.value = selection.getAbsolutePath();

```

Chrisir,  
Hallo,

Thanks a lot again, the code works well. I will adapt it to my needs. I will work on loading from OneDrive later.

Mitch
