4 votos

Cambiar la resolución de LG UltraFine a través de AppleScript

Me gustaría tener un AppleScript que cambie entre "Predeterminado para mostrar" en las Preferencias del Sistema y "Escalado" "Texto más grande".

El script debería detectar el estado actual de la pantalla y cambiar al otro estado (es decir, si está en Default for display, cambiar a un texto más grande a escala).

Llegué hasta aquí con mi script (que obtuve de este sitio y esto página de stackexchange ), pero parece que no puedo llegar a "hacer clic" virtualmente en el icono más a la izquierda de los cinco disponibles:

tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.displays"
    reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell

local indexToUse

-- Now lets make the necessary changes
tell application "System Events"
    tell tab group 1 of window "LG UltraFine" of application process "System Preferences" of application "System Events"
        tell radio group 1

            if (value of radio button "Scaled") = 0 then
                -- Click the "Scaled" radio button
                click radio button "Scaled"

                tell radio group 2
                    click radio button 1 of radio group 2
                end tell

            else
                click radio button "Default for display"
            end if
        end tell

    end tell
end tell

-- Quit "System Preferences"
quit application "System Preferences"

Al ejecutar el script, si las Preferencias del Sistema están en "Predeterminado para mostrar", me sale el siguiente script Error: System Events got an error: Can’t get radio group 2 of radio group 1 of tab group 1 of window "LG UltraFine" of application process "System Preferences". Invalid index.

Me gustaría poder hacer clic en el botón marcado en rojo en la captura de pantalla adjunta. Screenshot of System Preferences → Displays → Scaled

1voto

pdeli Puntos 64

Gracias a @wch1zpink y a la sugerencia de utilizar el Watch Me Do en Automator, aquí hay un script que finalmente parece hacer el trabajo:

-- Portions of the script found on https://gist.github.com/mvaneijgen/2f48f859ca07d2e75b3a
-- Launch "System Preferences", open the "Displays" options and change to the "Display" tab
(* If error "System Events got an error: Script Editor is not allowed assistive access" appears, then System Preferences → Security & Privacy → Privacy → add Script Editor to "Allow the app to control your computer"*)
(* as per: https://stackoverflow.com/questions/31019916/is-not-allowed-for-assistive-access-error-when-running-applescript-from-java) *)

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell

-- Now lets make the necessary changes
tell application "System Events"
    tell tab group 1 of window "LG UltraFine" of application process "System Preferences" of application "System Events"
        tell radio group 1

            if (value of radio button "Scaled") = 0 then -- Check if Scaled radio button is not selected
                click radio button "Scaled" -- Click the "Scaled" radio button

                -- and click on the icon above "Larger Text" (which is in fact a radio button)
                tell application "System Events"
                    tell tab group 1 of window "LG UltraFine" of application process "System Preferences" of application "System Events"
                        tell radio group 1 of group 2
                            click radio button 1
                        end tell
                    end tell
                end tell

                else -- Scaled radio button is already selected
                    click radio button "Default for display" -- therefore click on "Default for display"
            end if
        end tell
    end tell
end tell

0voto

wch1zpink Puntos 11

Esto funciona en mi MacBook Pro con la última versión de Sierra. Acabo de sustituir "Built in Retina display" por el tuyo. Creo que debería funcionar para usted.

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "LG UltraFine"
    click radio button "Scaled" of radio group 1 of tab group 1
    click radio button 1 of radio group 1 of group 1 of tab group 1
    delay 0.1 -- adjust this value as needed if you get a message that you need to click "ok".
    click button "OK" of sheet 1 -- on my MBP, choosing the lowest resolution displays a message that I need to click "ok". Just delete this command if you don't need it
end tell
quit application "System Preferences"

Este es el mensaje que aparece para el que añadí el comando "botón de clic"

enter image description here

Esto debería restablecer la resolución de la pantalla por defecto

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "LG UltraFine"
    click radio button "Scaled" of radio group 1 of tab group 1
    click radio button 3 of radio group 1 of group 1 of tab group 1
end tell
quit application "System Preferences"

Y aquí está la versión de la palanca

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "LG UltraFine"
    click radio button "Display" of tab group 1
    click radio button "Scaled" of radio group 1 of tab group 1
    tell radio group 1 of group 1 of tab group 1
        set getResolution to get value of radio button 1
    end tell
    if getResolution then
        click radio button 3 of radio group 1 of group 1 of tab group 1
    else
        click radio button 1 of radio group 1 of group 1 of tab group 1
        delay 0.1 -- adjust this value as needed if you get a message that you need to click "ok".
        try
            click button "OK" of sheet 1 -- on my MBP, choosing the lowest resolution displays a message that I need to click "ok". Just delete this command if you don't need it
        end try
    end if
end tell
quit application "System Preferences"

También puedes hacer "ingeniería inversa" (a falta de un término mejor) con Automator y "verme hacer". En Automator, inicie una grabación de "Watch me do" y cuando termine de grabar, seleccione todos los pasos de la acción grabada y la opción de menú Editar/Copiar. Luego cambie al ScriptEditor y haga un nuevo documento y "Pegue" lo que copió de Automator en el nuevo documento.

enter image description here

enter image description here

Puedes compilar el script y ejecutarlo si quieres. Pero lo más importante es que te mostrará los nombres exactos de tus elementos de interfaz de usuario (pestaña 1, área de desplazamiento, etc.) que puedes utilizar para sustituirlos en el código que he publicado antes.

AppleAyuda.com

AppleAyuda es una comunidad de usuarios de los productos de Apple en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X