Applescript replacing file problem

Discussion in 'Mac Programming' started by topcat777, Jan 11, 2012.

  1. topcat777, Jan 11, 2012
    Last edited: Jan 11, 2012

    macrumors newbie

    Joined:
    Aug 22, 2011
    #1
    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
     
  2. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    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.
     
  3. macrumors member

    Mark FX

    Joined:
    Nov 18, 2011
    #3
    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
     
  4. thread starter macrumors newbie

    Joined:
    Aug 22, 2011

Share This Page