Hi all,
I’m a bit new to AppleScript, so I’m not quite familiar with it’s nuiances.
I wrote this script that is to be used as a folder action, and adds any file under 30mb to a certain iTunes playlist. The code works fine in a script with a specific file, but it’s not working as a folder action, and I can’t quite figure out how to debug it. Any help would REALLY be appreciated right now because I’m at my wits end!
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 intMonth & "-" & (the month of (current date)) & "-" & (the year 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}
end if
if not (user playlist setsPlaylist exists) then
make new user playlist with properties {name:setsPlaylist}
end if
end tell
-- loop
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
tell application "Finder" to 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
tell application "Finder" to 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
display dialog "added to itunes"
--tell application "iTunes" to add myFinderItem to user playlist singlesPlaylist
else
--move to albums & mixes folder
display dialog "moved to mixes"
-- move myFinderItem to (this_folder & ":!albums & mixes")
-- display dialog "Moved " & myFinderItem & " to albums & mixers folder"
end if
end if
end if
-- check if user requested a positive report. defaults as 1 - reports shown.
if successReporting is 1 then
display dialog "Your track(s) has been added to the iTunes Library."
end if
on error
-- if there was an error report regardless of successReporting
display dialog "Could not add file to iTunes library"
end try
end repeat
end adding folder items to