gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:14:10 25 4
gpt4 key购买 nike

是否可以在没有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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com