Home > OS >  can applescript toggle choices in iTunes/Music form?
can applescript toggle choices in iTunes/Music form?

Time:06-06

I just migrated to Monterey from 10.14 and the migration destroyed all the display settings in my playlists. I have over 200 playlists, so I'm trying to write a script that will set them all to MY default setting, not Apple's default. I've got part of it working but I need help with the last bit.

so far I've done two things:

  1. created a keyboard shortcut to set the playlist view as Songs. The shortcut "command-shift-s" will now set the current playlist view as Songs. this works. (thanks to a helpful webpage).

  2. I've got a script which will iterate thru all my playlists and set them to view as Songs. (again thanks to the helpful webpage).

I need to include something in the script that will toggle the view options to what I want them to be. Toggling will work because every time I set a playlist to view as Songs the same options are selected so I know what needs to be toggled. The form with options looks like this: playlist view options form I want to toggle Album, Love, Rating and iCloud Download.

Trouble is, while I can get my applescript to open the form, I don't know how to toggle an item in the form, nor can I find the documentation on how to do it. I've tried recording mouse clicks using automator, but that went nowhere. Hopefully there is some simple syntax to toggle the items.

Here's what I have so far for my script. The script does only one playlist because I am testing it before trying it on my entire library. Iterating thru the playlists is something I can do without problem, I just need help with the form options. The delays are there so I can see what is happening step by step while testing.

tell application "Music"
activate
set theplaylist to playlist "test1"
delay 1
reveal theplaylist
try
-- use the keyboard shortcut to change the view to Songs, this works.
tell application "System Events" to keystroke "s" using {command down, shift down}
delay 1
-- open the view options form, this works.
tell application "System Events" to keystroke "j" using {command down}
-- What do I put here to toggle the checkmark for Album, Love, etc.?????
delay 1
-- this next command closes the view options form. not sure I will need this when I am going thru the entire set of playlists. 
-- It may be enough to just reveal the next playlist.
tell application "System Events" to keystroke "w" using {command down}
end try 
end tell

Any help would be appreciated. thanks to all.

CodePudding user response:

What do I put here to toggle the checkmark for Album, Love, etc

Here's a complete example for just the Artist column, and you can fill it out from here:

tell application "Music"
    activate
    tell application "System Events"
        tell process "Music"
            try
                click (get menu item "Show View Options" of menu 1 of menu bar item "View" of menu bar 1)
            end try
            get checkbox "Artist" of scroll area 1 of window 1
            if value of result = 0 then
                click result
            end if
        end tell
    end tell
end tell

CodePudding user response:

Thanks to matt for the code example. Here is the final program which goes thru all user playlists and sets them to "Songs" and sets the view options.

matt should get the credit for answering. I'm new to this site and I haven't figured out how to do that. my apologies if I've done it wrong.

the delays were needed in some cases and I enventually added them thru the whole program.

    tell application "Music"
        activate
        -- first user playlist is the music library so skip it
        repeat with i from 2 to (count of every user playlist)
            reveal user playlist i
            delay 0.1
            tell application "System Events"
                tell process "Music"
                    tell application "System Events" to keystroke "s" using {command down, shift down}
                    delay 0.1
                    tell application "System Events" to keystroke "j" using {command down}
                    delay 0.1
                    --              click (get menu item "Show View Options" of menu1 of menu bar item "View" of menu bar 1)
                    -- the above line worked but for some reason occasionally crashed. using cmd-j did not. I don't understand that and it could have been caused by something entirely unrelated.

                    get checkbox "Album" of scroll area 1 of window 1
                    if value of result = 1 then
                        click result
                    end if
                    delay 0.1
                    get checkbox "Track Number" of scroll area 1 of window 1
                    if value of result = 0 then
                        click result
                    end if
                    delay 0.1
-- put in as many options as you like 
                    tell application "System Events" to keystroke "w" using {command down}
                end tell
            end tell
        end repeat
    end tell
  • Related