PDA

View Full Version : iTunes Library and Apple Script?


swimp
09-01-2003, 06:00 PM
Does anyone know how to make a small applescript (folder action?) that automatically adds any mp3 file that is dropped in the /home/music/ folder?

Thanks.

Lankhmart
09-01-2003, 09:31 PM
Here is a folder action script that should work for you once attached to the Music folder. It tests to make sure the dropped files are MP3s before sending them to iTunes, but you could extend it to accept AIFFs, WAVs, AACs, etc. if you wish.

on adding folder items to thisFolder after receiving addedItems
set mp3List to {}
repeat with i from 1 to (count of addedItems)
set anItem to item i of addedItems
tell application "Finder"
if (name extension of anItem is "mp3") or (file type of anItem is in {"MP3 ", "MPG3"}) then
copy anItem to the end of mp3List
else
--the file is not an MP3; reject it from the Music folder, and move it to the desktop.
move anItem to the desktop
end if
end tell
end repeat
tell application "iTunes" to add mp3List
end adding folder items to

swimp
09-08-2003, 08:42 AM
btw, can you add support for folders?

Lankhmart
09-08-2003, 10:00 PM
Sure. I've tested this version briefly with multiple, nested folder hierarchies, and it should work to an arbitrary depth. I removed the portion of the script that placed rejected files on the desktop, but such files are logged by the script into the rejectedList variable so you could add a bit of code to move them or list them or log them to a file or whatever you would like.

Edit: I improved the results reporting and turned it on by default. Now the script will display a dialog indicating how many MP3s were imported and how many files were rejected. If you dislike this feature, you can turn it off by commenting out the indicated line ("reportResults()").

global mp3List
global rejectedList

on adding folder items to thisFolder after receiving addedItems
set mp3List to {}
set rejectedList to {}
mp3Tester(addedItems)
tell application "iTunes" to add mp3List
--Comment out the next line (by prepending two hyphens) to disable the dialog box reporting the results of the script.
reportResults()
end adding folder items to

on mp3Tester(itemList)
repeat with i from 1 to (count of itemList)
set anItem to item i of itemList
tell application "Finder"
if kind of anItem is "Folder" then
try
set folderContents to (every item of folder (anItem as string)) as alias list
on error
set folderContents to (every item of folder (anItem as string)) as alias
set folderContents to {folderContents}
end try
my mp3Tester(folderContents)
else if (name extension of anItem is "mp3") or (file type of anItem is in {"MP3 ", "MPG3"}) then
copy anItem to the end of mp3List
else
--the file is not an MP3; add it to the rejected list.
copy anItem to the end of rejectedList
end if
end tell
end repeat
end mp3Tester

on reportResults()
set mp3Count to count of mp3List
set rejectCount to count of rejectedList
if mp3Count = 1 then
set msg1 to ("" & mp3Count & " MP3 was added to your iTunes library.")
else if mp3Count > 1 then
set msg1 to ("" & mp3Count & " MP3s were added to your iTunes library.")
else
set msg1 to "No MP3s were added to your iTunes library."
end if
if rejectCount = 1 then
set msg2 to ("" & rejectCount & " non-MP3 file was rejected.")
else if rejectCount > 1 then
set msg2 to ("" & rejectCount & " non-MP3 files were rejected.")
else
set msg2 to "No files were rejected."
end if
display dialog (msg1 & return & return & msg2) buttons {"OK"} default button 1
end reportResults

Russ R.
09-08-2003, 10:29 PM
Apple has their own droplet - Add to iTunes Library available as part of the iTunes 10.1 Scripts on this page. (http://www.apple.com/applescript/itunes/) I used to have it in my Finder toolbar.

Edit: I just retried it, and it does still work with OS X 10.2.6 and iTunes 4.0.1.

swimp
09-09-2003, 07:24 AM
Thanks again Lankhmart - it works great!
I haven't tried Apple's yet.