Search code examples
applescriptitunes

Use paragraph-separated list to remove songs from iTunes playlist


I can get songs from a playlist based on a text file with a different title in each paragraph, but can I use another text file to delete any songs in playlist 'SongList' whose title matches one of the titles in my 'SongList' text file?

Know how to read the text file

set mySampleText to read file "Macintosh SSD:path:to:file.txt"

And how to get paras of it

set paras to paragraphs of mySampleText

But I can't find a way to delete any tracks who share a name with any line of mySampleText.

Can anyone help?

Thanks

Tardy


Solution

  • The script below find tracks with same title as in your text file and delete. Be careful, there is no reverse to the delete action !!!

    set FText to choose file "select your text file" -- select text file
    
    set mySampleText to read FText  -- read txt file
    set Paras to paragraphs of mySampleText
    
    tell application "iTunes"
    tell playlist "My preferred playlist"
    repeat with aSong in Paras -- loop to each title
        set myTracks to (every track whose name is aSong)
        if (count of myTracks) > 0 then -- only if found
            set myTrack to item 1 of myTracks -- take first item found
            Delete myTrack
        end if
    end repeat
    end tell
    end tell
    

    You may have to manage the case when you have multiple tracks with the same title. Script above only takes the 1st track found.

    I strongly reccomand that you replace the "delete" by "play" during debuting !! (to be safe).

    Last, but not least, make sure your txt file is properly encoded if you have special characters in your titles.