0

是否可以在没有 Applescipt 帮助的情况下直接在 Java 中获得 Finde-Selection?基本上可以通过在 Java 中执行一个 osascript 来调用另一个将 Finder-selection 作为字符串传递的 applescript。谢谢。

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

** 编辑 **

我做了一个图书馆。你可以在 github 上得到它。

4

2 回答 2

1

您可以通过摆脱外部applescript文件并使用这一行来简化您的代码......

String[] cmd = { "osascript", "-e", "tell application \"Finder\" to return POSIX path of ((item 1 of (get selection)) as text)" };
于 2013-08-16T23:16:12.310 回答
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"
                        };

所以这是给我一个字符串的命令,文件由逗号分隔,我稍后将其拆分为单个字符串。不知道这是不是最好的方法,但它有效......

于 2013-08-17T17:50:42.160 回答