0

Applescipt の助けを借りずに Java で Finde-Selection を直接取得することは可能ですか? 基本的には、Java で osascript を実行して、Finder 選択を文字列として渡す別の 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

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 に答える