AppleScript 在使用 repeat 时不循环所有数字
AppleScript not looping all numbers when using repeat
我正在使用此脚本关闭通知栏中的所有 "Alerts":
tell application "System Events"
tell process "NotificationCenter"
set numwins to (count windows)
repeat with i from numwins to 1 by -1
click button "Close" of window i
end repeat
end tell
end tell
然而,即使没有"Alert without Close button"。
,这并不会全部关闭它们
try catch
没有帮助。怎么了?
我测试了你的脚本,它似乎在我的机器上 运行 正确,但是当你在可变列表上使用重复循环时,总是有可能出现问题:换句话说,一个列表会改变随着重复循环的进行。每次你 close 一个 window 通知中心改变它的 window 列表并更新剩余 windows 的属性;该脚本可以简单地 lose 跟踪。我有点惊讶它在发生这种情况时不会抛出错误,但是...
您可以试试这段代码,看看它是否有效。它不是通过索引引用 windows,而是反复尝试 close 最后一个 window,忽略任何错误,并一直持续到 window 计数为零或循环100 次(最后一次是为了防止出现问题时无限循环)。
tell application "System Events"
tell process "NotificationCenter"
repeat 100 times
try
tell last window
click button "Close"
end tell
end try
if (count of its windows) = 0 then
exit repeat
end if
end repeat
end tell
end tell
编辑
根据评论,以上内容并不像宣传的那样有效,所以让我们将其切换到 AppleScriptObjC:
use framework "Foundation"
property NSUserNotificationCenter : class "NSUserNotificationCenter"
NSUserNotificationCenter's defaultUserNotificationCenter's removeAllDeliveredNotifications()
这在我的机器上似乎可以解决问题。当然,从 10.14 开始,NSUserNotificationCenter
已被弃用,因此这不会永远有效——最终你将不得不转移到通知的框架——但它应该适用于更多的 OS 版本。
编辑 2
根据另一条评论,任何使用 os 10.14 或更高版本(Mojave 和 Catalina,迄今为止)的人都可以使用 UserNotifications 框架执行等效的 AppleScriptObjC 例程,如下所示:
use framework "UserNotifications"
set notificationCenter to class "UNUserNotificationCenter"'s currentNotificationCenter
notificationCenter's removeAllDeliveredNotifications()
请注意,我使用了稍微不同的语法(仅调用 class "UNUserNotificationCenter"
而不是设置 属性)。两者都有效; property
语法仅在需要将 class 传递给处理程序时更可取。
我正在使用此脚本关闭通知栏中的所有 "Alerts":
tell application "System Events"
tell process "NotificationCenter"
set numwins to (count windows)
repeat with i from numwins to 1 by -1
click button "Close" of window i
end repeat
end tell
end tell
然而,即使没有"Alert without Close button"。
,这并不会全部关闭它们try catch
没有帮助。怎么了?
我测试了你的脚本,它似乎在我的机器上 运行 正确,但是当你在可变列表上使用重复循环时,总是有可能出现问题:换句话说,一个列表会改变随着重复循环的进行。每次你 close 一个 window 通知中心改变它的 window 列表并更新剩余 windows 的属性;该脚本可以简单地 lose 跟踪。我有点惊讶它在发生这种情况时不会抛出错误,但是...
您可以试试这段代码,看看它是否有效。它不是通过索引引用 windows,而是反复尝试 close 最后一个 window,忽略任何错误,并一直持续到 window 计数为零或循环100 次(最后一次是为了防止出现问题时无限循环)。
tell application "System Events"
tell process "NotificationCenter"
repeat 100 times
try
tell last window
click button "Close"
end tell
end try
if (count of its windows) = 0 then
exit repeat
end if
end repeat
end tell
end tell
编辑
根据评论,以上内容并不像宣传的那样有效,所以让我们将其切换到 AppleScriptObjC:
use framework "Foundation"
property NSUserNotificationCenter : class "NSUserNotificationCenter"
NSUserNotificationCenter's defaultUserNotificationCenter's removeAllDeliveredNotifications()
这在我的机器上似乎可以解决问题。当然,从 10.14 开始,NSUserNotificationCenter
已被弃用,因此这不会永远有效——最终你将不得不转移到通知的框架——但它应该适用于更多的 OS 版本。
编辑 2
根据另一条评论,任何使用 os 10.14 或更高版本(Mojave 和 Catalina,迄今为止)的人都可以使用 UserNotifications 框架执行等效的 AppleScriptObjC 例程,如下所示:
use framework "UserNotifications"
set notificationCenter to class "UNUserNotificationCenter"'s currentNotificationCenter
notificationCenter's removeAllDeliveredNotifications()
请注意,我使用了稍微不同的语法(仅调用 class "UNUserNotificationCenter"
而不是设置 属性)。两者都有效; property
语法仅在需要将 class 传递给处理程序时更可取。