Skip to content

Instantly share code, notes, and snippets.

@brendanjcaffrey
Created December 25, 2012 16:58
Show Gist options
  • Save brendanjcaffrey/4374188 to your computer and use it in GitHub Desktop.
Save brendanjcaffrey/4374188 to your computer and use it in GitHub Desktop.
A folder action that adds any audio files added to the folder to a specified playlist in iTunes.
-- folder action script that attempts to add files that appear in a folder to iTunes and then delete them.
-- installation: copy file into /Library/Scripts/Folder Action Scripts/, right click on the folder
-- you want this to run on (i.e. ~/Downloads/), click "Folder Actions Setup," and add the file as an action.
property music_exts : {"mp3", "m4a", "aif", "wav"}
property playlist_to_add_to : "New" -- change to the name of the playlist the file will be added to
on adding folder items to my_folder after receiving the_files
repeat with i from 1 to number of items in the_files
set added_file to (item i of the_files)
set added_file_info to the info for added_file
set should_delete to true -- this makes sure we only delete the file if it successfully adds it to iTunes
if the name extension of the added_file_info is in the music_exts then
tell application "iTunes"
launch
try
set the_playlist to playlist playlist_to_add_to -- the name of the playlist to add to
add added_file to the_playlist -- if you don't want to add it to a specific playlist, just remove "to the_playlist"
on error
set should_delete to false
display dialog "there was an error adding the new file to iTunes. the file has not been deleted."
end try
end tell
if should_delete is true then
tell application "Finder"
try
if exists file added_file then
delete file added_file -- moves it to the trash
end if
on error
display dialog "there was an error deleting the newly added file."
end try
end tell
end if
end if
end repeat
end adding folder items to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment