This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Mac OS and the fly likes Open a folder in finder in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Mac OS
Bookmark "Open a folder in finder in Java" Watch "Open a folder in finder in Java" New topic
Author

Open a folder in finder in Java

Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
Hi all,

I'm looking for a way to open a finder window to a file path specified in a string but I don't know Mac well enough to know the correct way to do this. In windows I use:



What is the equivelant native code on apple to do the same thing?

I tried varients of



but it didn't work.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

I don't know if there is an easier way to do this, however you can use the AppleScript programming language to develop a script on the fly which can then be executed. For example, to open my home folder within the finder, I could do something like:

Note that this is building a single executable command line. Normally each of those options would have been a single line in a script:


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
I was hoping for something which I could intergrate into my program so I dont need any external files, is it possible to do this from command line? This way I could call it useing Runtime.getRuntime().exec.

Also the path to the folder is not always teh same and it can be pretty deep down the directory tree so it would be a pain to code somethign to write the applescript file.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Colin McTaggart wrote:I was hoping for something which I could intergrate into my program so I dont need any external files, is it possible to do this from command line? This way I could call it useing Runtime.getRuntime().exec.

Which is why I showed the first version, which is a "single executable command line". The second version is the script that does the same thing.

Colin McTaggart wrote:Also the path to the folder is not always teh same and it can be pretty deep down the directory tree so it would be a pain to code somethign to write the applescript file.

I don't see how it would be difficult. A quick and dirty (I did this in my lunch break, so it is not pretty) would be:
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
Sorry I misunderstood what you ment there (was reading it on iphone on bus). This ways still seems overly complicated though, I found out that in the terminal you can open the working directory with the command "open ." I have tried to make this work with a file path but nothing happens, I'm guessing I just dont know how to rite a mac file path properly being a windows guy. Does anyone know the correct way to build a mac file path in this context?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hmmm, in thinking about it, I realized that I have over-engineered a solution (albeit my solution is way more powerful than what you are trying to do, and can be used as the stepping stones to wonderful stuff).

Anyway - it looks like you have been ignoring the results of the Runtime.getRuntime().exec() method - it returns a Process, which will allow you to look at the standard input, output, and error for the process you spawned. Had you looked at these, you should have been able to see the errors, which would have given clues as to the problem.

To start with, you don't need (or want) the dollar sign ($) in your command. You do not put the greater-than symbol (>) in the Microsoft equivalent, so why have it here?

The second thing is that there is no path defined in your process. The exec method allows parameters where you can define your path, or you could list it explicitly.

Combining those two, a simple solution might be:


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Open a folder in finder in Java
 
Similar Threads
rsync blank spaces problem
Runtime.getRuntime() help plzzzzzzzz
From web interface to Swing
Open a command prompt using Runtime.getRuntime().exec() and run a jar file
open a file pdf in unix using java