Search code examples
csvapplescriptitunes

Can I use Applescript to import songs from .csv list of song info into iTunes?


Is there a way I can use AppleScript to import songs from my Mac hard drive into iTunes using only a .tsv list containing tag information?

(I have too many files in my iTunes collection so not all of them sync correctly if I drag the folders on to import them.)

The .tsv list contains the following info [where , = TAB]: Title,Artist,Year.

Each file, whether mp3 or occasionally another format, is correctly tagged.

All the files are held in different folders in one big folder on the hard drive, General Music.

I hope that's enough information to solve the problem I'm flummoxed by!

Edit: Version - Mac OS Mohave 10.14.6

iTunes version: 12.9.5.5

Files just need to be added to iTunes main library, not copied to any folder. Some of the files might exist already in the iTunes library but many won't (this is the prob of having 25,000+ songs; iTunes seems to kick out some old ones when you add new ones).

The tsv does not include filenames, only tag information. Here are a few lines as a sample (you can't see the tabs but there's a tab after the song title and the artist):

Afternoon Delight Starland Vocal Band 1976

Against All Odds Phil Collins 1984

Aged & Mellow Blues Little Esther 1952


Solution

  • To solve I would suggest 2 steps:

    1. allocate the file (and its path) from info given in CSV
    2. add file (e.g. ~/pathTo/song.mp3) to iTunes

    Allocate audio file from song-info given in CSV

    You have to process (open & read) the CSV file line by line.

    For each line you have 2 options now to allocate and match the file:

    1. either prepare a list of file-paths (including folders and filename) beforehand, that you can match against the given CSV-records
    2. search on your drive for an audio file (e.g. with extension .mp3) that matches some of the CSV field's values:

    The matching from CSV-line to audio-files can use 2 criteria:

    • (A) in its name
    • (B) in its meta-info header (e.g. ID3-tags)

    For (1 and A) you could use find command in Terminal or Bash script:

    Find file(s) recursively and filter them by their path matching specific substring(s) - Unix & Linux Stack Exchange

    For (1 and B) there are utilities to search inside an audio file's tags:

    Search MP3/Ogg files by tags/parameters from the command line - Unix & Linux Stack Exchange

    Output ID3 tags from a group of MP3 files as a CSV file - Software Recommendations Stack Exchange, some of the utilities also run on MacOS, e.g. ffprobe, exifTool

    Add audio file to iTunes

    Is described here:

    Adding a song file to iTunes via the command line without playing the file - Ask Different

    A simple command in Terminal or a (Bash) script could do:

    cp ~/pathTo/song.mp3 "~/Music/iTunes/iTunes Media/Automatically Add to iTunes/"
    

    By copying (cp) or moving (mv) the file into iTune's special folder, it will be imported next time when opening iTunes.