How can i add a "Open with" function?

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.

1 Like

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.

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:

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

Hope that helps …

Cheers
— mnse

Examples
run.bat
example1

run.bat <path_to_processing_logo>

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 …

<path_to_processing>\processing-java.exe --sketch="<path_to_your_sketch_folder>" --run "<path_to_the_image_file>"
2 Likes

Hello @Aryszin,

This no-frills Processing sketch will open a text file with “notepad.exe” on Windows 10.

Code:

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:

:)

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

1 Like

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

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

1 Like

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

3 Likes