Utilizando Inspector de accesibilidad , una parte de Xcode muestra que una parte de las veces el Anuncia la hora: casilla de verificación informes " El padre no informa del elemento como uno de sus hijos " y como tal causará AppleScript código fallar bajo ciertas condiciones, y bajo otras condiciones no fallar. En otras palabras, escribiendo mi propio código puedo hacer que compruebe que casilla de verificación a veces y otras veces no.
Al tratar de averiguar un patrón para ver cómo solucionar esta situación, llegué a la conclusión de que el uso de Guiones de interfaz de usuario era la forma más fiable. También es, por desgracia, una forma fea y requiere dejar que el script terminar sin interrupción una vez iniciada, de lo contrario fracasará con toda seguridad. Tales son los escollos de la visibilidad total Guiones de interfaz de usuario .
El ejemplo AppleScript código que se muestra a continuación, se probó en Script Editor en MacOS Big Sur con Lengua y región ajustes en Preferencias del sistema ajustado a Inglés (EE.UU.) - Primaria y me ha funcionado sin problemas 1 .
- 1 Asume la configuración necesaria y adecuada en <strong>Preferencias del sistema </strong>> <strong>Seguridad y privacidad </strong>> <strong>Privacidad </strong>se han fijado/abordado según las necesidades.
Este script requiere que el Utilice la navegación por teclado para mover el foco entre los controles casilla de verificación se comprueba en el Preferencias del sistema > Teclado > Atajos y, tal como está codificado, el script comprueba su estado y activa el casilla de verificación Según sea necesario, en función de su estado actual.
Ejemplo AppleScript código :
-- # Check to see if System Preferences is
-- # running and if yes, then close it.
-- #
-- # This is done so the script will not fail
-- # if it is running and a modal sheet is
-- # showing, hence the use of 'killall'
-- # as 'quit' fails when done so, if it is.
-- #
-- # This is also done to allow default behaviors
-- # to be predictable from a clean occurrence.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
-- # Make sure System Preferences is not running before
-- # opening it again. Otherwise there can be an issue
-- # when trying to reopen it while it's actually closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
-- # Get the fully qualified POSIX pathname of the target .plist file.
set thePropertyListFilePath to ¬
the POSIX path of ¬
(path to preferences from user domain as string) & ¬
".GlobalPreferences.plist"
-- # Get the value of AppleKeyboardUIMode to determine if the
-- # 'Use keyboard navigation to move focus between controls'
-- # checkbox is checked on the **System Preferences** >
-- # **Keyboard** > **Shortcuts** tab.
tell application "System Events" to ¬
tell the property list file thePropertyListFilePath to ¬
set keyboardNavigation to the value of ¬
the property list item "AppleKeyboardUIMode"
if keyboardNavigation = 0 then
-- # Check the checkbox.
my toggleKeyboardNavagition()
end if
-- # Open System Preferences to the Clock row of the Dock & Menu Bar pane.
-- #
-- # This UI Script needs it to be visible, hence the activate command.
tell application "System Preferences"
activate
reveal anchor "Clock" of ¬
pane id "com.apple.preference.dock"
end tell
delay 1
-- # Tab to the 'Announce the time:' checkbox and toggle it.
tell application "System Events"
repeat 9 times
key code 48 -- # tab key
delay 0.2
end repeat
key code 49 -- # spacebar
delay 0.1
end tell
if keyboardNavigation = 0 then
-- # Uncheck the checkbox if it
-- # was previously unchecked.
my toggleKeyboardNavagition()
end if
delay 0.5
tell application "System Preferences" to quit
-- # Handler(s) #
-- # Toggles checkbox: 'Use keyboard navigation
-- # to move focus between controls'
on toggleKeyboardNavagition()
tell application "System Preferences"
activate
reveal anchor "shortcutsTab" of ¬
pane id "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell front window of ¬
application process "System Preferences"
set i to 0
repeat until (exists checkbox 1 of tab group 1)
delay 0.1
set i to i + 1
if i 20 then return
end repeat
click checkbox 1 of tab group 1
end tell
end tell
end toggleKeyboardNavagition
- Tenga en cuenta que, debido a los comentarios y al estilo de codificación, el script es largo. Termina con
end toggleKeyboardNavagition
así que asegúrese de resaltarlo todo cuando copie y pegue en Script Editor para las pruebas.
Notas:
En el tell application "System Events"
bloque en con hay el repeat 9 times
bucle que hace la tabulación para alternar el Anuncia la hora: casilla de verificación El valor de la delay
comando puede ser necesario ajustar.
Si el estado normal del Utilice la navegación por teclado para mover el foco entre los controles casilla de verificación no está marcada, entonces no ejecute el script inmediatamente de forma consecutiva, ya que se necesitan uno o dos segundos para que el valor de la property list item "AppleKeyboardUIMode"
en el usuarios archivo de preferencias globales para actualizar el cambio. Menciono esto principalmente para cuando se hace la prueba más que cuando en el uso de producción normal, ya que no debería ser un problema entonces.
Nota: El <em>ejemplo </em><strong>AppleScript </strong><em>código </em>es sólo eso y sin ningún tipo de inclusión <em>tratamiento de errores </em>no contiene ningún otro <em>tratamiento de errores </em>según corresponda. Corresponde al usuario añadir cualquier <em>tratamiento de errores </em>como sea apropiado, necesario o deseado. Eche un vistazo a la <a href="https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html#//apple_ref/doc/uid/TP40000983-CH6g-129232" rel="nofollow noreferrer"><strong>pruebe con </strong></a><em>declaración </em>y <a href="https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html#//apple_ref/doc/uid/TP40000983-CH6g-129657" rel="nofollow noreferrer"><strong>error </strong></a><em>declaración </em>en el <a href="https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html" rel="nofollow noreferrer"><strong>Guía del lenguaje AppleScript </strong></a>. Véase también, <a href="https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_error_xmpls.html#//apple_ref/doc/uid/TP40000983-CH221-SW1" rel="nofollow noreferrer"><strong>Trabajar con errores </strong></a>. Además, el uso de la <a href="https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW10" rel="nofollow noreferrer"><strong>retraso </strong></a><em>puede ser necesario entre eventos cuando sea apropiado, por ejemplo <code>delay 0.5</code> con el <em>valor </em>de la <em>retraso </em>ajustado apropiadamente.</em>