how to check if a specific UI element is in focus apple script I've searched a lot for this, but all answers talk about how bringing a whole window (or dialogue.. which is still a window really) into focus via activate etc.

But I couldn't find any way of detecting if a specific UI element was selected, in focus etc. 



I'm wrote an apple script that automatically jumps to alfred create custom web search, but the script failed when the features tab wasn't selected by default.. so this is what my code looks like:

tell application "System Events"
    keystroke "," using command down
    delay delay_time
    tell process "Alfred Preferences"
        delay delay_time
        -- hack: to ensure that the the "Default results" button isn't selected
        -- by default, we select another tab first, then select the "features" tab
        click button "Appearance" of toolbar 1 of window 1
        click button "Features" of toolbar 1 of window 1
        delay delay_time
        UI elements of scroll area 1 of window 1
        -- bring the left menu to focus first (see hack above, without hack this would work sometimes only)
        keystroke tab


I'd much rather not do this nasty hack (which causes the app to flicker rapidly between tabs whenever i call the shortcut).. i'd rather make a simple if else statement.. ideas?
This definitely works on my system running Alfred 3.  You may need to adjust the delay times but as they are now, this script works for me.

activate application "Alfred Preferences"
delay 1.5
tell application "System Events"
    tell process "Alfred Preferences"
        click button "Features" of toolbar 1 of window "Alfred Preferences" of application process "Alfred Preferences" of application "System Events"
        delay 1
        set selected of row 3 of table 1 of scroll area 1 of window "Alfred Preferences" of application process "Alfred Preferences" of application "System Events" to true
        delay 1
        click button "Add Custom Search" of tab group 1 of window "Alfred Preferences" of application process "Alfred Preferences" of application "System Events"
    end tell
end tell