使用 JPA 在多 window 应用程序中激活给定的 window

Acitvate a given window in a multi-window application with JXA

假设我有两个打开的 TextEdit 文件,因此有两个 windows。 如何使用 JXA 以编程方式 switch/activate windows?

目前我有这个:

var text = Application("textEdit")
text.activate()
var file1_path = Path("/Users/..../text1.rtf")
var file2_path = Path("/Users/..../text2.rtf")

var doc1 = text.open(file1_path)
var doc2 = text.open(file2_path)

console.log("text1",text.windows["text1.rtf"].index()) //prints 2
console.log("text2",text.windows["text2.rtf"].index()) //prints 1

//the next row brings text1 window to front but doesn't activate it, 
//as a matter of fact the 3 buttons upper/left (close,minimize, full screen) are grey
text.windows["text1.rtf"].index = 1

你可以试一试,这似乎适用于 High Sierra:

TextEdit=Application('TextEdit');
Sys=Application('System Events');

TextEdit.activate()

/* open files, etc. */

Sys.processes["TextEdit"].windows[1].actions['AXRaise'].perform();

当然也可以将索引1替换为window的名称,例如"text1.rtf".