我从代码here开始,对其进行了一些修改以满足我的需要:
set appList to {} --list of all open applications
set winList to {} --list of all open windows
tell application "System Events"
repeat with theProcess in (processes whose visible is true)
tell theProcess
set processName to name
set theWindows to (name of windows) as text
end tell
set allWins to allWins + (count of theWindows)
set appList to appList&processName
set winList to winList&theWindows
end repeat
end tell
作为一个例子:假设我在Finder中以自己的空间打开了我的Documents文件夹,在脚本编辑器中打开了我当前的脚本,在Safari中以拆分视图打开了我的StackOverflow,并最小化了Mail。该脚本应以某种排列返回appList = {"Finder","Safari","Script Editor","Mail"}
,以某种排列返回winList = {"Documents","StackOverflow","MyScript","All Inboxes"}
。
这是我的问题。在实践中,当我点击“运行”时,显然我是打开拆分视图的,因为这是我的脚本编辑器所在的位置。因此,我实际上得到的是appList = {"Finder","Safari","Script Editor","Mail"}, winList = {,"Stack Overflow","MyScript","All Inboxes"}
-换句话说,当脚本处理Finder处于打开状态时,它不会检测到任何打开的窗口,因为它位于不同的空间。另一方面,“邮件”窗口被最小化,因此它在Dock中进行拾取。
起初我以为发生这种情况的原因是由于讨厌的whose visible is true
行-但是Finder的窗口不可见,因为它们在不同的屏幕中。
所以,我把那条线拿出来了。尽管appList
堆放了随机的幕后应用程序,但可以预见的是,winList
保持不变(保留了大量额外的空白)–那里仍然没有Finder窗口。>
是否有办法获取所有应用程序的所有窗口,即使它们位于不同的空间中?