--
-- SUPER PLAYLIST SYNC
--
global path_to_folder
property track_separator : " - "
property extension_separator : "."
-- =========================================
-- if iTunes is not running, then exit
--tell application "System Events" to if (name of every process) does not contain "iTunes" then return
-- init some lists...
set filenames_in_folder to {}
set track_filenames to {}
set track_locations to {}
tell application "Finder"
--activate
--hide any other active application windows
--set visible of every process whose name is not "Finder" to false
set path_to_folder to (choose folder)
end tell
tell application "iTunes"
set current_view to get view of first window
set the_playlist to get current_view's name
end tell
tell application "Finder"
try
repeat with this_file_alias in (get every file of folder (path_to_folder))
set end of filenames_in_folder to my get_filename_from_filepath(this_file_alias as string)
end repeat
end try
end tell
tell application "iTunes"
repeat with this_track in (get every track of playlist the_playlist)
if class of this_track is file track then
try
set loc to this_track's location
set end of track_locations to (loc as string)
--set end of track_filenames to my get_filename_from_filepath(loc)
set end of track_filenames to my get_artist_title_from_track(this_track)
end try
end if -- not file track
end repeat
end tell
-- check if any files in folder are in the playlist...otherwise delete
repeat with this_file in filenames_in_folder
if this_file is not in track_filenames then delete_from_folder(this_file)
end repeat
-- check if any file tracks are in the folder...otherwise add to folder
repeat with i from 1 to length of track_filenames
set this_file_track to item i of track_filenames
if this_file_track is not in filenames_in_folder then add_to_folder(item i of track_locations, this_file_track)
end repeat
-- HANDLERS
to get_filename_from_filepath(fp)
if (class of fp) is not string then set fp to (fp as string)
tell application "Finder" to return name of (get info for file fp)
end get_filename_from_filepath
to delete_from_folder(this_file)
try
do shell script "cd " & (quoted form of POSIX path of path_to_folder) & "; rm " & (quoted form of this_file)
on error m
log m
end try
end delete_from_folder
to add_to_folder(this_file, new_file)
try
-- set new_name to this_file as string
-- display dialog "new name:" & new_name buttons {"OK"}
-- set new_name to quoted form of POSIX path of new_name
-- display dialog "posix name:" & new_name buttons {"OK"}
set new_name to (path_to_folder & new_file) as string
-- set new_name to quoted form of POSIX path of new_name
do shell script "cp " & (quoted form of POSIX path of this_file) & " " & (quoted form of POSIX path of new_name)
on error m
log m
end try
end add_to_folder
to get_artist_title_from_track(t_track)
tell application "iTunes"
set loc to t_track's location
if (class of loc) is not string then set loc to (loc as string)
set track_artist to the artist of t_track --get the artist name
set track_title to the name of t_track --get the track title
end tell
tell application "Finder"
set track_extension to name extension of (get info for file loc) --get the extension of track's file name
end tell
return track_artist & track_separator & track_title & extension_separator & track_extension
end get_artist_title_from_track