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!

3
  • 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? Commented Aug 20, 2023 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. Commented Aug 20, 2023 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! Commented Aug 21, 2023 at 1:57

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

10 Comments

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.
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
Note I am assuming you have placed the file SL in your HOME directory.
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!
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.