Auto copy files based in file extension in folders

Hi All,

I’m new to Apple script and I’m having trouble with what I want to do and hoping for some help.

I want to be able to drop a folder into another folder, then have a folder watcher script open the folder and copy the contents to either a movie or music folder as a sub folder within the folder it was dragged into. To work the script will need to look in the folder and assess the file extension of the files in the folder. I was also wondering if there would need to be some kind of delay needed until the dragged folder had completed copying from its original location to avoid errors.

This is the code I have so far, any help would de greatly appreciated!

Thanks


property movieFiles : {"mov", "m4v", "mkv", "avi"}
property musicFiles : {"mp3, "flac""}

on adding folder items to theFolder after receiving theFiles
	repeat with aFile in theFiles
		tell application "System Events"		
			if name extension of aFile is in movieFiles then
				copy aFile to the folder "My Book for Mac:Torrent Downloads:Movies"
			else if name extension of aFile is in musicFiles then
				copy aFile to the folder "My Book for Mac:Torrent Downloads:Music"
			end if		
		end tell
	end repeat
end adding folder items to

You code seems fine apart from the copy command. duplicate is the command name for copying files (as in the Finder). copy is an AppleScript command to copy contents of variables or objects. It sometimes needed when AppleScript’s set is is only moving a reference and not copying its contents.

Hello,

Thanks for the tips, I made some changes as below but its still not working :frowning:

Any ideas why?


property movieFiles : {"mov", "m4v", "mkv", "avi"}
property musicFiles : {"mp3"}

on adding folder items to theFolder after receiving theFiles
	repeat with aFile in theFiles
		tell application "Finder"
			if name extension of aFile is in movieFiles then
				display dialog "It recognised the movie"
				duplicate aFile to the folder "My Book for Mac:Torrent Downloads:Movies"
			else if name extension of aFile is in musicFiles then
				duplicate aFile to the folder "My Book for Mac:Torrent Downloads:Music"
			end if
		end tell
	end repeat
end adding folder items to

Model: Mac Mini late 2012
Browser: Safari 601.3.9
Operating System: Mac OS X (10.10)

First version using the Finder.

property movieFiles : {"mov", "m4v", "mkv", "avi", "mp4"} # I added mp4
property musicFiles : {"mp3"}
(*
# Instructions used for testing
set theFolder to ((path to desktop as text) & "receiver:") as alias
tell application "Finder" to set thefiles to files of theFolder as alias list
*)

on adding folder items to theFolder after receiving thefiles
	set baseFolder to "My Book for Mac:Torrent Downloads:"
	# Here is the storage folder used in my tests
	set baseFolder to (path to desktop as text) & "final storage:Torrent Downloads:"
	repeat with aFile in thefiles
		tell application "Finder"
			set nameExt to name extension of aFile
			if nameExt is in movieFiles then
				duplicate aFile to the folder (baseFolder & "Movies")
				
			else if nameExt is in musicFiles then
				duplicate aFile to the folder (baseFolder & "Music")
				
			end if
		end tell
	end repeat
end adding folder items to

It works but it’s awfully slow - not a surprise.
The original files remain in theFolder

Second version using System Events

property movieFiles : {"mov", "m4v", "mkv", "avi", "mp4"} # I added mp4
property musicFiles : {"mp3"}
(*
# Instructions used for testing
set theFolder to ((path to desktop as text) & "receiver:") as alias
tell application "Finder" to set thefiles to files of theFolder as alias list
*)

on adding folder items to theFolder after receiving thefiles
	set baseFolder to "My Book for Mac:Torrent Downloads:"
	# Here is the storage folder used in my tests
	set baseFolder to (path to desktop as text) & "final storage:Torrent Downloads:"
	repeat with aFile in thefiles
		tell application "System Events"
			set nameExt to name extension of aFile
			if nameExt is in movieFiles then
				move aFile to the folder (baseFolder & "Movies")
				
			else if nameExt is in musicFiles then
				move aFile to the folder (baseFolder & "Music")
				
			end if
		end tell
	end repeat
end adding folder items to

It works, it’s fast but the originals are no longer in theFolder.
I was unable to use the duplicate command available in System Events.

Third version using the shell command mv.

property movieFiles : {"mov", "m4v", "mkv", "avi", "mp4"} # I added mp4
property musicFiles : {"mp3"}
(*
# Instructions used for testing
set theFolder to ((path to desktop as text) & "receiver:") as alias
tell application "Finder" to set thefiles to files of theFolder as alias list
*)
on adding folder items to theFolder after receiving theFiles
	set baseFolder to "My Book for Mac:Torrent Downloads:"
	# Here is the storage folder used in my tests
	set baseFolder to (path to desktop as text) & "final storage:Torrent Downloads:"
	repeat with aFile in theFiles
		tell application "System Events"
			set nameExt to name extension of aFile
		end tell
		if nameExt is in movieFiles then
			--display dialog "It recognised the movie"
			do shell script "mv " & quoted form of POSIX path of aFile & " " & quoted form of POSIX path of (baseFolder & "Movies")
		else if nameExt is in musicFiles then
			do shell script "mv " & quoted form of POSIX path of aFile & " " & quoted form of POSIX path of (baseFolder & "Music")
		end if
	end repeat
end adding folder items to

It works, it’s faster but as in second version the original files are no longer in theFolder.
If you want to keep the originals in theFolder, replace
do shell script "mv "
by
do shell script "cp "
It’s not so fast than cp but it’s faster than the Finder version.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) mardi 26 janvier 2016 19:00:27