property type_list : {"MPG3", "MIDI", "AIFF", "AAC"}
property extension_list : {"mp3", "mid", "aif", "m4a"}
property add_counter : 0
property remove_counter : 0
global thefolder, makeplaylist
set add_counter to 0
set remove_counter to 0
tell application "Finder"
--### add tracks from the scanned folders to the itunes Library
try
set thefolder to folder "iTunes Scan Folders" of startup disk
set thefolder to folder "iTunes Scan Folders" of startup disk
set thealiases to every alias file of thefolder
on error
make new folder at startup disk with properties {name:"iTunes Scan Folders"}
error "A new folder called \"iTunes Scan Folders\" has been created on you startup disk. Just place aliases of the folders you would like to scan in that folder and run this script again."
end try
end tell
if (count of thealiases) = 0 then
error "No Aliases in the Scan Folder!"
end if
--### remove all missing tracks from the iTunes Library first =)
display dialog "Should I remove all missing tracks before adding new ones?" buttons {"Yes", "No", "Cancel"} default button {"No"} giving up after 10
set button_returned to button returned of the result
if button_returned = "" then set button_returned to "Yes"
if button_returned = "Yes" then
remove_missing()
end if
--### make playlists for imported mp3s?
display dialog "Add mp3s to the library, or make a playlist for each imported folder?" buttons {"Lib", "Playlist", "Cancel"} default button {"Lib"} giving up after 10
set button_returned to button returned of the result
if button_returned = "" then set button_returned to "Lib"
if button_returned = "Playlist" then
set makeplaylist to true
else
set makeplaylist to false
end if
repeat with thealias in thealiases
tell application "Finder"
set this_folder to original item of thealias as alias
set deepcount to character 1 of (name of thealias as text)
try
set deepcount to deepcount as integer
on error
set deepcount to 0
end try
end tell
if deepcount = 0 then
tell application "Finder" to set playlist_name to name of this_folder
process_item(this_folder, playlist_name)
else
process_folder(this_folder, deepcount)
end if
end repeat
--### write statistics to a logfile
write_log(thefolder, "action.log", "")
on process_folder(this_item, deepcount)
tell application "Finder"
set these_items to every folder of this_item
set deepcount to deepcount - 1
end tell
repeat with this_item in these_items
if deepcount = 0 then
tell application "Finder" to set playlist_name to name of this_item
process_item(this_item, playlist_name)
else
process_folder(this_item, deepcount)
end if
end repeat
end process_folder
on process_item(this_item, playlist_name)
try
tell application "iTunes"
if playlist playlist_name exists then
set change_string to true
else
set change_string to false
end if
end tell
if change_string = true then
set playlist_name to (playlist_name & " " & (make_datestring(current date))) as text
end if
tell application "iTunes"
set prev_libcount to count of every track of playlist 1 of source 1
if makeplaylist = true then
set theplaylist to make new playlist with properties {name:playlist_name}
set this_track to add (this_item as alias) to theplaylist
else
set this_track to add (this_item as alias) to playlist 1 of source 1
end if
set aft_libcount to count of every track of playlist 1 of source 1
set add_counter to add_counter + (aft_libcount - prev_libcount)
end tell
on error msg
write_log(thefolder, "action.log", msg)
end try
end process_item
on remove_missing()
tell application "iTunes"
set the stored_setting to fixed indexing
set fixed indexing to true
set sourcename to name of source 1
tell source sourcename
set libname to name of playlist 1
tell playlist libname
repeat with i from the (count of tracks) to 1 by -1
try
if the location of track i is missing value then
delete track i
set the remove_counter to the remove_counter + 1
end if
end try
end repeat
end tell
end tell
set fixed indexing to the stored_setting
end tell
end remove_missing
on write_log(thelocation, logname, logtxt)
set thelocation to POSIX path of (thelocation as alias)
if logtxt = "" then
do shell script "echo `date \"+%y-%m-%d %H:%M:%S\"` \\ \\ \\ Tracks removed: " & remove_counter & "|cat>>" & quoted form of thelocation & quoted form of logname
do shell script "echo `date \"+%y-%m-%d %H:%M:%S\"` \\ \\ \\ Tracks added: " & add_counter & "|cat>>" & quoted form of thelocation & quoted form of logname
else
do shell script "echo " & quoted form of logtxt & "cat>>" & quoted form of thelocation & quoted form of logname
end if
end write_log
--### some time features for duplicated playlists
on get_month_number(incomingDate)
copy incomingDate to b
set the month of b to January
set month_number to "0" & (1 + (incomingDate - b + 1314864) div 2629728) as text
return (characters -2 through -1 of month_number) as text
end get_month_number
on make_datestring(thedate)
set theday to characters -2 through -1 of (("0" & day of thedate) as text) as text
set thestring to (year of thedate) & "-" & get_month_number(thedate) & "-" & theday & " " & (hours of thedate) & ":" & minutes of thedate
end make_datestring