我有一个非常简单的脚本,在这种情况下,它是使用Javascript For Automation而不是Applescript用JS编写的(如果有人可以帮助,我可以在Applescript中找到解决方案)。该脚本仅删除VLC.app中当前正在播放的文件。我已将脚本分配给键盘快捷键。
尽管我有多个VLC实例正在运行,每个实例都是通过终端命令启动的:
open -n /Applications/VLC.app
下面的脚本大部分可以满足我的要求,但是,它总是与我首先打开的VLC.app的任何实例进行交互-而不是集中的活动实例。
let vlc = Application("VLC");
vlc.includeStandardAdditions = true
let file = vlc.pathOfCurrentItem.get()
let path = Path(file);
var dialogText = "Delete file " + file + "?"
let result = vlc.displayDialog(dialogText, {
buttons: ["No", "Yes"],
defaultButton: "Yes",
cancelButton: "No"
})
if (result.buttonReturned == "Yes") {
let finder = Application("Finder");
finder.delete(path)
}
[我正在尝试获取focused应用程序实例,所以我将第一行更改为:
let vlc = Application.currentApplication();
但是,当我运行它时,出现此错误:
我在此上花了太多时间,所以我想知道是否有人可以告诉我我所缺少的内容!
提前感谢。
通常不建议在AppleScript上使用JXA,因为JXA破坏了许多可在AS中使用的功能。但是,这是JXA确实具有优势的一种用例,因为它可以按进程ID而不是应用程序名称(例如,应用程序名称)为目标。 Application(123)
。剩下的工作是获取现有进程的PID(例如通过NSWorkspace.runningApplications
)。
ps.s。 current application
是运行脚本的过程。