1

I'm trying to search the result of an AppleScript to determine if a string appears.

Running this code:

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

Results 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"}

Now I would like to search this result to see if "Files Synced" is present. However running

 myResult contains "Files Synced"

Gives me a printout of the entire result again. How do I do search this result to determine if a string is present?

1 Answer 1

0

You got the same printout both times because you didn't remove the return myResult line after your first run. return will always terminate a script when it reaches this command.

▸ Additionally, change this:

    set myStatus to menu items of menu 1

to this:

    set myResult to name of menu items of menu 1

▸ Delete this line:

    get menu items of menu 1

and this line:

    set myResult to result

(They're literally not doing anything.)

Your final script will look like this:

    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

which will return true or false.

Alternatively, without the explicit variable declarations (and using the AppleScript pre-defined result variable):

    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

If you need any clarification or have any further queries, don't hesitate to leave a comment and I'll get back to you. Please consider selecting this as your accepted answer if it helps to solve your problem.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .