# Is it possible to open a file with processing sketch? (command line)

**URL:** <https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613>\
**Category:** Coding Questions\
**Created:** [December 30, 2020, 8:16am UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613 "2020-12-30T08:16:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brastin](https://avatars.discourse-cdn.com/v4/letter/b/c5a1d2/32.png) [@Brastin](https://discourse.processing.org/u/Brastin)\
**Post date:** [December 30, 2020, 8:16am UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/1 "2020-12-30T08:16:49Z")

</div>

I would like to create something like a text editor, and allow the user to open text files with the text editor, rather than having to open the editor first. Is this possible? I’ve tried to achieve this before but I haven’t had any luck and I haven’t been able to find any information on this issue.

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [December 30, 2020, 9:44am UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/2 "2020-12-30T09:44:37Z")

</div>

You can use `selectInput() ` to open a platform-specific file chooser dialog to select a file for input. After the selection is made, the selected File will be passed to the ‘callback’ function

a crude text editor might be achieved by loading the file’s strings with `loadStrings()` and then it’s up to you to write the associated text editor

You could also use a `BufferedReader ` to read line by line the file

> **[selectInput() \\ Language (API) \\ Processing 3+](https://processing.org/reference/selectInput_.html)**

> **[loadStrings() \\ Language (API) \\ Processing 3+](https://processing.org/reference/loadStrings_.html)**

> **[BufferedReader \\ Language (API) \\ Processing 3+](https://processing.org/reference/BufferedReader.html)**

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 30, 2020, 2:02pm UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/3 "2020-12-30T14:02:22Z")

</div>

Hello,

Exporting applications:

- [https://github.com/processing/processing/wiki/Export-Info-and-Tips](https://github.com/processing/processing/wiki/Export-Info-and-Tips)
- [https://happycoding.io/tutorials/processing/exporting-applications](https://happycoding.io/tutorials/processing/exporting-applications)

Related:

- [Text editor base?](https://discourse.processing.org/t/text-editor-base/23822)

`:)`

---

<div class="post-metadata">

**Author:** ![Brastin](https://avatars.discourse-cdn.com/v4/letter/b/c5a1d2/32.png) [@Brastin](https://discourse.processing.org/u/Brastin)\
**Post date:** [January 4, 2021, 1:50pm UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/4 "2021-01-04T13:50:07Z")

</div>

Sorry, I worded this pretty badly initially.  
I’ve made a text editor already, I know how to load and save files, within the sketch/application.  
What I want to be able to do is set my sketch as the default program for a file to open in. Like on windows for example, the default program for a text file is notepad. I would like to know if it’s possible to change that to my processing sketch so it can be used much easier as a text editing application.

---

<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:** [January 4, 2021, 1:58pm UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/5 "2021-01-04T13:58:56Z")

</div>

> [@Brastin](#):
>
> What I want to be able to do is set my sketch as the default program for a file to open in.

in Windows:  
You need to export your Sketch as an exe

Then in Windows right click a txt-file, Choose Open With…

- choose your Sketches exe
- and check **Option Box** use this always

---

<div class="post-metadata">

**Author:** ![Brastin](https://avatars.discourse-cdn.com/v4/letter/b/c5a1d2/32.png) [@Brastin](https://discourse.processing.org/u/Brastin)\
**Post date:** [January 4, 2021, 2:01pm UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/6 "2021-01-04T14:01:05Z")

</div>

How would I configure my processing sketch to recognise that I have opened it with the context of the selected document though? I’ve managed to set it as a default before but I haven’t found a way to actually get it to open a document that way.

---

<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:** [January 4, 2021, 2:26pm UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/7 "2021-01-04T14:26:22Z")

</div>

When you selected the Option Box “always use this app”  
Windows should always use your Sketch to open ANY txt file.

It’s a matter of Windows not of your Sketch.

> [@Brastin](#):
>
> to recognise that I have opened it with the context of the selected document though

example from the forum

```auto

//https://forum.processing.org/two/discussion/23999/detect-command-line-invocation-of-a-sketch
// 2019-08-25
// Modified to check for external argument only!

/**
 * CommandLineDetect
 * 2017-09-21 Jeremy Douglass - Processing 3.3.6 
 * Was a sketch run from PDE, or from the command line?
 * If from the command line, what are its arguments and their values?
 * 
 * Invoke it:
 * 1. from the command line above the sketch folder, with no arguments:
 * processing-java --sketch=`pwd`/CommandLineDetect --run
 * 2. ...or with arguments:
 * processing-java --sketch=`pwd`/CommandLineDetect --run foo bar=baz lorem="ipsum dolor sit amet, consectetur adipiscing elit" etc 
 * 3. or form PDE
 *
 * The sketch relies on the --external flag to detect a PDE launch, as per:
 * - processing.github.io/processing-javadocs/core/processing/core/PApplet.html#main-java.lang.String:A-
 * 
 * The command line passes in a String[] of args. If you wish to arguments of the:
 * - form foo=bar
 * - lorem="ipsum dolor"
 * 
 * Related forum discussions:
 * - forum.processing.org/two/discussion/23999/detect-command-line-invocation-of-a-sketch
 * - forum.processing.org/two/discussion/23924/handling-command-line-arguments-in-a-sketch
 * - forum.processing.org/two/discussion/6457/how-to-use-arguments-with-an-application-built-with-processing
 */

// Some editing by GLV. See original (above) for the complete version.

boolean PDE;
StringDict argDict = new StringDict();
color setBack = 0;
String str1;

boolean lorem = false; 

void setup() 
{

  size (770, 770); 

  // Local command-line arguments for testing:  
  // String [] args = {"external", "foo", "bar=baz", "lorem=ipsum dolor sit amet, consectetur adipiscing elit", "etc"};

  println(args == null ? "null" : args);
  if (args == null) 
    str1 = "internal";
  else
    str1 = args[0];

  if (str1.equals("external")) 
  {
    PDE=true;
    setBack = color(0, 255, 0);
  } else 
  {
    PDE=false;
    setBack = color(255, 0, 0);
  }

  // load command line arguments, load them
  if (args != null) 
  {
    for (String arg : args) 
    {
      String[] entry = split(arg, "=");
      if (entry.length == 1) 
      {
        argDict.set(entry[0], "");
      }
      if (entry.length == 2) 
      {
        argDict.set(entry[0], entry[1]);
      }
    }
  }

  // set default if specific arg not present
  if (!argDict.hasKey("xyzzy")) {
    println("Setting default for xyzzy");
    // ...do something
  }

  // demonstrate using arguments
  for (String arg : argDict.keyArray()) {
    switch(arg) {

    case "external":
      //arg external
      println("mode:", arg);
      break;
      //case "foo":
      // // key only arg
      // println("mode:", arg);
      // // ...do something
      // break;
    case "bar":
      // key=value arg
      println("Setting", arg, "=", argDict.get(arg));
      // ...do something
      break;
    case "lorem":
      // parsed value
      String parsed = join(sort(argDict.get(arg).split(" ")), " ");
      println("Sorting", arg, "=", parsed);
      lorem=true; 
      // ...do something
      break;
    }
  }
  //exit();
}

void draw()
{
  background(setBack);
  if (lorem)
    text("lorem", 20, 20);
}

```

> [@Can't detect PDE vs command line, PDE no longer passing --external](https://discourse.processing.org/t/cant-detect-pde-vs-command-line-pde-no-longer-passing-external/10508/8):
>
> Thanks for bringing this to the topics! I will be using processing command-line in the future. This is my first pass at this and “green” background from command-line launch and “red” from PDE launch: //https://forum.processing.org/two/discussion/23999/detect-command-line-invocation-of-a-sketch // 2019-08-25 // Modified to check for external argument only! /\*\* \* CommandLineDetect \* 2017-09-21 Jeremy Douglass - Processing 3.3.6 \* Was a sketch run from PDE, or from the command line? \* If f…

use batch file (name c1.bat) to test

content

`start sketch_210104b.exe "lorem=ipsum dolor sit amet, consectetur adipiscing elit"`

see also [Can't detect PDE vs command line, PDE no longer passing --external](https://discourse.processing.org/t/cant-detect-pde-vs-command-line-pde-no-longer-passing-external/10508)

---

<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:** [January 4, 2021, 2:27pm UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/8 "2021-01-04T14:27:57Z")

</div>

> [@Brastin](#):
>
> How would I configure my processing sketch to recognise that I have opened it with the context of the selected document though

Does it work now?

Chrisir

---

<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:** [January 4, 2021, 3:24pm UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/9 "2021-01-04T15:24:50Z")

</div>

new version

```auto
//https://forum.processing.org/two/discussion/23999/detect-command-line-invocation-of-a-sketch

/**
 * CommandLineDetect
 * 2017-09-21 Jeremy Douglass - Processing 3.3.6 
 * 
 * Related forum discussions:
 * - forum.processing.org/two/discussion/23999/detect-command-line-invocation-of-a-sketch
 * - forum.processing.org/two/discussion/23924/handling-command-line-arguments-in-a-sketch
 * - forum.processing.org/two/discussion/6457/how-to-use-arguments-with-an-application-built-with-processing
 */

// Some editing by GLV & Chrisir. See original (above) for the complete version.

color setBack = 0;
String str1;

void setup() {

  size (770, 770); 

  // Local command-line arguments for testing:  
  // String [] args = {"external", "foo", "bar=baz", "lorem=ipsum dolor sit amet, consectetur adipiscing elit", "etc"};

  if (args == null) {
    println("args is null"); 
    str1 = "no args / internal";
  } else {
    printArray(args); 
    str1 = args[0];
  }//else
}//func 

void draw() {
  background(setBack);
  text( str1, 20, 20 );
}
//

```

keywords: command line, cmd, cmdline, args, arguments, starting, start of Sketch, environment

---

<div class="post-metadata">

**Author:** ![Brastin](https://avatars.discourse-cdn.com/v4/letter/b/c5a1d2/32.png) [@Brastin](https://discourse.processing.org/u/Brastin)\
**Post date:** [July 29, 2021, 1:18pm UTC](https://discourse.processing.org/t/is-it-possible-to-open-a-file-with-processing-sketch-command-line/26613/10 "2021-07-29T13:18:53Z")

</div>

sorry for the incredibly late reply, I had moved on to another project and forgot about this thread. But this works! and it is exactly what I needed, thank you so much. 🙂
