由于 OP 已超过六年,我无法在当时使用的 OS X 10.7 下进行测试,但是,以下内容example AppleScript code 在 macOS Catalina 下为我工作并返回正确的窗口计数跨所有桌面/空间,但理解给定AppleScript 命令的任何应用程序以及为什么正在使用try 和on error 语句。
示例 AppleScript 代码:
tell application "System Events" to ¬
set appBundleIdentifierList to ¬
the bundle identifier of ¬
(every process whose visible is true)
repeat with appBundleIdentifier in appBundleIdentifierList
try
tell application id appBundleIdentifier to ¬
set {appName, winCount} to {name, (count windows)}
log appName & ": " & winCount
on error errorMessage
log errorMessage
end try
end repeat
我的系统上的示例 输出,该系统有多个 桌面/空间,所有或一些桌面/空间和每个的窗口计数在所有桌面/空间上都是正确的,而不仅仅是活动的桌面/ 脚本的运行空间。
(*Safari: 6*)
(*Terminal: 2*)
(*TextEdit: 4*)
(*Script Editor: 7*)
(*Finder: 3*)
(*BBEdit: 1*)
(*Norton Secure VPN got an error: every window doesn’t understand the “count” message.*)
(*Music: 2*)
注意事项:
并非所有应用程序都是AppleScript可编写脚本的,因为有些应用程序的中不包含AppleScript 字典 em>应用程序包。
由于application process 无法在所有桌面/空间 中返回正确的窗口 数量,此方法依赖application 来返回 的数量>windows 跨所有桌面/空间。
更新:
以下示例 AppleScript 代码 执行以下操作:
示例 AppleScript 代码:
set menuName to "Window"
tell application id "com.apple.systemevents" to ¬
set appBundleIdentifierList to ¬
the bundle identifier of ¬
(every process whose visible is true)
repeat with appBundleIdentifier in appBundleIdentifierList
try
tell application id appBundleIdentifier to ¬
set {appName, winCount} to {name, (count windows)}
log appName & ": " & winCount & ¬
" -- By querying the application directly."
on error
set winCount to 0
set notVisibleWindowList to {}
set errAppName to ¬
name of application id appBundleIdentifier
tell application id "com.apple.systemevents"
try
tell application process errAppName
set notVisibleWindowList to ¬
(windows whose visible is false)
if notVisibleWindowList is {} then ¬
set winCount to ¬
length of notVisibleWindowList
end tell
end try
try
set theTargetMenuItemsList to ¬
the reverse of ¬
(get name of ¬
menu items of ¬
menu menuName of ¬
menu bar item menuName of ¬
menu bar 1 of ¬
application process errAppName)
on error
set theTargetMenuItemsList to {}
end try
end tell
if theTargetMenuItemsList is not {} then
repeat with anItem in theTargetMenuItemsList
if contents of anItem is ¬
missing value then exit repeat
set winCount to winCount + 1
end repeat
log errAppName & ": " & winCount & ¬
" -- By querying the Window menu of the application process."
else
try
tell application id "com.apple.systemevents" to ¬
set winCount to ¬
(count windows of ¬
application process errAppName)
log errAppName & ": " & winCount & ¬
" -- By querying the application process. " & ¬
"May not be accurate, verify as necessary."
end try
end if
end try
end repeat
运行两个版本的示例 AppleScript 代码以显示输出的差异:
第一版示例 AppleScript 代码:
(*Safari: 6*)
(*TextEdit: 4*)
(*Finder: 3*)
(*BBEdit: 1*)
(*Norton Secure VPN got an error: every window doesn’t understand the “count” message.*)
(*Music: 2*)
(*Sublime Text 2 got an error: every window doesn’t understand the “count” message.*)
(*DiskCatalogMaker got an error: every window doesn’t understand the “count” message.*)
(*Script Editor: 7*)
(*System Preferences: 2*)
(*VMware Fusion got an error: every window doesn’t understand the “count” message.*)
(*Activity Monitor got an error: every window doesn’t understand the “count” message.*)
(*Terminal: 2*)
第二版示例 AppleScript 代码:
(*Safari: 6 -- By querying the application directly.*)
(*TextEdit: 4 -- By querying the application directly.*)
(*Finder: 3 -- By querying the application directly.*)
(*BBEdit: 1 -- By querying the application directly.*)
(*Norton Secure VPN: 0 -- By querying the application process. May not be accurate, verify as necessary.*)
(*Music: 2 -- By querying the application directly.*)
(*Sublime Text 2: 4 -- By querying the Window menu of the application process.*)
(*DiskCatalogMaker: 2 -- By querying the Window menu of the application process.*)
(*Script Editor: 7 -- By querying the application directly.*)
(*System Preferences: 2 -- By querying the application directly.*)
(*VMware Fusion: 1 -- By querying the Window menu of the application process.*)
(*Activity Monitor: 0 -- By querying the Window menu of the application process.*)
(*Terminal: 2 -- By querying the application directly.*)
您甚至可以看到 Activity Monitor,一个原生默认 macOS 应用程序,Window 菜单必须直接查询为应用程序不懂基本的count windowsAppleScript command。
虽然第二版代码的输出在执行时在所有桌面/空间上都是准确的,但任何应用程序有“通过查询申请过程。可能不准确,根据需要进行验证。”作为其输出的一部分,仅包括它执行形式的活动桌面/空间的窗口计数。底线是使用 basic vanilla AppleScript 无法保证获得每个可见应用程序的完整准确的窗口计数> 除非当时的所有应用程序都可以直接查询。通过应用进程查询Window菜单也应该是准确的。
话虽如此,我认为可能需要使用其他方法来获得准确的计数。