jbwharris
jbwharris

Reputation: 720

If a dialog exists, click certain button with Applescript

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

Upvotes: 3

Views: 1776

Answers (1)

wch1zpink
wch1zpink

Reputation: 3142

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

Upvotes: 1

Related Questions