¿Qué versión de MacOS utiliza? He descubierto algo para que pueda auto-despedir a los molestos persistentes updates available
(que se producen independientemente de la configuración de actualización automática), pero al parecer las funciones de Applescript para las notificaciones cambian enormemente entre las versiones del sistema operativo. Mi solución no es realmente "actuar al recibir una notificación", sino que comprueba constantemente las notificaciones visibles en busca de determinadas cadenas. Eso es más o menos lo mejor que puedes hacer sólo con Applescript. Debería funcionar bien en al menos Monterey y posiblemente algo más nuevo.
La parte superior es un poco de configuración, a continuación, en la parte inferior he añadido un comentario para indicar dónde agregar su manejo. Probablemente deberías leer todo el script de todos modos. =] Como un bono también he incluido la funcionalidad de auto-despedir, que debe ser independiente del idioma. Además, no estoy seguro de si los subtítulos todavía existen, porque ninguno apareció en mis casos de prueba. Pero he añadido un comentario donde se puede tratar de recuperarlos.
global notificationStrings
set notificationStrings to {"Bricks + Agent"}
-- These *must* be the English names, since they will be translated to whatever's currently on the notification
global closeStrings
set closeStrings to {"Close", "Clear All"}
global accessCheckTitle
set accessCheckTitle to "Notifications Monitor"
-- An error message will be directly appended to this
global accessCheckSubtitle
set accessCheckSubtitle to "This app needs both Accessibility and Automation access:\n\n"
global buttonOk
set buttonOk to "Close"
global buttonSysPrefs
set buttonSysPrefs to "Open System Preferences"
-- Amount of ticks before we reload the strings (at return 3 this is about 300 seconds, or 5 min)
global reloadStringsInterval
set reloadStringsInterval to 100
-- END OF CONFIG
global reloadStringsTicks
set reloadStringsTicks to -1
global closeStringsl10n
set closeStringsl10n to {}
on idle
checkNotifications()
return 3 -- Delay for next run (seconds)
end idle
on quit
continue quit -- Allows the script to quit
end quit
on checkNotifications()
try
-- All of these throw errors when they've explicitly been denied access, otherwise they'll prompt (at least on the very first time)
tell application id "com.apple.SystemEvents" to set ncenterProcess to the first process whose bundle identifier = "com.apple.notificationcenterui" -- For Automation access on System Events
set ncenter to path to application id "com.apple.notificationcenterui" -- For Automation access on Notification Centre
tell ncenterProcess to exists window 1 -- For Accessibility access
on error errmsg
-- A try is necessary to handle the cancel button without "crashing" (thanks Apple)
set openSysPrefs to false
try
set res to (display dialog (accessCheckSubtitle & errmsg) with title accessCheckTitle buttons {buttonOk, buttonSysPrefs} default button buttonSysPrefs cancel button buttonOk with icon stop)
if button returned of res is equal to buttonSysPrefs then
set openSysPrefs to true
end if
end try
if openSysPrefs then
open location "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"
end if
-- Always need to quit here so that we can pick up on any newly granted permissions next time we run
delay 30
quit
end try
set reloadStringsTicks to reloadStringsTicks + 1
if reloadStringsTicks is equal to 0 or reloadStringsTicks is greater than or equal to reloadStringsInterval then
set l10nTmp to {}
try
repeat with closeString in closeStrings
set the end of l10nTmp to localized string closeString in bundle ncenter
end repeat
on error
-- I think this can happen if this script runs too early after logging in, so let's make sure we'll immediately try again on the next run
set reloadStringsTicks to -1
return
end try
set closeStringsl10n to l10nTmp
set reloadStringsTicks to 0
end if
tell application id "com.apple.SystemEvents"
tell ncenterProcess
-- The main window may not always (immediately/fully) exist apparently
try
set allNotifications to groups of UI element 1 of scroll area 1 of window 1
on error
return
end try
repeat with checkNotification in allNotifications
-- It may have already (been) closed in the meantime
try
set nTitle to the value of static text 1 of checkNotification
--set nContents to the value of static text 2 of checkNotification
-- There *may* be a static text 3 for subtitles, but it didn't exist in my test cases
if nTitle is in notificationStrings then
-- This is where you would add your dialog and other handling, for example (to dismiss the notification afterwards):
repeat with checkAction in actions of checkNotification
if description of checkAction is in closeStringsl10n then
try
perform checkAction
end try
delay 2 -- Give it some time to close
end if
end repeat
end if
end try
end repeat
end tell
end tell
end checkNotifications
Guárdelo como Application
mientras se desactiva Show startup screen
y habilitar Stay open after run handler
. Añadirlo a tus elementos de inicio debería funcionar sin problemas.