通过这个简单的AppleScript,我可以列出iTunes中所有歌曲的名称
tell application "iTunes"
set mainLibrary to library playlist 1
repeat with nexttrack in (get every track of mainLibrary)
try
name of nexttrack
end try
end repeat
end tell
然后我使用ScriptEngine将其作为Java 7代码运行,但是很明显如何获取脚本的结果,在这种情况下只是最后一个轨道的名称.
StringBuilder script =new StringBuilder();
script.append("tell application \"iTunes\"\n");
script.append("set mainLibrary to library playlist 1\n");
script.append("repeat with nexttrack in (get every track of mainLibrary)\n");
script.append("try\n");
script.append("name of nexttrack\n");
script.append("end try\n");
script.append("end repeat\n");
script.append("end tell\n");
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("AppleScript");
System.out.println(engine.eval(script.toString()));
How do can I capture the name of all tracks , i.e all output from the script. I assume I have to redirect standard out to a Reader, but cannot see how to do it.
更新:
I found this article
但是通过代码更新
StringBuilder script =new StringBuilder();
script.append("tell application \"iTunes\"\n");
script.append("set mainLibrary to library playlist 1\n");
script.append("repeat with nexttrack in (get every track of mainLibrary)\n");
script.append("try\n");
script.append("name of nexttrack\n");
script.append("end try\n");
script.append("end repeat\n");
script.append("end tell\n");
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("AppleScript");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
StringWriter swError = new StringWriter();
PrintWriter pwError = new PrintWriter(swError);
engine.getContext().setWriter(pw);
engine.getContext().setErrorWriter(pwError);
System.out.println(engine.eval(script.toString()));
System.out.println("************* engine:"+sw.getBuffer());
System.out.println("************* engine error:"+swError.getBuffer());
只是输出
See You on the Other Side (short)
************* engine:
************* engine error:
所以你可以清楚地看到脚本工作,因为eval显示了最后一个轨道的名称,但我从重定向的编写器中得不到任何回复.想知道这实际上适用于Applecript吗?
更新2
我在Javascript中编写了一个测试,发现设置编写者DID WORK,所以我想也是
>在OSX上的Java 7的早期访问版本的Applescript(我使用最新版本1.7.0_40-ea-b29)的脚本引擎中有一些损坏
>我的Applescript是错的,即不仅仅是nexttrack的名字,我应该做类似于nexttrack的echo名称,以实际使它转到stdout.
我想(2)是问题所在,但我找不到任何关于在Applescript中输出文本的引用,echo不是一个有效的命令,而且print命令确实会尝试打印到打印机而不是打印到stdout,因为它会在Java或C.
更新3
所以我无法让这个工作,现在想知道Applescript是否只写了返回值.我找到了一个使用stdout的例子(或者我认为),但我认为这只是一个命名约定,事实上它所做的只是不断地将数据附加到变量,并最终返回.
所以我通过使用存储在文件中的以下脚本来实现它的工作,然后在Java中使用engine.eval(new java.io.FileReader(“filename”))
tell application "iTunes"
set output to ""
set mainLibrary to library playlist 1
repeat with nexttrack in (get every track of mainLibrary)
if (class of nexttrack is file track) then
try
set trackname to name of nexttrack
set loc to location of nexttrack
set locpath to POSIX path of loc
set persistid to persistent ID of nexttrack
set nextline to trackname & "::" & locpath & "::" & persistid
set output to output & "\n" & nextline
end try
end if
end repeat
copy output to stdout
end tell
但这实际上并没有解决原来的问题,它的一个问题是,如果有任何错误,那么我不会得到任何回报,而如果它在读取我的iTuneslibrary中途失败并且我使用stdout我至少会得到一半数据回来了.