Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Open a folder in finder in Java

 
Colin McTaggart
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11878
195
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Colin McTaggart
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11878
195
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11878
195
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic