8

If I have a file system path can I open a window in Explorer (on windows) or in Finder (on OS X) displaying the folder that the path leads to?

Cookie points for answers that are cross-platform and/or plugin-less.

Jakub Hampl
  • 39,863
  • 10
  • 77
  • 106

2 Answers2

3

For node-webkit (NW.js) users you can use...

var gui = require("nw.gui");

document.querySelector("[data-location]").onclick = function() {
  gui.Shell.showItemInFolder(__dirname + '/content/project/' + this.textContent);
};
Michael Schwartz
  • 8,153
  • 14
  • 81
  • 144
2

You need to be able to run a new process from a browser. There are a few ways to do this. I'll show the JNLP way to do this.

Create a jnlp file as follows:

    <?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://example/" href="jnlpTest.jnlp">
    <information>
        <title>Some Title</title>
        <vendor>Some Vendor</vendor>
        <homepage href="http://example/" />
        <description>Some Description</description>
    </information>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.6+" />
        <jar href="jnlpTest.jar" />
    </resources>
    <application-desc main-class="MainClass" />
</jnlp>

Create a jnlpTest.jar from the following:

public class MainClass {
    public static void main(String args[]) {
        Runtime rt = Runtime.getRuntime();
        try {
            //TODO - different exec for Mac
            rt.exec("explorer.exe");
        } catch (IOException e) {
            //exception
        }

    }
}

With a Manifest:

Manifest-Version: 1.0
Main-Class: MainClass

Sign your JNLP jar:

keytool -genkey -keystore testKeys -alias jdc
jarsigner -keystore testKeys jnlpTest.jar jdc

place both the jar and jnlp file on a web server. Make sure the mime type JNLP is served as application/x-java-jnlp-file.

Reference for making a JNLP: http://java.dzone.com/articles/java-web-start-jnlp-hello

Now when a user clicks your jnlp link they will download the jar and be asked if it is ok to run. Running it will cause the explorer window to open. I know it's not the best solution, but any solution will require asking the users permission to execute code on their machine.

Daniel Moses
  • 5,872
  • 26
  • 39
  • Something this vague should really be a comment, not an answer. I'd also be very very surprised if the Mac port of Silverlight includes COM. – millimoose Feb 28 '12 at 16:16
  • I started putting this in a comment, but I did want to add a little more information than just "try this url". Here is a starting point if that helps, if not ignore my answer. – Daniel Moses Feb 28 '12 at 16:18
  • @DMoses The linked article says in the beginning: "It should be noted that these features are only available in elevated privileges Out-Of-Browser mode and are not available in-browser." Are you absolutely relevant to this question? – Jakub Hampl Feb 28 '12 at 16:19
  • 1
    Well, the edit is less vague, but it looks like you're misunderstanding what the OP wants. I believe it's not "how to read a file on the filesystem", but "how to open a file manager window" – millimoose Feb 28 '12 at 16:22
  • Ahh in that case i'll change my answer appropriatly. – Daniel Moses Feb 28 '12 at 16:23
  • If you are going down that path, on OS X, you probably want to use `open`. So, instead of explorer.exe, `open ~/Documents/` or whatever. – Tom Morris Dec 10 '12 at 18:28
  • I've tried this with a more recent example (below) and I found it was blocked. I don't manage the security settings so there may be a way around it if you have control (?) but I think not after Java 8 update 20 (according to the message). Worth checking before anyone gets too involved with this solution. For example, I'm not going to get an external cert for a little app to open windows explorer! (http://examples.javacodegeeks.com/java-basics/web-start/java-web-start-getting-started/) – gringogordo May 13 '15 at 14:58