Using args[] for opening files

I am working on a program that I want to be able to open files with by double-clicking on the file and choosing “open with…” - name of exported sketch. I know the program can open the files because it opens them just fine when I use the open button that I put into the GUI. I can also open files if I make a shortcut to the exported sketch and drop the file onto it, but it doesn’t do anything when I just double-click the file choosing “open with…” - name of exported sketch. It doesn’t even open the app.

Here’s the args[] code:

if(args[].length != null){
openTab(args[0]);
}

And the openTab function (this works):

void openTab(String path){
//open code
}

I’m using processing 3.3.7 on Windows 7.

Any suggestions? Thanks.

2 Likes

I’ve looked at all of those posts. the only one that sort of covered it was https://forum.processing.org/two/discussion/6457/how-to-use-arguments-with-an-application-built-with-processing, and this post did not get resolved.
I checked the forums before I posted the questions.

Based on your post, are you using Windows as your OS?

So if I understand correctly, you created some code, you exported it so to get the executable version. Now, you go to your fav file browser, find a file that your program can read and then you try to open by right clicking in the file and selecting the open with… option. However, after selecting your program it does not work. Is this right?

Kf

Yes, that is correct.

I guess you are saying yes to both of my questions.

Some references, related but likely unhelpful:

https://forum.processing.org/two/discussion/17102/cannot-set-processing-sketch-as-default-program-for-opening-test-files#latest
https://superuser.com/questions/655600/i-cant-set-a-program-as-the-default-to-open-a-filetype-with-in-windows-7
https://forum.processing.org/two/discussion/5372/my-exported-application-won-t-run

Assume you have your own defined file extension. I did my test using .axx and it is executed with an executable generated by Processing via export. The executable is called openWithDEMO.exe. This is what I did:

  1. Open cmd as administrator. One way is to click start/windows logo, type cmd and then press ctrl+shift+ENTER
  2. Then type assoc .axx=axxFiles
  3. Then type ftype axxFiles=C:\Users\kfrajer\Documents\Processing\CMsketches\untested\openWithDEMO\application.windows64\openWithDEMO.exe %1 %*

Now if I dbl click any .axx app, it opens the axx file with my executable app generated by Processing via export. This was tested in Windows 10. Two more things for you to consider:

  1. In the command prompt, type ftype /? to read some of the documentation related to this process.
  2. If you want your Processing exported application to open a file that is known by the current OS (Windows in this case), you will need to read more about it. For instance check this [post]{https://superuser.com/questions/361816/pass-command-line-arguments-to-windows-open-with} from another forum.

By the way, this is my testing code:


final String FILENAME = "c:\\mySandBox\\output.txt";

void setup() {
  size(400, 600);

  fill(255);
  strokeWeight(2);

  surface.setTitle(args==null?"No args":(args.length==0?"args.len=0":args[0]));
  openTab(args);
}

//===========================================================================
// OTHER FUNCTIONS:

void openTab(String[] path) {


  String[] lines = {"FAIL"};  //In case no argument is provided

  if (path!=null && path.length>0) {
    lines = path;
  }

  saveStrings(FILENAME, lines);
}

Kf

3 Likes

Is there any way to put that into a batch file? Like to put into my app’s installer to automatically associate the file extension

I don’t know. I guess it is possible. Probably this is what of the few things that software installers do. They unpacked the files, create proper associations and define uninstallers instructions. You will need to do some reading on your own as I don’t have experience doing this.

You could create a bat file and provide instructions to run it. Remember you need to run this as admin. The instructions provided above were only tested in Win10. You will need to test these instructions in Win 7/8. Lastly, make sure you do proper testing, including managing 32 and 64 bit platforms separately and research in how to undo these changes in case the user needs or wants to remove them.

Kf

Yeah, I’ll test it. I think I’ll make a ps1(powershell) script with those commands
and just call it in the setup function of the app. Or since I’m using Inno Script Studio for creating the installers, maybe I can make post-install commands. Thanks for all the help.

OK, I just tested it and the problem persists. The app still won’t even open up.

Sorry, what did you test? assoc and ftype command? You follow the instructions and it dodn’t work? What is your OS including machine arch.

Kf

That is what I did. My computer is a 64-bit HP computer running Windows 7. I’m not sure what exact version of Windows it is, but I could find out if you needed to know.
I was able to open the file if it was in the same directory as the executable program, but if it was in a different directory; i.e. “documents”, it wouldn’t open them, Not even with the executable’s directory added to the system PATH.

@REAS @fry Is it possible that this is a bug in processing? What args[] does processing send to the sketch?

I don’t think it is a bug in Processing as it works in my machine. It might be a bug in the OS. Ok, wait, I shouldn’t be talking bad about Windows. Let me put it in another way. The way I implemented was tested only on Win 10 OS and it is not guaranteed to work in other Windows OSes. I believe you have to do your own research to try to run it properly in Win 7. After all, my attempt above was my own research on my own time.

If I had a Win7 machine, I would give it a try and see if I could figure it out. Unfortunately, I do not own one.

Can you show how you were trying to do this in the command line when running your executable? Also, can you show your code that shows how you handle this file input by the user? For instance, are you using the selectInput() function?

Kf

@kfrajer

public void setup(){
/* setup code */
    
 if(args != null){
  if(args.length > 0){
  openTab(args[0]);
  }
 } 
 
  }

void openTab(String sel){
  path = sel;
 json = loadJSONObject(sel);
  Song = json.getString("Song");
  Artist = json.getString("Artist");
  Album = json.getString("Album");
  Genre = json.getString("Genre");
  Description = json.getString("Description");
  timing = json.getInt("Timing");
  compTab = json.getString("CompTab");
  measure = json.getInt("Measure");
  sldestr = Integer.parseInt(json.getString("sldestr"));
  chordOn = json.getBoolean("chordOn");
  sep = json.getString("sep");
  string[0] = json.getString("string[0]");
  string[1] = json.getString("string[1]");
  string[2] = json.getString("string[2]");
  string[3] = json.getString("string[3]");
  string[4] = json.getString("string[4]");
  string[5] = json.getString("string[5]");
  chord[0] = json.getString("chord[0]");
  chord[1] = json.getString("chord[1]");
  chord[2] = json.getString("chord[2]");
  chord[3] = json.getString("chord[3]");
  chord[4] = json.getString("chord[4]");
  chord[5] = json.getString("chord[5]");
  tuningin[0] = json.getInt("tuning[0]");
  tuningin[1] = json.getInt("tuning[1]");
  tuningin[2] = json.getInt("tuning[2]");
  tuningin[3] = json.getInt("tuning[3]");
  tuningin[4] = json.getInt("tuning[4]");
  tuningin[5] = json.getInt("tuning[5]");
  surface.setTitle("Guitar Tab Editor v" + version + " - " + path);
  str1.setSelected(tuningin[0]);
  str2.setSelected(tuningin[1]);
  str3.setSelected(tuningin[2]);
  str4.setSelected(tuningin[3]);
  str5.setSelected(tuningin[4]);
  str6.setSelected(tuningin[5]);
  beat.setText(str(timing + 1));
  meas.setText(str(measure + 1));
  sngnme.setText(Song);
  art.setText(Artist);
  alb.setText(Album);
  gnr.setText(Genre);
  descrip.setText(Description);
  updateTuning();
  updateTab();
  upmdevnt(upmd,GEvent.CLICKED);
 
}

That is the code I was using.

I wasn’t running it from the command-line, I was double clicking the file after creating the the file association the way you showed.

Another thing is that i don’t know what parameters Windows sends to the executable when you double-click a file, e.g. the filename, the path, or the filename and then the path, though this,

should resolve that.