applescriptitunesfinderspotlightapple-musickit

Automating import of songs from a list into iTunes?


All my music is kept in a folder on my external HD called 'General Music'.

I have a list of selected songs in a text file called 'SL'. I really want to import them into Music into a playlist called 'SL playlist'!

The list 'SL' is tab-separated and looks like this:

"Help[TAB]The Beatles[TAB]1965".

It does occasionally contain some special characters (:,å, that kind of thing). Is it possible to get Applescript to search my computer/hard drive for the string of words listed line-by-line in a text file like this "Help The Beatles 1965", and add any files into Music?

I realise sometimes the file won't find a song for whatever reason but I don't care about errors, I'm happy for it to try and move on if unsuccessful. I also realise there might be more than one music file found but again, I can deal with that. Really appreciate any help as moving from computer to another has left me a bit lost! (Now on Venture 13.5.)

Just to explain why I need this: I made a script to go through the list and copy the text from each line and then I would paste this into Spotlight and copy the file, but with 7000+ you can imagine how slow my progress is! Also, I would import all my files into 'iTunes' (Music now) but I have over 50,000 and I don't want to import them all!


Solution

  • I would do this in bash rather than Applescript so I hope you just want a result rather than worrying about the method. You can run the commands below by typing them into Terminal.

    So, first thing is to generate a list of all your tracks and save it in your HOME folder as TRACKS. I can't see what your disk is called or how things look on your machine but you want something very similar to:

    find "/Volumes/DRIVENAME/General Music" -type f > $HOME/TRACKS
    

    You should be able to check if it is correct with:

    cat $HOME/TRACKS
    

    Now you want a script to search for items from SL in there. So you need to save the following as $HOME/searcher.sh:

    #!/bin/bash
    while IFS=$'\t' read title artist year ; do
       echo Searching for title: $title, artist: $artist, year: $year
       grep -i "$title" TRACKS | grep -i "$artist" | grep "$year"
    done < $HOME/SL
    

    That assumes your SL file is in your HOME directory.

    If you use TextEdit to write the script, ensure it is plain text, see here.

    Now you need to make that file executable (necessary just once) with:

    chmod +x $HOME/searcher.sh
    

    Then you can run it with:

    $HOME/searcher.sh
    

    and see if it identifies the tracks. If that works, we can get those tracks into Music as a second step.


    Second step... don't start on this till first step is looking good. Also, reduce your SL file to just 2-3 lines in case it goes wrong so there's not too much to put right!

    So, if the above finds your files, we now need to add them to a playlist. So, manually make a playlist yourself, say "Old Stuff", then change the code above so it looks like this:

    #!/bin/bash
    while IFS=$'\t' read title artist year ; do
       echo Searching for title: $title, artist: $artist, year: $year
       this="$(grep -i "$title" TRACKS | grep -i "$artist" | grep "$year" | head -1)"
       osascript -e "tell application \"Music\" to add (\"$this\" as string) to playlist \"Old Stuff\""
    done < $HOME/SL