Search code examples
applescriptitunesapple-music

How can I delete Apple Music tracks from my Library using AppleScript?


My Apple Music library is too big. I want to weed it out by removing a whole load of tracks that I have never listened to. I already did the same thing successfully with playlists but my script isn't working to remove tracks:

tell application "Music"
    activate
    set mytracks_list to (get the id of (every track whose loved is false and played count is 0 and rating is less than 60))
    repeat with mytrack_id in mytracks_list
        delete (the track whose id is mytrack_id)
    end repeat
end tell

The mytracks_list is populated with no problems. The error message I get is:

error "Can’t get track whose id = item 1 of {130098, [............] }

Am I doing something wrong, and can it be made to work?


P.S. This is what worked for my playlists:

tell application "Music"
    activate
    set myplaylists_to_delete to (get the name of every playlist whose name does not contain "Adrian" and name does not contain "Loved" and name does not contain "Shazam" and name does not contain "Library" and name is not "Music" and name does not contain "Recent" and name does not contain "5 Stars" and name does not contain "Duo")
    repeat with myplaylist in myplaylists_to_delete
        delete playlist myplaylist
    end repeat
end tell

Solution

  • Did you try:

    tell app "Music"
        delete every track whose loved is false and played count is 0 and rating is less than 60
    end tell
    

    Well-designed, well-implemented “AppleScriptable" apps can usually apply a command to multiple objects; you don’t need to iterate the objects yourself. (Hint: Apple event IPC = RPC + queries, not OOP.)