Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate iTunes playlist from csv file

Could anyone offer a way to populate my playlist with songs from a csv file/text file formatted like this: song title,artist? I can do it for title alone but can't specify it must have a certain artist.

EDIT: Here is an example of how I'm getting them by titles:

set TheFile to read file "Macintosh HD:Applications:Automator stuff:01b iTunes Scripts:SongList.txt"
tell application "iTunes"
    set thePlaylist to playlist "SongList"
    try
        delete every track of thePlaylist
    end try
    set MySongs to paragraphs of (TheFile) -- read artist names (separated by newlines) from the file
    repeat with AnItem in MySongs -- get all tracks from each artist
        set AnItem to (contents of AnItem)
        if AnItem is not "" then try -- don't bother with empty names
            set MyTracks to (location of file tracks of playlist "Music" whose name is AnItem)
            --can also modify the above from "is" to "contains" or "_begins with_"
            add MyTracks to thePlaylist
        on error errmess -- oopsie (not found, etc)
            log errmess -- just log it
        end try
    end repeat
end tell
like image 673
tardy pigeon Avatar asked Apr 21 '18 17:04

tardy pigeon


People also ask

How do I import playlists into iTunes?

Go to iTunes on the new computer, then File-->Library-->import playlist. Click the file you just copied and the copied playlist will now appear in iTunes.

How do I transfer a playlist from an external hard drive to iTunes?

Step 1: Open iTunes > Plug in your external hard drive. Step 2: Click on File menu in iTunes > Choose Add to Library. Step 3: Select your external hard drive > Choose the music or music folder > Click on Open.

How do I import playlist to Iphone?

Connect your device to the computer. “Music Playlists” should be listed in the left pane. Select the playlist(s) you wish to sync, then drag them up to your device listed in the “Devices” section on the left pane. They will then sync to your device.


2 Answers

OK, figured it out! Couldn't work out how to work around titles with commas in them (which I have a few of), so I ended up using tab separating them instead. So, once I have my tab-separated file, this code did the trick:

set thisTSVFile to (choose file with prompt "Select the CSV file")
readTabSeparatedValuesFile(thisTSVFile)

set theList to readTabSeparatedValuesFile(thisTSVFile)

tell application "iTunes"
    set myPlaylist to playlist "Test1"
    set sourcePlaylist to playlist "Music"
end tell

repeat with i from 2 to number of items in readTabSeparatedValuesFile(thisTSVFile)
     --gets first column
    set theName to item 1 of item i of theList
     --gets second
    set theArtist to item 2 of item i of theList
    tell application "iTunes"
        duplicate (some track of sourcePlaylist whose name is theName and artist is theArtist) to myPlaylist
    end tell
    delay 0.1
end repeat

on readTabSeparatedValuesFile(thisTSVFile)
    try
        set dataBlob to (every paragraph of (read thisTSVFile))
        set the tableData to {}
        set AppleScript's text item delimiters to tab
        repeat with i from 1 to the count of dataBlob
            set the end of the tableData to (every text item of (item i of dataBlob))
        end repeat
        set AppleScript's text item delimiters to ""
        return tableData
    on error errorMessage number errorNumber
        set AppleScript's text item delimiters to ""
        error errorMessage number errorNumber
    end try
end readTabSeparatedValuesFile
like image 80
tardy pigeon Avatar answered Dec 24 '22 05:12

tardy pigeon


You can use ObjectiveC (or probably Swift too) with XCode to do the heavy lifting (parsing the files) then hit iTunes from there, although it will likely be a lot slower than running in the iTunes process through its script menu.

Here's some ObjectiveC code that gets the current track title; you can adapt the method to suit a more complicated script like populating a playlist.

+(NSString *)getTitle {
    return [self runAppleScriptAndReturnResult:@"Tell application \"iTunes\" \nreturn the name of the current track\nend tell"];
}

+(NSString *)runAppleScriptAndReturnResult:(NSString*)script {
    NSAppleScript *appleScript=[[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"with timeout of 3 seconds\n%@\nend timeout\n", script]];
    return [[appleScript executeAndReturnError:nil] stringValue];
}
like image 32
Charlie Avatar answered Dec 24 '22 07:12

Charlie