Applescript "move to newest folder" action fails

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hey All,

I setup a rule and applescript to move files to the newest subfolder of a given destination and posted about the trouble I was having with the actual file move. I figured it out, so here's the solution in case anyone else wants to do this cool trick originally from Six Colors.

Add the script below as your rule's embedded AppleScript and edit the variables as needed:

  • Enter your highest-tier folder as youPath var at the top, if not in your home folder use the path starting with the Mac SDD or whatever your drive is named using : instead of /
  • Enter the Finder file kind as itemKind. This can be found by getting info on a file.
  • If you want to add an additional folder to the final destination, as I have done with the RAW folder (for raw audio) enter it as oneMoreDir, otherwise, delete RAW from the script or it will fail... or make a folder... I didn't test it.
  • File source is the folder the rule runs on.

Code: Select all
set yourPath to (path to home folder as string) & "Dropbox:files:toMove"
set itemKind to "Adobe Audition Session File"
set oneMoreDir to "RAW"

tell application "Finder"
   set folder_list to folders of alias yourPath
end tell

set theNewestDate to date "Tuesday, October 6, 1970 at 7:00:00 AM"

repeat with theFolder in folder_list
   tell application "Finder"
      set folder_contents to entire contents of theFolder
   end tell
   repeat with the_item in folder_contents
      if kind of the_item is itemKind then
         set theDate to (get modification date of theFolder)
         if theDate is greater than theNewestDate then
            set theNewestDate to theDate
         end if
      end if
   end repeat
   if theNewestDate is date "Tuesday, October 6, 1970 at 7:00:00 AM" then
      set theResult to alias yourPath
      tell application "Finder"
         set theDestinationFolder to (every item of alias yourPath whose modification date is theNewestDate)
      end tell
      set theResult to POSIX path of (item 1 of theDestinationFolder as alias) & oneMoreDir
   end if
end repeat

set theFilePath to POSIX path of theFile
do shell script (("mv -f " & quoted form of theFilePath & space & quoted form of theResult) as string)

if you want the script to copy a file rather than move it, change "mv" to "cp" in the shell script bit here at the bottom.

Thanks all, hope this helps someone out!
Posts: 26
Joined: Mon Mar 29, 2010 12:07 pm

Return to Support