Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

topcat777

macrumors newbie
Original poster
Aug 22, 2011
4
0
Hi Guys

Was wondering if any of you could help me. I've trawled the internet to find a solution to automating something at work. I need a script that will copy files into specific folders dependent on their file names. I found a script that seemed to work but one of the things I need it to do is replace any files in the destination folders if files of the same file name exist. I have a 'with replacing' term in there but it doesn't seem to work. I'm at a bit of a loss now.

Here's my code,

Code:
property One : "Macintosh HD:Users:macuser:Desktop:Test:One" as alias
property Two : "Macintosh HD:Users:macuser:Desktop:Test:Two" as alias
property Three : "Macintosh HD:Users:macuser:Desktop:Test:Three" as alias
property ErrorIn : "Macintosh HD:Users:macuser:Desktop:Test:ErrorIN" as alias
property FileIn : "Macintosh HD:Users:macuser:Desktop:Test:Review Images" as alias

try
	tell application "Finder" to set theFiles to files of FileIn
	set theList to files of theFiles
	repeat with aFile in theFiles
		set FileName to name of aFile as text
		try
			if FileName contains "one" then
				move aFile to One with replacing
			else if FileName contains "two" then
				move aFile to Two with replacing
			else if FileName contains "three" then
				move aFile to Three with replacing
			else
				move aFile to ErrorIn with replacing
			end if
		end try
	end repeat
end try

Any help will be appreciated.

Thanks in advance.

Thom
 
Last edited:

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
The with replacing option is from the Finder's dictionary, so you will need to target that application to use it. You are also throwing away all error notifications by putting everything in a try statement without any on error statements, so about the only other clue you left yourself would have been the Editor's syntax coloring of the replacing term.
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
I have rewritten your Applescript code, and tested it on OSX Lion, and it
seems to work OK, check your OS version is 10.5 or higher.

Also I have rewritten your folder paths in variables, instead of properties, but
you must make sure the folders exist, and are spelled correctly, if lower or
upper case are incorrect, then you will have to add an "Ignoring Case" block
to the code.

I have added a Delay to the repeat loop, you might have to adjust the delay time on your system, because the Finder app needs the time to perform the file move operation, before moving on to the next one, otherwise an error will occure.

Also check your system read write permissions, as this will also cause error problems.

Lastly I have added some Error trapping in the code as suggested by Red Menace, but you could add more to check each file exists, before attempting the move.

Code:
try
	set folderOne to ((path to desktop folder) & "One" as text) as alias
	set folderTwo to ((path to desktop folder) & "Two" as text) as alias
	set folderThree to ((path to desktop folder) & "Three" as text) as alias
	set ErrorInFolder to ((path to desktop folder) & "ErrorIn" as text) as alias
	set FileInFolder to ((path to desktop folder) & "Review Images" as text) as alias
on error errorMessage number errorNumber
	display alert errorMessage & space & errorNumber message ¬
		"An error occured while trying to access the desktopo folders." & return & ¬
		"Check the folder paths are entered & spelled correctly." as warning giving up after 60
	return
end try
tell application "Finder"
	set theFiles to every file in FileInFolder as list
	repeat with aFile in theFiles
		set fileName to name of aFile as text
		try
			if fileName contains "one" then
				move aFile to folderOne with replacing
			else if fileName contains "two" then
				move aFile to folderTwo with replacing
			else if fileName contains "three" then
				move aFile to folderThree with replacing
			else
				move aFile to ErrorInFolder with replacing
			end if
			delay (0.5)
		on error errorMessage number errorNumber
			display alert errorMessage & space & errorNumber message ¬
				"An error occured while trying to move the file " & aFile & "." & return & ¬
				"Check the file exists, or the delay time setting." as warning giving up after 60
		end try
	end repeat
end tell

hope this helps

Regards Mark
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.