I’m trying to make an Applescript that can be used as a Folder Action to watch the Downloads folder and:
– Open .mp3s in iTunes and append #single to track Comments, then delete original .mp3s.
I’m stuck at the appending to Comments part right now. Here’s what I have so far:
property myFiletypes : {".mp3"}
on adding folder items to this_folder after receiving added_items
set myFinderItems to added_items
tell application "System Events"
set allItems to every item of myFinderItems
end tell
repeat with i from 1 to the count of allItems
try
-- get an item
set myFinderItem to (item i of allItems as alias)
-- 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
set mySongName to myFinderItem as alias
-- add track to iTunes
tell application "iTunes"
add mySongName
end tell
tell application "Finder" to delete file myFinderItem
end if
end if
on error
display dialog "Error"
end try
end repeat
end adding folder items to
property myFiletypes : {"mp3"}
on adding folder items to this_folder after receiving added_items
repeat with anItem in added_items
try
-- see if the file has the right extension
if myFiletypes contains (name extension of (info for anItem)) then
-- add track to iTunes
tell application "iTunes"
set addedTrack to add anItem
set trackComment to comment of addedTrack
if trackComment is "" then
set comment of addedTrack to "#single"
else
set comment of addedTrack to trackComment & return & "#single"
end if
end tell
tell application "Finder" to delete anItem
end if
on error e
display dialog "Error: " & e
end try
end repeat
end adding folder items to
Thank you, but it’s giving an error and not working consistently at all. Sometimes it won’t add all the songs, sometimes it comments and sometimes not. Odd.
This is working in every way EXCEPT it is not deleting the .mp3 file from the ‘Downloads’ folder. Any help?
property myFiletypes : {"mp3"}
on adding folder items to this_folder after receiving added_items
repeat with anItem in added_items
try
-- see if the file has the right extension
if myFiletypes contains (name extension of (info for anItem)) then
-- add track to iTunes
tell application "iTunes"
set addedTrack to add anItem
-- add grouping to track
set trackGrouping to grouping of addedTrack
if trackGrouping is "" then
set grouping of addedTrack to "singles"
else
set grouping of addedTrack to trackGrouping & "singles"
end if
end tell
tell application "Finder" to delete anItem
end if
end try
end repeat
end adding folder items to
You could always try this. (Due to time restrictions I’ve not managed to test it out yet)
property myFiletypes : {"mp3"}
on adding folder items to this_folder after receiving added_items
repeat with anItem in added_items
try
-- see if the file has the right extension
if myFiletypes contains (name extension of (info for anItem)) then
-- add track to iTunes
tell application "iTunes"
set addedTrack to add anItem
-- add grouping to track
set trackGrouping to grouping of addedTrack
if trackGrouping is "" then
set grouping of addedTrack to "singles"
else
set grouping of addedTrack to trackGrouping & "singles"
end if
end tell
do shell script ("rm " & anItem)
(* Please note that this command needs a path in the form
/Users/[username/Downloads...
and NOT
Macintosh HD:Users:username...
so adapt 'anItem' to suit if it dosen't work.*)
end if
end try
end repeat
end adding folder items to
Hello.
I have looked at your code, and can’t really believe why it won’t delete that file. Maybe Finder is specially picky about
getting a reference for the delete command, or maybe it is something else.
I have “instrumented” your code to return an error message, if something goes wrong during deletion, and also
made a reference out of the argument to the delete command.
Please post the error message and error number if it still doesn’t work. If it does work, then you can remove the
inner try block expect the new delete statement.
property myFiletypes : {"mp3"}
on adding folder items to this_folder after receiving added_items
repeat with anItem in added_items
try
-- see if the file has the right extension
if myFiletypes contains (name extension of (info for anItem)) then
-- add track to iTunes
tell application "iTunes"
set addedTrack to add anItem
-- add grouping to track
set trackGrouping to grouping of addedTrack
if trackGrouping is "" then
set grouping of addedTrack to "singles"
else
set grouping of addedTrack to trackGrouping & "singles"
end if
end tell
try
tell application "Finder" to delete file (anItem as Unicode text)
on error e number n
display dialog e & " : " & n & " File name : " & (anItem as Unicode text)
end try
end if
end try
end repeat
end adding folder items to
McUsr, I have been trying your script for a few days and not only does it not delete the file, it also does not edit the ‘Grouping’ tag OR show any errors?
Fistoprince, I have not tried yours because I do not really understand the specifics of what I am to do?
It is really your script, I have just instrumented it to pinpoint out where the error is. Since the previous didn’t give any error messages, please try the new one.
Try to install this one, and watch what happens when you add an mp3 file. And please report back any error messages. If this script really doesn’t show up with any error messages, then everything should be all right.
property myFiletypes : {"mp3"}
on adding folder items to this_folder after receiving added_items
repeat with anItem in added_items
try
-- see if the file has the right extension
if myFiletypes contains (name extension of (info for anItem)) then
-- add track to iTunes
tell application "iTunes"
try
set addedTrack to add anItem as list
on error
tell me
activate
display dialog "There were errors adding the track " & addedTrack as text
end tell
end try
-- add grouping to track
set trackGrouping to grouping of addedTrack
if trackGrouping is "" then
set grouping of addedTrack to "singles"
else
set grouping of addedTrack to trackGrouping & "singles"
end if
end tell
try
tell application "Finder" to delete file (anItem as Unicode text)
on error e number n
tell me
activate
display dialog e & " : " & n & " File name : " & (anItem as Unicode text)
end tell
end try
else
tell me
activate
display dialog "No items to add to the Itunes library were found."
end tell
end if
end try
end repeat
end adding folder items to
Hello, and that would be when there are different items than mp3 files that are added to the Downloads folder?
Silence is consent or what they say, so I just post this, positively assuming that everything will work fine from now;)
The main error in your script was that iTunes expected the item to be added of type list.
property myFiletypes : {"mp3"}
on adding folder items to this_folder after receiving added_items
repeat with anItem in added_items
try
-- see if the file has the right extension
if myFiletypes contains (name extension of (info for anItem)) then
-- add track to iTunes
tell application "iTunes"
set addedTrack to add anItem as list
-- add grouping to track
set trackGrouping to grouping of addedTrack
if trackGrouping is "" then
set grouping of addedTrack to "singles"
else
set grouping of addedTrack to trackGrouping & "singles"
end if
end tell
tell application "Finder" to delete file (anItem as Unicode text)
end if
end try
end repeat
end adding folder items to
Yes, it does display “No items to add to the Itunes library were found” when something other than an .mp3 is added, BUT it displays it also when an .mp3 file is added.
In other words, every time.
Also, it fires up when downloading begins, and not after a downloaded file is completed.
I really doesn’ understand why it gives a message about not being added when it is being added, if you wait a little moment, I’ll add a handler to which takes care of the busy issue.
Here you are, you should not receive any error messages regarding “download not completed” or whatever with this one.
It would be nice to know if this version works so that all the mp3 files gets deleted from your downloads folder after having been added to iTunes.
I couldn’t help myself, but make a version in order to make the deletions work properly.
Hello Dozens, I’m sorry, I edited the code online, while adding the on error statement, I have added the missing ampersand in the code above. Please try it, I’m very anxious about this!