java - 直接在 Java 中获取 Finder 选择,无需任何 Applescript 的帮助

标签 java applescript runtime.exec osascript

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

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

** 编辑 **

我建了一个图书馆。即可获取here on github .

最佳答案

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

String[] cmd = { "osascript", "-e", "tell application \"Finder\" to return POSIX path of ((item 1 of (get selection)) as text)" };

关于java - 直接在 Java 中获取 Finder 选择,无需任何 Applescript 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18274077/

相关文章:

java - 从 WAR 文件获取文件

java - 将对象添加到 LinkedList java 数组

ios - 如何使用 AppleScript 删除 plist 键

macos - 查找 AppleScript 应用程序的 Apple 标识符

java - .jar 不执行并显示 System.out.print();输出

java - 如何将 txt 文件加载到您的程序中?

java - 为什么存在 persistence.xml 时持久性单元名为 null

macos - 在 .app 运行时触发脚本(AppleScript 或 JXA)?

java - 循环内部/外部使用 'Runtime.getRuntime()'

java - swing GUI 中被阻止的外部进程