您可以使用以下代码示例来通过AppleScript对应用程序的“主”窗口进行操作,即使它不在最前面:
-- 检查应用程序是否正在运行
if application "应用程序名称" is running then
-- 通过名称获取应用程序的进程ID
set appPID to unix id of application "应用程序名称"
-- 获取应用程序的所有窗口
tell application "System Events"
set appWindows to windows of process appPID
end tell
-- 遍历所有窗口,并找到“主”窗口
repeat with theWindow in appWindows
-- 检查窗口是否是“主”窗口
if value of attribute "AXMain" of theWindow is true then
-- 将“主”窗口置于最前面
tell application "System Events" to perform action "AXRaise" of theWindow
exit repeat -- 找到“主”窗口后退出循环
end if
end repeat
end if
请替换代码中的应用程序名称
为您想要操作的应用程序的名称。这段代码将检查应用程序是否正在运行,然后获取应用程序的所有窗口并遍历它们,找到“主”窗口并将其置于最前面。
请注意,某些应用程序可能不支持通过AppleScript将窗口置于最前面。在这种情况下,您可以尝试其他方法,如使用快捷键模拟或使用其他自动化工具来操作窗口。