0 votos

Cómo aprender la secuencia de comandos de apple correctamente

Recientemente estoy tratando de configurar un AppleScript para hacer alguna tarea sencilla. Pero realmente no puedo encontrar algunos recursos que puede aportar con una introducción sistemática o tutorial para mí para empezar con el Script de Apple. Así que me gustaría saber algunos consejos sobre cómo debo empezar. O en otras palabras, que son los materiales recomendados o la documentación que debo seguir?

Traté de ir a través de la Apple de la documentación oficial, pero que parece demasiado pesado para ser un guía de inicio rápido.

Algo como esto como lo hemos hecho en python sería la mejor.

Y acabo de adjuntar el problema que estoy tratando de resolver a continuación.

una lógica simple:

if safari is not the front most application:
    bring safari to frontmost
    if certain webpage is opened in safari:
        bring that tab of the webpage to the frontmost
    else
        open that webpage
    end if
else
    hide safari
end if

Bastante simple e ingenuo, no lo es.

1voto

iggymoran Puntos 930

Hey después de un poco de exploración, descubrí el código.

Y me gustaría compartirlo contigo en caso de que alguien más tenga la misma necesidad.

Este código básicamente alterna una página web preestablecida en safari. La lógica detallada se puede encontrar en el pseudo código en la descripción de la pregunta. Y creo que puede ser realmente útil si está involucrado mucho con aplicaciones envueltas en la web.

 set appName to "Safari"
set pageName to "<+Your Page Name+>"
set pageUrl to "<+Your page URL+>"

tell application "System Events"
    if not (exists process appName) then
        tell application appName to activate
    else if frontmost of process appName then
        # only hide application if the current tab is what we want
        # my search_tabs(pageName, pageUrl)
        set visible of process appName to (my search_tabs(pageName, pageUrl))
    else
        # still unable to give focus to desired window
        set frontmost of process appName to true
        my search_tabs(pageName, pageUrl)
    end if
end tell

on search_tabs(pageName, pageUrl)
    set tab_found to false
    set be_visible to false
    tell application "Safari"
        tell window 1
            if name of current tab starts with pageName then
                # if the current tab is already what we want then
                # return false
                return false
            end if
        end tell
        repeat with w in windows
            if name of w is not "" then --in case of zombie windows
                repeat with t in tabs of w
                    if name of t starts with pageName then
                        set tab_found to true
                        tell w to set current tab to t
                        if index of w is not 1 then
                            tell w to set index to 1
                        end if
                    end if
                end repeat
            end if
        end repeat
        # if the webpage doesn't exist
        if tab_found is false then
            tell window 1
                set current tab to (make new tab with properties {URL:pageUrl})
            end tell
        end if
        return true
    end tell
end search_tabs
 

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