AppleScript – Wie erstelle ich eine Suche nach Ergebnissen für eine Zeichenfolge?

AppleScript – Wie erstelle ich eine Suche nach Ergebnissen für eine Zeichenfolge?

Ich versuche, das Ergebnis eines AppleScripts zu durchsuchen, um festzustellen, ob eine Zeichenfolge angezeigt wird.

Ausführen dieses Codes:

tell application "System Events" to tell process "Box Sync" to ¬
    tell menu bar item 1 of menu bar 2
        click
        get menu items of menu 1
        set myStatus to menu items of menu 1
        set myResult to result
        return myResult             
    end tell

Ergebnisse in:

{menu item "Files Synced" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 2 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Pause" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 4 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Open Box Sync Folder" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Open Box.com" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 7 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Preferences…" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 9 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Quit" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events"}

Nun möchte ich dieses Ergebnis durchsuchen, um zu sehen, ob "Dateien synchronisiert" vorhanden ist. Allerdings läuft

 myResult contains "Files Synced"

Gibt mir wieder einen Ausdruck des gesamten Ergebnisses. Wie durchsuche ich dieses Ergebnis, um festzustellen, ob eine Zeichenfolge vorhanden ist?

Antwort1

Sie haben beide Male den gleichen Ausdruck erhalten, weil Sie die return myResultZeile nach Ihrem ersten Durchlauf nicht entfernt haben. returnbeendet ein Skript immer, wenn es diesen Befehl erreicht.

▸ Ändern Sie zusätzlich Folgendes:

    set myStatus to menu items of menu 1

dazu:

    set myResult to name of menu items of menu 1

▸ Löschen Sie diese Zeile:

    get menu items of menu 1

und diese Zeile:

    set myResult to result

(Sie tun buchstäblich nichts.)

Ihr endgültiges Skript sieht folgendermaßen aus:

    tell application "System Events" to tell process "Box Sync" to ¬
        tell menu bar item 1 of menu bar 2
            click
            set myResult to name of menu items of menu 1
            myResult contains "Files Synced"
        end tell

truewelches oder zurückgibt false.

Alternativ ohne die expliziten Variablendeklarationen (und unter Verwendung der vordefinierten AppleScript- resultVariable):

    tell application "System Events" to tell process "Box Sync" to ¬
        tell menu bar item 1 of menu bar 2
            click
            get the name of menu items of menu 1
            result contains "Files Synced"
        end tell

Wenn Sie Erläuterungen benötigen oder weitere Fragen haben, hinterlassen Sie gerne einen Kommentar und ich werde mich bei Ihnen melden. Bitte ziehen Sie in Erwägung, dies als Ihre akzeptierte Antwort auszuwählen, wenn es Ihnen hilft, Ihr Problem zu lösen.

verwandte Informationen