I've been trying to figure out how to conditionally click a dialog if it pops up. I have a script to copy a bunch of playlists to another one in Apple Music, but sometimes those playlists might have duplicates, which I'd like to skip. Problem is, it doesn't happen every time, only after you've copied a few already will it start encountering those duplicates. I had tried just pressing return to dismiss the dialog, but it seems to miss sometimes.
This is the logic I was hoping to achieve, but I can't seem to get it to identify whether there's a dialog or not.
if (dialog = true) then
click button "Skip" of window "Music"
end if
Update: I was able to adapt an approach from @wch1zpink answer to get to a solution. One of the problems I was running into was when exploring with UI Browser, sometimes the popup would be window 1 and sometimes it would be window 2, so it would fail when it wouldn't meet the criteria. So I made conditions for both instances and it seems to work now.
tell application "System Events"
if exists of UI element "Skip" of window 1 of application process "Music" then
click UI element "Skip" of window 1 of application process "Music"
else if exists of UI element "Skip" of window 2 of application process "Music" then
click UI element "Skip" of window 2 of application process "Music"
end if
end tell