Hi all,
I’ve created a script which adds mp3s to a playlist in iTunes as they are downloaded. I have it set up as a folder action. Unfortunately, it has only been working properly a certain percentage of the time.
As I couldn’t really think of another way to debug a Folder Action script, i tried adding some code to the script that would write activities to a logfile. However, it appears I totally broke it by doing so.
Any suggestion on how to fix it or better yet, a better way to debug it, would be greatly appreciated!
Here is the code:
-- set filetypes which you want iTunes to import
property myFiletypes : {".mp3", ".aac"}
on adding folder items to this_folder after receiving added_items
-- successReporting variable is used to report whether or not it was a successful import. 0 is off. 1 is on.
set successReporting to 1
-- get items
set myFinderItems to added_items
tell application "System Events"
set allItems to every item of myFinderItems
end tell
-- create string in the format of mm-Month-year
copy (current date) as string to z
copy (((offset of (the month of (current date)) in "jan feb mar apr may jun jul aug sep oct nov dec ") + 3) / 4) as integer to intMonth
if intMonth < 10 then copy "0" & intMonth as string to intMonth
copy (the year of (current date)) & "-" & intMonth & "-" & (the month of (current date)) to str
--create playlist title strings
copy str & " Singles" to singlesPlaylist
copy str & " Sets / Albums" to setsPlaylist
--check if the correct playlists exist, if not, creates them
tell application "iTunes"
if not (user playlist singlesPlaylist exists) then
make new user playlist with properties {name:singlesPlaylist}
tell application "Finder"
open for access (file "auto-import.log") with write permission
write ("Created playlist: " & singlesPlaylist & "
") to (file "auto-import.log") starting at eof as string
close access logFile
end tell
end if
if not (user playlist setsPlaylist exists) then
make new user playlist with properties {name:setsPlaylist}
tell application "Finder"
open for access (file "auto-import.log") with write permission
write ("Created playlist: " & setsPlaylist & "
") to (file "auto-import.log") starting at eof as string
close access logFile
end tell
end if
end tell
-- loop
tell application "Finder"
repeat with i from 1 to the count of allItems
try
-- get an item
set myFinderItem to (item i of allItems)
-- check if it is a folder
set myItemIsFolder to (kind of myFinderItem = "Folder")
-- if it isn't a folder...
if myItemIsFolder is false then
-- see if the file has the right extension
set myItemExtension to "." & (name extension of myFinderItem) as string
if myFiletypes contains myItemExtension then
--check to make sure it's not a mix
if ((size of myFinderItem) as integer) is less than 30000000 then
-- add track to iTunes
tell application "iTunes" to add myFinderItem to user playlist singlesPlaylist
else
--move to albums & mixes folder
move myFinderItem to (this_folder & ":!albums & mixes")
tell application "Finder"
open for access (file "auto-import.log") with write permission
write ("Moved file to albums & mixes folder: " & (name of file myFinderItem) & "
") to (file "auto-import.log") starting at eof as string
close access logFile
end tell
end if
end if
end if
-- check if user requested a positive report. defaults as 1 - reports shown.
if successReporting is 1 then
--tell application "iTunes" to display dialog "Your track(s) has been added to the iTunes Library."
tell application "Finder"
open for access (file "auto-import.log") with write permission
write ("Added file to " & singlesPlaylist & ": " & (name of file myFinderItem) & "
") to (file "auto-import.log") starting at eof as string
close access logFile
end tell
end if
on error
-- if there was an error report regardless of successReporting
tell application "Finder"
open for access (file "auto-import.log") with write permission
write ("Error adding: " & (name of file myFinderItem) & "
") to (file "auto-import.log") starting at eof as string
close access logFile
end tell
end try
end repeat
end tell
end adding folder items to
it appears to work fine up until it gets to the tell block where it loops through the added items.