|Jan 11, 2012, 11:12 AM||#1|
Applescript replacing file problem
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,
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
Thanks in advance.
Last edited by topcat777; Jan 11, 2012 at 11:36 AM.
|Jan 11, 2012, 12:17 PM||#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.
MacBook Pro / OS X Mavericks (10.9.4) / Xcode 5.1.1 / RubyMotion FTW
|Jan 11, 2012, 02:42 PM||#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.
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
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|Applescript: How do I scan an image to a file?||moonman239||Mac Programming||2||May 11, 2014 04:40 PM|
|Opening a file in AppleScript||Chemical666||Mac Programming||2||May 7, 2014 06:47 PM|
|Formatting text file in AppleScript||dam316||Mac Programming||1||May 5, 2014 10:21 AM|
|Applescript, look after file don't work why?||Dirtyharrydev||Mac Programming||1||Jan 17, 2014 09:06 PM|
|applescript differentiate file from folder||dancks||Mac Programming||2||Jun 30, 2013 07:34 PM|
All times are GMT -5. The time now is 04:52 AM.