MacOS – iTunes 12: How to cycle through View Options with script/shortcut

applescriptitunesmacos

I'm trying to writing an applescript that cycles through Songs, Albums, Artists, Composers, Genres, and back to Songs.

Problem

There is nothing in applescript Dictionary with regards to this, so I have to resort to UIscripting/Accessibility. This is what I have so far:

tell application "System Events" to tell process "iTunes"

    set frontmost to true -- necessary
    delay 1 -- for last line to take effect, delete if you bind this to keyboard shortcut

    tell window 1 -- tell window "iTunes" only works when in fullscreen

        tell pop up button 2 to perform action "AXPress" -- equivalent of
        --keystroke "j" using {command down}

        tell UI element 1 of row 5 of table 1 of pop over 1 of pop up button 2 to perform action "AXPress"
        -- pressing Genres button, assuming Vies is not in Genres already, of course

    end tell
end tell

You can run this with Script Editor, or preferably for the purpose of the rest of this question, bind it to a keyboard shortcut and invoke it while in iTunes. When you run this, you see the pop up menu flash on your screen, right?

Now, here is the interesting part: Use your cursor to click the pop up button, move down and position the cursor on Artists.

Cursor

The cursor itself is omitted by OS X Screenshot function, but you can see Artists is in focus. Now press Esc to hide the menu, and invoke the script while the cursor in still in this position.

Bam! You now switched to Artists view. It turns out the 2nd perform action "AXPress" is performed under cursor because the pop up menu is not in focus after clicking the vie option button.

There is a simpler way to verify this problem/bug. 1) Click view option. 2) Press Down key a couple times. You would expect focus would move to Songs, and then Albums, right? Nope, you are just moving through library items in the background, all the while the menu is still in display in the foreground.

So, my question boils down to, how can I move cursor focus to this pop up menu?

UPDATE: Thanks @jackjr300 for the answer. This what my finished script looks like:

tell application "System Events"
    tell process "iTunes"
        set frontmost to true
        delay 1
        tell pop up button 2 of window 1
            click

            if value of attribute "AXDescription" is "Genres" then
                tell (select row 1 of table 1 of pop over 1) to click UI element 1
            end if
            if value of attribute "AXDescription" is "Songs" then
                tell (select row 2 of table 1 of pop over 1) to click UI element 1
            end if
            if value of attribute "AXDescription" is "Albums" then
                tell (select row 3 of table 1 of pop over 1) to click UI element 1
            end if
            if value of attribute "AXDescription" is "Artists" then
                tell (select row 4 of table 1 of pop over 1) to click UI element 1
            end if
            if value of attribute "AXDescription" is "Composers" then
                tell (select row 5 of table 1 of pop over 1) to click UI element 1
            end if

        end tell
    end tell
end tell

Best Answer

Use the select command to select the row.

tell application "System Events"
    tell process "iTunes"
        set frontmost to true 
        delay 1
        tell pop up button 2 of window 1
            click
            tell (select row 5 of table 1 of pop over 1) to click UI element 1
        end tell
    end tell
end tell