Is it possible to check for operating system?

I have written a piece of code for Windows, and would like to use almost entirely the same piece of code for Android, just with a few minor changes (in particular screen size and mouse activity). Instead of copying the code over to APDE and making the changes there, is it possible to check which operating system is running and apply the appropriate logic so that I can use the same code for each? Thanks

1 Like
  println( "System     : " + System.getProperty("os.name") + "  " + System.getProperty("os.version") + "  " + System.getProperty("os.arch") );

try on different systems and look for a string to compare

1 Like

Here is an example of how to use that to run platform-specific setup and configuration – and store a convenient global variable with the value you are looking for:

String osType;

void setup() {
  osType = osSetup();
  println(osType);
}

String osSetup () {
  String os = System.getProperty("os.name");
  if (os.contains("Windows")) {
    // os-specific setup and config here
    return "win";
  } else if (os.contains("Mac")) {
    // ...
    return "mac";
  } else if (os.contains("Linux")) {
    // ...
    return "linux";
  } else {
    // ...
    return "other";
  }
}

Note that your case detection can be anything – win32/64, or checking for a particular linux version that you know doesn’t work, or making it only “mac” vs “other”. A previous example:

2 Likes

Thanks @kll !
:+1: :+1: :+1:

That’s fantastic @jeremydouglass, thanks for the help :smile: