3

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
1
  • 1
    Without testing I'm not completely sure, however, assuming it's a modal (dialog) window, you will not be able to script it closing from the script that started the copy process as that script can do nothing else until the user responds to the modal dialog window. Is it a free standing dialog window or is it being display in Notification Center? If the latter there may be a third-party workaround. However, the other option is to code it so it only copies items that do not already exist in the target. Commented Nov 2, 2020 at 20:07

1 Answer 1

1

Here is a solution I came up with a while back for some of my own projects. It will require saving this following AppleScript code as a stay open application. It’s function will be to handle all of the “clicking of the skip buttons” whenever that dialog opens while using Music.app. For purposes of this, I saved this as an application and named it “Click Skip.app”, in Script Editor.

As soon as you finished saving “Click Skip.app", go right over to System Preferences/ Security & Privacy/Privacy and add it to the list of applications allowed to control your computer

on idle
    set musicRunning to application "Music" is running
    if not musicRunning then quit
    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"
        end if
    end tell
    return 0.1 -- in seconds
end idle

on quit
    --  Executed when the script quits
    continue quit -- allows the script to quit
end quit

Next: Use this following AppleScript code and insert your Music.app commands into a new Script Editor document. This will be your main script. It will launch your “Click Skip.app”, then your Music code, then quits “Click Skip.app” when your script completes its run.

tell application "Music" to activate

tell application "Click Skip" to activate

tell application "Music"
    
    -- Insert Your Code For 
    -- Copying Music To Playlists
    
end tell

tell application "Click Skip" to quit
Sign up to request clarification or add additional context in comments.

1 Comment

I was able to grab a piece of your solution and use it right off. The if exists of UI element was what I was missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.