1

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!

tardy pigeon
  • 225
  • 6
  • 18
  • So if the line in SL is *"HelpTABThe BeatlesTAB1965"* does that mean **all** those 3 **exact** terms must appear in that exact order and nothing else in a file's name for it to be imported? – Mark Setchell Aug 20 '23 at 22:24
  • Also you should say if all the songs are in a single directory or in a hierarchy. And say what extensions they use. – Mark Setchell Aug 20 '23 at 23:14
  • @MarkSetchell So no, the order of the words isn't important but if all the words are there it should identify most of my songs. And to your second point — the folder 'General Music' contains folders that contain more folders so it will need to search recursively. Extension-wise, MP3 and m4a only. Thanks for your help! – tardy pigeon Aug 21 '23 at 01:57

1 Answers1

1

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks! All the other steps worked but this one: When I run $HOME/searcher.sh I get this error: /Users/.../searcher.sh: line 5: /SLtest: No such file or directory~ I've tried changing the reference to SLtest.txt but no luck. – tardy pigeon Aug 21 '23 at 11:35
  • 1
    Start a new **Terminal** and run `cd` to change to your HOME directory. Then type `ls SL*` to see all files whose names start with `SL`. Then edit your script so the last line is `done < THATFILENAME` – Mark Setchell Aug 21 '23 at 13:25
  • 1
    Note I am assuming you have placed the file `SL` in your HOME directory. – Mark Setchell Aug 21 '23 at 13:33
  • OK, I have done that on a test file and it seems to work - I get the following: Searching for title: '74-'75, artist: The Connells, year: 1993 Searching for title: '74: No, artist: The Magnetic Fields, year: 2017 Searching for title: '77, Life Ain't All Bad, artist: The Magnetic Fields, year: 2017 /Volumes/EXTERNALHD/Full backup/General Music/02 Rock/02E Rock 2010-2019/08 2017/50 Song Memoir - Magnetic Fields - 2017/'77, Life Ain't All Bad - The Magnetic Fields - 2017.mp3 etc! – tardy pigeon Aug 22 '23 at 19:59
  • I can't read that in a comment because it's unformatted. Please click `edit` under your question and add it in there properly formatted. Thank you. – Mark Setchell Aug 22 '23 at 20:10
  • Ideally, after each line *"Searching for..."*, there should follow one line with the full path that corresponds to that track. So it would be good to know what sort of percentage of your tracks it is actually finding. I mean, if there are 30 tracks in SL, there should be 60 lines in the output. – Mark Setchell Aug 22 '23 at 20:29
  • Hi - thanks, so yes I've used the script on the full file and of 7391 files it has found 5530, so around 75%. That number is not a problem and would be a real help to me as it is even. One thing I've noticed: it doesn't seem to process files with parentheses/brackets in very well. (About 3,000 of the titles it didn't find have () in them somewhere and some of the artists.). I hope that helps! – tardy pigeon Aug 24 '23 at 20:17
  • Ok, I wasn't expecting special characters in there. Try adding `-F` to the parameters of every `grep` so parentheses and brackets are not treated as metacharacters, e.g. `grep -Fi "$title" TRACKS | grep -Fi " $artist" | ...` – Mark Setchell Aug 24 '23 at 21:30
  • Thanks for that. It seems to pick up a few more but the ones it isn't adding I don't get why (I find I can find these if I search on Spotlight and put quotes around the title and artist so they do seem to be visible to Finder etc.: `Adiós Nonino (Piazzolla) Astor Piazzolla (Rodolfo Nerone) 1955 Aftermyth Of War (from Beyond the Fringe) Beyond The Fringe (Cast) (prod. George Martin) 1961 Ain't Misbehavin' (Waller, Razaf, Brooks) Louis Armstrong & His Orchestra 1929 Alfie (Bacharach, David) Cilla Black (Conducted By Bacharach) 1966` – tardy pigeon Aug 24 '23 at 22:55
  • I'm not sure what's causing this, but suspect it is the quoting on the line starting `osascript`. There may be another way to import songs into **Music** by selecting all the files in a directory rather than using `osascript`, so maybe you could make a temporary directory on your Mac somewhere and replacing the `osascript` line with a `copy` command to copy the song to the temp directory and then import everything from that directory at the end, then delete it. It would be `cp "$this" TEMPDIR` instead of `osascript...` – Mark Setchell Aug 25 '23 at 07:50