I have an iTunes playlist I previously backed up into a text file which is in the following format:
"title","artist","track number","album"
I created a sample file using four of these tracks:
"Ritual","Chick Corea Elektric Band II","9","Paint The World"
"Risk","Deftones","9","Diamond Eyes"
"Risveglio","Goblin","10","Zombi"
"Ritual","Ashes Divide","8","Keep Telling Myself It's Alright"
All the tracks from this playlist are currently in iTunes. I want to add each of these tracks into a playlist using AppleScript. I've been able to do it with a single item (example: title) with the following AppleScript:
-- set variables
set srcFile to "/Users/kjesso/Documents/=Scripting=/AppleScript/ipod_gym_playlist_track_names_sample.txt"
set allRecords to paragraphs of (read srcFile as «class utf8»)
set myPlaylist to "Test"
property okflag : false
-- check if iTunes is running
tell application "Finder"
if (get name of every process) contains "iTunes" then ¬
set okflag to true
end tell
if okflag then
-- if iTunes is running then do this
tell application "iTunes"
repeat with aRecord in allRecords
set results to (every file track of playlist "Library" whose name is aRecord)
repeat with aTrack in results
duplicate aTrack to playlist myPlaylist
end repeat
end repeat
end tell
else
-- if iTunes is not running do this
return "Unable to execute because iTunes is not running"
end if
However, if a duplicate track title is found from a different artist it will just take the first track since the script can't distinguish between different artists with only the "title" as the content. Do array's exist natively in AppleScript?
I assume this would need to be done with a Property List file? Upon further reading online, trying to create an array to do what I wanted (capture the track title, artist, album, etc.), I came across various threads like this stating it is better to use a Property List? I'm trying to achieve similar to what was done here , but instead of sending the output to a CSV file I want to send it to a playlist in iTunes.
I created the following sample property list file if I need to use a property list to achieve my goal:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>title</key>
<string>"Ritual"</string>
<key>artist</key>
<string>"Chick Corea Elektric Band II"</string>
<key>album</key>
<string>"Paint The World"</string>
</dict>
<dict>
<key>title</key>
<string>"Risk"</string>
<key>artist</key>
<string>"Deftones"</string>
<key>album</key>
<string>"Diamond Eyes"</string>
</dict>
<dict>
<key>title</key>
<string>"Risveglio"</string>
<key>artist</key>
<string>"Goblin"</string>
<key>album</key>
<string>"Zombi"</string>
</dict>
<dict>
<key>title</key>
<string>"Ritual"</string>
<key>artist</key>
<string>"Ashes Divide"</string>
<key>album</key>
<string>"Keep Telling Myself It's Alright"</string>
</dict>
</plist>
Anyone have any ideas on how to get this to work?
If you want the selection to be not only based on name, but also on Artist, Album... just add that filters like in example bellow.
Also, you may not need to check if iTunes is open or not. When script will run, if iTunes is not launched, the script will launch it directly. So except if you really don't want it to automatically open iTunes, do nothing.
Usually, you do not need to refer to specific playlist "Library". this is the default value.
set myPlaylist to "Test"
set {theTitle, theAlbum, theArtist} to {"Ritual", "Paint The World", "Chick Corea Elektric Band II"}
tell application "iTunes"
set myTracks to (tracks whose (name is theTitle) and (album is theAlbum) and (artist is theArtist))
duplicate (item 1 of myTracks) to playlist myPlaylist
end tell
I made the assumption that there is only 1 track matching title, album and artist (then I took this first and only item found). If you're not sure it is enough, you can add something else in the filter (year, duration,...).
About the plist or text file or list of records, just keep in mind that the key is that you use same method to write and read your file. So the right question is : how are you writing that file ? (I guess not manually !)
If you're building your file from another script, then it is much easier to save and read records (one record={title,album,artist} ). You will have nothing to do except read and write in your scripts. The only negative point is that you will not be able to read the file with a text editor...but it is required ?
In the example bellow the script read from txt file (same as your example) with 1 track per line and each values separated by ',' :
set textFile to choose file "Select your text file"
set myText to (paragraphs of (read textFile))
set AppleScript's text item delimiters to {","}
set myRecords to {}
repeat with aParagraph in myText
set MS to aParagraph as string
if (count of (text items of MS)) is 4 then
set the end of myRecords to {text item 1 of MS, text item 2 of MS, text item 3 of MS, text item 4 of MS}
else
-- skipt the record : invalid number of text item !
end if
end repeat
The result is a list of myRecords, each record being a list of 4 values {title, artist, trackNo, album}
I ended up finding the solution myself using a one-dimensional array (list) with help from this post . Here is the final result:
set srcFile to "/Users/kjesso/Documents/=Scripting=/AppleScript/ipod_mp3s_sample.csv"
set allRecords to paragraphs of (read srcFile as «class utf8»)
set myPlaylist to "Test"
tell application "iTunes"
repeat with aRecord in allRecords
set AppleScript's text item delimiters to ","
set arrayVar to text items of aRecord
set results to (every file track of playlist "Library" whose name is (item 1 of arrayVar) and artist is (item 2 of arrayVar) and track number is (item 3 of arrayVar) and album is (item 4 of arrayVar))
repeat with aTrack in results
duplicate aTrack to playlist myPlaylist
end repeat
end repeat
end tell
and here is the source file "ipod_mp3s_sample.csv" contents:
Ritual,Ashes Divide,8,Keep Telling Myself It's Alright
Ritual,Chick Corea Elektric Band II,9,Paint The World
Risk,Deftones,9,Diamond Eyes
Risveglio,Goblin,10,Zombi
here's a script I wrote that takes a CSV file with Track / Artist TAB delimited. I used it to search my own iTunes library from playlists i've found on Spotify.
I used a online exporter that exports your Spotify playlists to CSV. I had to clean up the playlists in excel first.
this also creates to separate log files:
1) of the tracks it found
2) the tracks it didn't find
(i use this list for a separate script that I have that
will then search for those songs on soulSeek for me)
here's the code:
-- Choose a file
set CSVstr to "Please locate your CSV file..."
set CSV_File to (choose file with prompt CSVstr) as text
set posixfilepath to (the POSIX path of CSV_File)
set posixfilepathLIST to emptylist(stringtolist(posixfilepath, "/"))
--set thename to item 1 of stringtolist(last item of posixfilepathLIST, ".")
set thename to (ListToString((reverse of (rest of (reverse of (stringtolist(last item of posixfilepathLIST, "."))))), "."))
set posixfilepathLISTCLEANED to reverse of (rest of (reverse of posixfilepathLIST))
set posixfilepathSTRINGCLEANED to ListToString(posixfilepathLISTCLEANED, ":")
--creates log file
set log_file_found to posixfilepathSTRINGCLEANED & ":" & (thename) & "_Matched_in_iTunes.txt"
global log_file_found
ClearLog(log_file_found)
WriteLog("Program Started....")
set log_file_notfound to posixfilepathSTRINGCLEANED & ":" & (thename) & "_NotFound_in_iTunes.txt"
global log_file_notfound
ClearLog(log_file_notfound)
WriteLog2("Program Started....")
property dialog_timeout : 3 -- set the amount of time before dialogs auto-answer.
-- Reading your file to memory
set CSV_Lines to every paragraph of (read file CSV_File from 1)
set AppleScript's text item delimiters to {""}
set Line_Values to {}
set {tids, text item delimiters} to {text item delimiters, " "}
set trackCount to (count CSV_Lines)
set foundCount to 0
set NotfoundCount to 0
set gov to 1
tell application "iTunes"
try
set opt to (display dialog "Enter Name for Playlist" default answer {thename} default button 2 with title " Spotify Recreate from CSV " with icon 1)
set newName to (text returned of opt)
set maxfind to (button returned of opt)
if newName is "" then error
on error
return
end try
try
set newnom to ("_WrangledFrom_" & newName)
if exists playlist newnom then delete playlist newnom
set newP to (make new playlist with properties {name:newnom})
set thePlaylist to view of the front browser window
set view of front window to newP
set listOfNames to {}
set listOfNamesNotFound to {}
end try
end tell
-- moves through the list one item at a time
repeat with i from 1 to trackCount
set savedTIDS to AppleScript's text item delimiters
set searchName to text item 1 of item i of CSV_Lines
set searchArtist to text item 2 of item i of CSV_Lines
set searchAll to (searchName & " - " & searchArtist) as text
set searchAll2 to (searchName & " " & searchArtist) as text
set tid to AppleScript's text item delimiters
#insert routine here:
tell application "iTunes"
--ignoring diacriticals and punctuation
--set big_list to (every file track whose name contains {searchName} and artist contains {searchArtist})
--set big_list to (every file track of playlist "Library" whose name contains searchName and artist contains searchArtist)
set big_list to (search library playlist 1 for {searchAll2} only songs)
--set search_results to (search library playlist 1 for searchAll2)
--set results to (every file track of playlist "Library" whose name contains searchName and artist contains searchArtist)
--end ignoring
set foundtracks to (count of items of big_list)
if (count of items of big_list) is greater than or equal to gov then
set foundCount to foundCount + 1
set foundtrackinfo to ("Found " & foundtracks & " For | " & searchAll)
delay 0.2
my WriteLog(foundtrackinfo)
copy foundtrackinfo to the end of listOfNames
repeat with a in big_list
duplicate a to newP
end repeat
else
set NotfoundCount to NotfoundCount + 1
set foundtrackinfo to ("Not Found | " & searchAll) as text
delay 0.1
my WriteLog2(foundtrackinfo)
copy foundtrackinfo to the end of listOfNamesNotFound
end if
end tell
end repeat
delay 2
tell application "iTunes"
set view of front window to newP
end tell
delay 2
try
tell application "System Events"
tell process "iTunes"
set frontmost to true
end tell
keystroke "a" using {control down, option down, command down}
delay 1
keystroke "a" using {option down, command down}
delay 1
end tell
end try
set AppleScript's text item delimiters to savedTIDS
set AppleScript's text item delimiters to {""}
display dialog ("Spotify CSV Wrangle Complete") buttons {"OK"} default button 1
my WriteLog("Program Ended...")
my WriteLog2("Program Ended...")
on WriteLog(text4Log)
set wri to open for access file log_file_found with write permission
write (text4Log & return) to wri starting at eof
close access wri
end WriteLog
on WriteLog2(text4Log)
set wri to open for access file log_file_notfound with write permission
write (text4Log & return) to wri starting at eof
close access wri
end WriteLog2
on ClearLog(clear_log_file)
set clearLF to open for access file clear_log_file with write permission
set eof of clearLF to 0
close access clearLF
end ClearLog (*
--Progress Bar Subroutine
--my SetupProgress([[linked-template:text]], 0, "Processing Data...", "Preparing to process.")
on SetupProgress(SPTotalCount, SPCompletedSteps, SPDescription, SPAdditionalDescription)
set progress total steps to {SPTotalCount}
set progress completed steps to {SPCompletedSteps}
set progress description to {SPDescription}
set progress additional description to {SPAdditionalDescription}
end SetupProgress
*)
on emptylist(klist)
set nlist to {}
set dataLength to length of klist
repeat with i from 1 to dataLength
if item i of klist is not "" then
set end of nlist to (item i of klist)
end if
end repeat
return nlist
end emptylist
on ListToString(theList, delim)
set oldelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set alist to theList as string
set AppleScript's text item delimiters to oldelim
return alist
end ListToString
on stringtolist(theString, delim)
set oldelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set dlist to (every text item of theString)
set AppleScript's text item delimiters to oldelim
return dlist
end stringtolist
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.