1 votos

Abre la url en una nueva pestaña y pega el texto copiado

Soy nuevo en AppleScript. Quiero usar AppleScript con la página de Sci-hub. En mi caso, destaco la dirección DOI -> AppleScript abrirá la página "Sci-hub.tw" en una nueva pestaña de Safari -> pega la dirección DOI en el cuadro de texto -> pulsa Enter / Return. He encontrado en muchos sitios y trató de código. Es aquí, pero mi código sólo abrir url en una nueva pestaña, no pegar la dirección highligh.

¡Por favor, ayúdenme!

Gracias y saludos.

on run {input, parameter}
   tell application "Safari"
        activate
        try
        tell window 1 to set current tab to make new tab with properties {URL:"http://www.sci-hub.tw"}
             on error
                open location theURL
        end try
        tell application "System Events"
            tell process "Safari"
                 activate
                 keystroke "v" using command down
                 delay 0.3
                 key code 36 #return
            end tell
        end tell
    end tell
end run

2voto

user3439894 Puntos 5883

El ejemplo AppleScript código que se muestra a continuación, puede serle útil.

Tal y como está codificado, funciona desde script Editor en MacOS High Sierra Sin embargo, para MacOS Mojave Una edición menor es necesaria para el tell application "System Events" comando en el on waitForPageToFinishLoadingInSafari() manipulador y se muestra en el comentario -- # NOTE: dentro del código abajo.

Ejemplo AppleScript código :

set theURL to "http://www.sci-hub.tw"

tell application "Safari"
    activate
    try
        tell window 1 to set current tab to make new tab with properties {URL:theURL}
    on error
        open location theURL
    end try
end tell

my waitForPageToFinishLoadingInSafari()

tell application "System Events"
    keystroke "v" using command down
    delay 0.3
    key code 36 #return     
end tell

--  # Handlers:

on waitForPageToFinishLoadingInSafari()

    --  # NOTE: For macOS Mojave, change 'UI element 1' to 'UI element 2` in the code below.

    tell application "System Events"
        repeat until (accessibility description of ¬
            button 1 of UI element 1 of every group of toolbar 1 of window 1 of ¬
            process "Safari" whose name = "Reload this page") contains "Reload this page"
            delay 0.5
        end repeat
    end tell

end waitForPageToFinishLoadingInSafari
  • Nota: Tal y como está codificado, asume que ya tiene la cadena de búsqueda deseada en el Portapapeles, de modo que cuando Eventos del Sistema hace ⌘V se pega en el cuadro de texto de la página.

Nota: El ejemplo AppleScript código es sólo eso y sin tratamiento de errores de su código original no contiene ninguna tratamiento de errores según corresponda. Corresponde al usuario añadir cualquier tratamiento de errores como sea apropiado, necesario o deseado. Eche un vistazo a la intente declaración y error declaración en el Guía del lenguaje AppleScript . Véase también, Trabajar con errores .


Actualización:

Esta actualización es para abordar explícitamente cómo el OP está utilizando su Acción rápida servicio en Automatizador según su comentario a esta respuesta.

El ejemplo AppleScript código que se muestra a continuación, se ha probado en MacOS Mojave y funciona en mi sistema tal cual.

Sustituir el código por defecto de la Ejecutar AppleScript acción con lo siguiente ejemplo AppleScript código :

on run {input, parameters}

    set theURL to "http://www.sci-hub.tw"
    set theSearchString to (item 1 of input as text)

    tell application "Safari"
        activate
        try
            tell window 1 to set current tab to make new tab with properties {URL:theURL}
        on error
            open location theURL
        end try
    end tell

    my waitForPageToFinishLoadingInSafari()

    tell application "System Events"
        keystroke theSearchString
        delay 0.2
        key code 36 -- # Enter Key     
    end tell

end run

--  # Other Handlers:

on waitForPageToFinishLoadingInSafari()

    -- # NOTE: For macOS High Sierra and earlier , change 'UI element 2' to 'UI element 1' in the code below.

    tell application "System Events"
        repeat until (accessibility description of ¬
            button 1 of UI element 2 of every group of toolbar 1 of window 1 of ¬
            process "Safari" whose name = "Reload this page") contains "Reload this page"
            delay 0.5
        end repeat
    end tell

end waitForPageToFinishLoadingInSafari

Nota: Para utilizar el Acción rápida servicio Cada uno de ellos aplicación desde el que lo activas tendrá que añadirse a dos lugares en:

  • Preferencias del sistema > Seguridad y privacidad > Privacidad
    • Accesibilidad
    • Automatización

Se le pedirá lo necesario y tendrá que desbloquear el Preferencias del sistema > Seguridad y privacidad > Privacidad ficha según sea necesario y compruebe el aplicación según sea necesario. Vea las imágenes siguientes.


Automator Quick Action

En las dos imágenes siguientes, verá que Safari y TextEdit se han añadido y esto es un resultado directo del uso del Acción rápida servicio de ambas aplicaciones. Si tuviera que seleccionar algo para ser buscado con Buscar con SCI-HUB de la Servicios menú en, por ejemplo, Correo Entonces también habrá que añadirlo, y de nuevo se le pedirá que lo haga.

Security & Privacy - Accessibility Security & Privacy - Automation

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