How to use arguments with an application built with processing ?

edited February 2017 in How To...

I read in this thread : http://forum.processing.org/two/discussion/comment/22707#Comment_22707

that "Processing's PApplet has a String[] field called args", so I'm wondering if anyone has managed to do an application that can use an argument on startup :

I mean I would like to double click on a gif and open a custom program made with processing to display it. Has anyone done this before ?

Answers

  • edited July 2014 Answer ✓

    I would like to double click on a ".gif" and open a custom program made with Processing to display it.
    Has anyone done this before?

    Dunno! I imagine that a ".gif" mime should be created to point to the app's launcher.
    Also, it gotta pass the file's path as the argument, so the app would know where the ".gif" is!

    I did a very simple args mock-up. Hit CTRL+E in order to export it.
    Then we can run it via command line or from a shortcut pointing to it.
    Don't forget to pass arguments to it separated by an empty space:

    /**
     * CommandLineTest (v1.0)
     * by GoToLoop (2014/Jul)
     *
     * forum.processing.org/two/discussion/6457/
     * how-to-use-arguments-with-an-application-built-with-processing-
     *
     * forum.processing.org/two/discussion/316/processing-command-line
     */
    
    println("\n# of args: " + args.length + "\n");
    println(args);
    println();
    exit();
    
  • edited February 2016

    Sorry for the very slow reply but yes it works, here is some sample code :

    PImage img;
    
    void setup() {
      size(800, 400);
      textSize(15);
      if (args.length != 0) {
        img = loadImage(args[0]);
      }
    }
    
    void draw() {
    
      text("\n# of args: " + args.length + "\n", 20, 25);
      for (int i = 0 ; i < args.length ; i++) { 
        text(args[i], 20, 75+ i*50);
      }
      if (args.length != 0) {
        image(img, 0, 0);
      }
    }
    

    If you export this code as an application, you can now image open files with your custom p5 app :) !

    So this lets you open an image rubbishly because it doesn't resize, and doesn't do anything but showing the idea.

    It's interesting to note, that when you run your sketch in processing IDE you always have one argument (the sketch path), but when you use the command line or just double click on your app it disappears leaving you with no arguments.

    Thanks GoToLoop !

  • edited October 2014

    ... or just double click on your app it disappears leaving you with no arguments.

    You gotta setup your Processing loadImage() sketch as an app which launches images in the OS.
    For example, right-click an image file and choose to "Open With" your sketch.
    That'll establish the sketch as the MIME for that file type.
    So hopefully, the OS will pass the file's path as the 1st argument for args[0]. >-)

  • yes definitely, it works perfectly

    i was just pointing out, that when using the ide you have the sketch path by default as args[0], but not when your application has been exported : args[0] is empty unless you specify an argument (that's why I have tests when using args)

  • edited February 2016

    Does this still work in the newest version of processing? I get an application that fails to load when attempting to open the image from explorer using "open with". (Using the code berenger posted above)

  • edited March 2016

    Does this still work in the newest version of processing?

    P3 still got args. And in latest version 3.0.2, args is available as soon as settings().

    (Using the code @berenger posted above)

    Have you exported the sketch before attempting "Open with..."?
    Also, use my own example, which is simpler, before @berenger's.

  • @GoToLoop Your code does seem to work just fine for passing in arguments from the cmd line. I was able to replicate this. I'm curious how @berenger was able to get the filepath of the image to be passed into args[0] upon "open with". I used his code and exported it to an app. Selected an image using windows explorer,right click, "open with", and selected the newly created app.exe file. I get nothing. It fails to open the app at all.

  • edited February 2016

    ... but not when your application has been exported...
    I'm curious how @berenger was able to get the filepath of the image to be passed into args[0] upon "open with".

    After re-reading his post, it doesn't seem like he was successful.
    And I'm clueless about the innards of how OSes deal w/ passed parameters to apps.

    Have you tried exporting my example and used it w/ "Open with..."?
    If args.length is more than 0, then something was passed... :-\"

    Otherwise, try to add %1 at Target's path in the shortcut's properties.
    I've heard about that %1 represents the 1st argument passed. :(|)

  • edited February 2016

    Thanks @GoToLoop for your quick responses. Hopefully @berenger will jump in and clarify whether or not his code truly worked for him as an exported application. Per his above post:

    If you export this code as an application, you can now image files with your custom p5 app !<

    This makes me think it worked for him somehow.

  • I can confirm it works fine here with processing 3.0.2, tested on win 8.

    I tried this morning again, and had some troubles. I had to fiddle with the compatibilities settings of the exported app. (right click -> compatibility)

    I ended up changing the parameters for all users, and ticked - compatibility with windows xp sp3 - turn off 'display scaling at high resolution' - run as administrator

    those are not the right translation (my system is not in english, so I'm not sure what they are in english).

    All those are probably not necessary, you could pin point what is really needed to make it work.

    cheers

  • Thanks for the input @berenger . I stll have not gotten this to work. I've tried the app on many variations of compatibility settings and on Windows 10 as well as Windows 7 to no luck. I can get it to work by sending the filepath to the image as an arg in cmd line, but not if I open an image file directly in explorer using "open with" ... "myImager" application.

  • Update: @berenger @GoToLoop Strangely, if I create a shortcut of the imagerapp.exe I can drag an image file and drop it on the app's icon. This opens the app and loads the image using args[0] for the filepath of the image being dropped. It works under any compatibility settings; however, this does not work with the original imagerapp.exe file, it must be a shortcut of it. Per the original goal, attempting to define the .exe as default application and opening still does not work for me under any of the compatibility settings, shortcut or not.

  • edited March 2016

    ... attempting to define the .exe as default application and opening...

    You need to redefine the MIMEs for ".jpg" & ".png" to be opened by your "imagerapp.exe" aFaIK.
    Sorry i dunno much else than that. :-S

  • edited February 2017

    @GoToLoop just asking, but what is MIME? And how would you go about redifining the MIMEs for a particular extension?

  • @tiej1990 Exact same problem here. I'll try that shortcut method and see if it works.

  • edited September 2017

    @GoToLoop -- you mentioned that args is still present in Processing 3.

    I'm having a problem getting it to work. Currently in Processing 3.3.5 this sketch:

    void setup(){
      // detect command line invocation
      if (args.length != 0) {
        println("args!");
      }
    }
    

    ...is giving me "NullPointerException."

    The weird thing is that the IDE thinks that args is a String[] -- but it won't let me check it's length. I'm a bit mystified.

  • ...sigh. Never mind! args is null (not empty) when there are no args.

    void setup() {
      // detect command line invocation
      if (args != null) {
        if (args.length != 0) {
          println("args!");
        }
      }
    }
    

    There was actually something recent about that here:

    https://forum.processing.org/two/discussion/23924/handling-command-line-arguments-in-a-sketch

  • It can be a String[] and be null

  • What operating system is this?

  • You were right koogs -- it was null. (OS X)

Sign In or Register to comment.