Search code examples
applescriptitunes

Generate iTunes playlist from text list of filenames


I have a text list of filenames of files all in different folders contained in one folder, Tardy's Music.

I haven't got the whole POSIX path, just the filenames (eg '01 Song Title.mp3').

Is there a way I can create an AppleScript which could search in Tardy's Music for each filename and add it to the playlist 'Best Of'?

I can generate playlists from the title of the song as seen in iTunes, but not the filename.

I have the titles in a text file, but could putt them in csv if that helps.

(Also, the files are all already in my iTunes library.)


Solution

  • You didn't mention what version of MacOS and iTunes you're running, so I'm offering the script below as an example of one way your objective can be met, on the understanding that it has been loosely tested on MacOS 10.13 using iTunes 12.7.

    By "loosely", I mean that I've run the script a few times in ideal conditions, where I supplied it with a list of mp3 file names I knew existed; were unique; and were already added to my main iTunes Library. That said, non-existence, non-uniqueness, and files not already added to your iTunes Library ought not to pose a problem.

    Here's the script, after which I'll walk you through it:

        set SongTitlesFile to "/Users/CK/Desktop/songs.txt"
        set MusicFolder to "/Users/CK/Music"
    
        set my text item delimiters to space
    
    
        set command to {¬
            "while read f;", ¬
            "do", ¬
            "find", quoted form of the MusicFolder, ¬
            "-iname \"$(printf '%q' \"$f\")\" -type f;", ¬
            "done", "<", quoted form of the SongTitlesFile}
    
        do shell script (command as text)
    
        set SongFiles to the paragraphs of the result
        if SongFiles is {} then return "No matching files found."
    
    
        repeat with SongFile in the SongFiles
            set end of SongFiles to POSIX file SongFile as alias
            set SongFiles to the rest of SongFiles
        end repeat
    
    
        tell application "iTunes" to ¬
            add the SongFiles to make new playlist ¬
                with properties {name:"Best Of"}
    

    Before using this script, you will need to change the value of the first two variables, SongTitlesFile and MusicFolder. Enter the full paths (i.e. /Users/%you%/... and not ~/...) of:

    ① The file containing the list of song titles. I have assumed that the file is a plain text document, where each filename occupies a single line. Whitespace before or after the filename ought not to affect the result, but the ideal format would look like this:

    01 A Song I Like.mp3
    04 Second Song.m4a
    Song 3.mp3
    I Love This Song.mp3
     
    

    Leave a blank line at the end, as illustrated here, otherwise the last song in the list may be omitted from the search. The search is not case-sensitive, but is otherwise exact, in that whole filenames are required (including extension) for a match to be made.

    These are criteria that I settled upon whilst creating the script. Different criteria and matching strictness can be used if needed, but this didn't seem necessary given the description in your brief.

    If you need the file to take a different format, let me know and I can attempt to re-work the script to cater for it.

    ② The top-level folder containing your music in which these files are to be found (somewhere), i.e. the full path to Tardy's Music.

    My script uses the help of a shell command find to locate the files. It reads each line of the text file and searches for the file with that name, returning its full path. It will only return files, not folders, and uses case-insensitive name matching.

    If more than one file exists with the same filename (but at different locations), both songs will be returned and added to the final list.

    If no matches are found, the script terminates with a message to that effect that will appear in the results window of Script Editor.

    The repeat loop that follows takes the list of file paths and converts them from Posix paths (i.e. /Paths/That Look Like/This.mp3) to an alias type that iTunes can use.

    Finally, the script instructs iTunes to create a playlist called "Best Of", and add the songs to it.

    If the files have already been imported into your library, they won't be imported again (thankfully), and will merely be added to the playlist; if the files have not been imported to your library, in loose testing, the file is imported and added to your library, and added to the playlist.

    Let me know how it goes for you. If you have any queries, leave a comment and I'll get back to you.