# How can i add a "Open with" function?

**URL:** https://discourse.processing.org/t/how-can-i-add-a-open-with-function/41062
**Category:** Processing
**Created:** [February 25, 2023, 5:16pm UTC](https://discourse.processing.org/t/how-can-i-add-a-open-with-function/41062 "2023-02-25T17:16:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)
#### Post date: [February 25, 2023, 5:16pm UTC](https://discourse.processing.org/t/how-can-i-add-a-open-with-function/41062/1 "2023-02-25T17:16:27Z")

</div>

Is it possible to make with processing, that when i install a programm, there is a function like “open with app” dialog. so i could set in windows settings, that the processing app will be used to open images.

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [February 26, 2023, 9:05am UTC](https://discourse.processing.org/t/how-can-i-add-a-open-with-function/41062/2 "2023-02-26T09:05:15Z")

</div>

Hi @Aryszin,

you mean by install that want to make an executable from your sketch ie. MySketch.exe on a specific location on disk ?

If so you can do for example the following to get the command line parameter on calling the sketch executable.

```auto
PImage myImage;
String myDefaultImageName = "noimage.jpg"; // should be a valid and existing image in data folder
String myImageName;

void settings() {
  if (args == null) {
    myImageName = myDefaultImageName;
  } else {
    myImageName = args[0];
  }
  myImage=loadImage(myImageName);
  if (myImage == null) {
    myImageName = myDefaultImageName;
    myImage=loadImage(myImageName);    
  }
  size(myImage.width, myImage.height);
}

void setup() {
  surface.setTitle(myImageName);
}

void draw() {
  image(myImage, 0, 0);
}

```

Assuming you already have build the executable from your sketch and it processes the command line argument similar as shown above, you can start your sketch executable by  
`...\MySketch.exe <full_path_to_image>`.

_Please note that you need to set the working directory to your MySketch.exe path otherwise it wouldn’t start the MySketch.exe properly. For that it could be necessary to wrap everything in a batch file to make a cd before calling the executable_

ie run.bat content:

```auto
cd /D <path_to_MySketch.exe>
MySketch.exe %1

```

Hope that helps …

Cheers  
— mnse

Examples  
`run.bat`  
 ![example1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/0/8088e28eb78ac0662fb2a467c7eb2893b72641bf.png)

`run.bat <path_to_processing_logo>`

 ![example2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/6/e621a63e031b8771bcf4adc12199416a3e98f133.png)

PS: Error handling could be refined to your needs (ie. exit if not a valid image instead of showing noimage)

PPS: if you don’t want to create an executable but want to call the sketch by processing-java.exe, you can call the above example by …

```auto
<path_to_processing>\processing-java.exe --sketch="<path_to_your_sketch_folder>" --run "<path_to_the_image_file>"

```

---

<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: [February 26, 2023, 10:24am UTC](https://discourse.processing.org/t/how-can-i-add-a-open-with-function/41062/3 "2023-02-26T10:24:32Z")

</div>

Hello @Aryszin,

> [@Aryszin](#):
>
> Is it possible to make with processing, that when i install a programm, there is a function like “open with app” dialog. so i could set in windows settings, that the processing app will be used to open images

This [no-frills](https://www.merriam-webster.com/dictionary/no-frills) Processing sketch will open a text file with “notepad.exe” on Windows 10.

Code:

```auto
String skPath;
String txtFileName;

void setup() 
  {
  size(200, 200);
  }

void draw() 
  {
  // draw() must be present for mousePressed() to work
  String txtFile[] = {"Example text file",
                       " Short and sweet."
                      };
  skPath = sketchPath("");
  txtFileName = "text.txt";
  saveStrings(txtFileName, txtFile);              
  }

void mousePressed() {
  println("Opening Process_4");
  launch("notepad.exe" + " " + skPath + txtFileName);
}

```

The above is my initial interpretation of your post without the "open with app” dialog.

References:

> **[launch() / Reference](https://processing.org/reference/launch_.html)**
>
> Attempts to open an application or file using your platform's launcher. The filename parameter is a String specifying the file name and location. The location parameter must be a full path name…

`:)`

---

<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: [February 26, 2023, 8:17pm UTC](https://discourse.processing.org/t/how-can-i-add-a-open-with-function/41062/4 "2023-02-26T20:17:05Z")

</div>

In addition to mnse: in the win dialog you can choose your exe file you created with processing and check the mark for always use this app if I remember correctly

---

<div class="post-metadata">

### Author: ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)
#### Post date: [February 27, 2023, 4:31pm UTC](https://discourse.processing.org/t/how-can-i-add-a-open-with-function/41062/5 "2023-02-27T16:31:21Z")

</div>

thank you! does it als give an option in file explorer like “open with”? thanks

---

<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: [February 27, 2023, 5:17pm UTC](https://discourse.processing.org/t/how-can-i-add-a-open-with-function/41062/6 "2023-02-27T17:17:37Z")

</div>

Right mouse click on a file and select  
open with  
From the local menu (context menu) ?

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [February 27, 2023, 6:30pm UTC](https://discourse.processing.org/t/how-can-i-add-a-open-with-function/41062/7 "2023-02-27T18:30:37Z")

</div>

Hi,  
Just an addition…

Unfortunately there are some difficulties using the “Open with” Explorer feature. Even as you can choose the Sketch executable it wouldn’t run as expected as for the sketch executable it is mandatory to have the working directory set to the executable path to run properly but this isn’t the case when Explorer starts the process for the sketch executable by using “Open With…”.  
As a workaround one can use a batch file as described above and run that by “Open With…”

Cheers  
— mnse
