Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Need help with simple AppleScript.

Need help with simple AppleScript.
Thread Tools
wildside
Fresh-Faced Recruit
Join Date: Jul 2008
Status: Offline
Reply With Quote
Jul 18, 2008, 02:12 PM
 
Is this the right place to post this?

I need help with a simple applescript.

I need to copy files from FolderA to FolderB

I need to overwrite files from FolderA if they exist in FolderB and ONLY if they are the same exact date or older.

I have been working on a bash shell script with no avail.

Can anyone help me do this with AppleScript?

thanks
     
zombie punk
Dedicated MacNNer
Join Date: Jul 2008
Status: Offline
Reply With Quote
Jul 18, 2008, 02:14 PM
 
Have you looked at automator?
     
Rainy Day
Grizzled Veteran
Join Date: Nov 2001
Location: Oregon
Status: Offline
Reply With Quote
Jul 18, 2008, 05:26 PM
 
Here’s a brute force way of doing it in Applescript (see notes below):

Code:
(* Moving files from folder A to B, only if files didn’t exist in B, or are newer than B *) set theSourcePath to "~/junk/" set theDestinationPath to "~/junk1/" tell application "Finder" set theSourceFolder to POSIX file (do shell script "echo " & theSourcePath) as alias set theDestinationFolder to POSIX file (do shell script "echo " & theDestinationPath) as alias repeat with theFile in (the name of every item in folder theSourceFolder) as list try move file theFile in folder theSourceFolder to folder theDestinationFolder on error set sourceDate to modification date of file theFile in folder theSourceFolder set targetDate to modification date of file theFile in folder theDestinationFolder if sourceDate ≥ targetDate then move file theFile in folder theSourceFolder to folder theDestinationFolder with replacing end if end try end repeat end tell
Notes:
  • I’m not sure i understand your replacement criteria as you worded it, so you may have to change the inequality to get the desired results.
  • I also wasn’t sure if you wanted to copy the files, which is what you said, or move the files. This Applescript moves files. You may want to change those move commands to copy.
  • I think there is a more direct way of setting up the path variables, without a do shell script call, but i forgot how to do it and didn’t want to spend a lot of time tracking that down. This way works. Personally i prefer working with Unix file paths, rather than the AppleScript way, but clearly you can do it more directly by using AppleScript path notation.
  • The repeat loop moves one file at a time (not very efficient, but it gets the job done)
  • The Try statement traps errors; if the file exists in the destination, then the On Error block is executed
  • In the On Error block, we examine modification dates. Could be altered to exmine creation dates instead. If the dates are right, then overwrite the exiting file. Also, you might want to perform some other action if the dates are not correct.


In bash, you can find the modification date with:

  GetFileInfo -m junk/foo


Important Note: Be all that as it may, probably the best (and fastest) way to do this, however, is with the CLI tool rsync. It is designed to do just this kind of thing, and you should be able do it all with a single rsync command. But it will take you some time to read the man page and understand the proper way to construct the command (because it has so many options, most of which are just noise for a simple task like this). rsync is designed to sync folders between different computers, but it can work just as effectively between different folders on a single computer.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 03:36 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,