0

Is it possible to get the Finde-Selection directly in Java without any help of Applescipt? Basically its possible by executing an osascript in Java which calls another applescript that passes the Finder-selection as a string. thx.

import java.io.*;

public class FinderSelection {
    public static void main(String [] args) throws IOException {
        String[] cmd = { "osascript", "-e", "run script \"FinderSelection.scpt\" as POSIX file" };

        InputStream is = Runtime.getRuntime().exec(cmd).getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader buff = new BufferedReader (isr);
        String line;
        while((line = buff.readLine()) != null)
            System.out.println(line);
    }
}

FinderSelection.scpt

tell application "Finder"
    set theSelection to selection
    set item1 to POSIX path of (item 1 of the theSelection as alias) as string
end tell

** EDIT **

I made a library. You can get it here on github.

domizai
  • 343
  • 1
  • 5
  • 13
  • is String line given a value elsewhere? I see where you're telling it to print to screen, but it has not been given a value. – Jeremy Johnson Aug 16 '13 at 13:14
  • I would also put try/catch block around your input stream, throwing and never catching an exception is a bad idea – Jeremy Johnson Aug 16 '13 at 13:15
  • Well this is just a simplified version here and it works for me, i also get the expected output. But the question is if there is a method without any help of applescript, maybe by using i/o? – domizai Aug 16 '13 at 13:25
  • For best results with this, I would go ahead and include your working code for review as well because this is just going to end up getting picked apart, I think. Just trying to help you get your question answered. – Jeremy Johnson Aug 16 '13 at 13:27

2 Answers2

1

You can simplify your code by getting rid of the external applescript file and using this line...

String[] cmd = { "osascript", "-e", "tell application \"Finder\" to return POSIX path of ((item 1 of (get selection)) as text)" };
regulus6633
  • 18,848
  • 5
  • 41
  • 49
0
String [] cmd = {   "osascript", "-e",
                            "tell application \"Finder\" to set theSelection to (selection) as alias list",
                            "-e",
                            "set myFiles to {}",
                            "-e",
                            "repeat with i from 1 to length of theSelection",
                            "-e",
                            "set myFiles to myFiles & POSIX path of (item i of the theSelection as alias) & \", \" as string",
                            "-e",
                            "end repeat"
                        };

So this is the command which gives me back a string, files separated by a comma which i split into single strings later on. Dunno if that is the best way, but it works...

domizai
  • 343
  • 1
  • 5
  • 13
  • 1
    Your code may work but since you are adding strings in your repeat loop it doesn't make sense that the initial value of myFiles is an empty list. It should be an empty string. Therefore I would change that line to: set myFiles to "". You also don't need "as alias" because the list from the Finder is already a bunch of alias's. You also don't need "as string" because a POSIX path is a string. So in the repeat loop use: set myFiles to myFiles & posix path of (item i of theSelection) & "," – regulus6633 Aug 17 '13 at 21:25