Search code examples
macosapplescriptmp3itunes

AppleScript Folder Action - Add Only MP3's from Download folder Automatically to iTunes


I am trying create an applescript that will automatically add only downloaded MP3's from my download folder as a Folder Action. I found the following script online and it works great except for two things: iTunes launches anytime I download anything from the Internet and also adds pretty much any file that iTunes is willing to add from that download folder. I've tried modifying the code about 15 different times and every time either iTunes does not launch, or iTunes launches for any file downloaded and does not add the file

The original code is:

on adding folder items to my_folder after receiving the_files
    repeat with i from 1 to number of items in the_files
        tell application "iTunes"
            launch
            try
                set this_file to (item i of the_files)

                add this_file

            end try
        end tell
    end repeat
end adding folder items to 

Thanks for the help!


Solution

  • To do this, you need to use if/then logic to filter for the files you want. Something along the lines of this (which I did not test, so the syntax may be slightly off)...

    on adding folder items to my_folder after receiving the_files
        repeat with i from 1 to number of items in the_files
            set theCurrentFile to item i of the_files
            if (name extension of (info for theCurrentFile)) = "mp3" then
                tell application "iTunes"
                    launch
                    try
                        set this_file to (item i of the_files)
    
                        add this_file
    
                    end try
                end tell
            end if
        end repeat
    end adding folder items to 
    

    Or, why not use Automator to do this? It has an action for filtering files and an action for importing files into iTunes. And, you can create a Folder Action with it. See my attached screenshot.

    enter image description here

    Hope this helps.

    -Ben