How to view graphics file on non-mac

On the Mac, I can use the “open” program to open an image file such as png. The
program is non-blocking, so the Seq("open"....)! returns immediately.

Can someone suggest how to update this function so that it will also work on linux and windows?
Even better if there is a way to do this, non-blocking, directly with scalafx.

  def openImageFile(path:Path):Unit = {
    import sys.process._
    if ("Mac OS X" == System.getProperty("os.name")) {
      println(s"open $path")
      Seq("open", path.toString).!
    } else
      println("[warning]: don't know how to display graphical image in OS: "+ System.getProperty("os.name"))
  }
1 Like

see Desktop (Java Platform SE 8 )

2 Likes

Looks like the following works:

  def openImageFile(path:Path):Unit = {

    val file:java.io.File = path.toFile

    java.awt.Desktop.getDesktop().open(file)
  }

As indicated in another thread, will this solution work on Linux?

It should. I recall it working on the Linux systems I personally tried it on, but this was 10+ years ago. (Though one hopes the situation would have only improved since then, not regressed…)

It works on linux (I just checked on Ubuntu 20.04). The following ammonite session opens the png file.

@ import java.io._ 
import java.io._

@ val file = new File("aabab.png") 
file: File = aabab.png

@ java.awt.Desktop.getDesktop().open(file)

regards
Siddhartha

1 Like

As long as you have X11 (or equivalent stubs) installed.

1 Like