2 Replies Latest reply: Mar 8, 2013 8:35 AM by MattJayC
MattJayC Level 1 (0 points)

I have this script, and it sorts the files to move into the other folders of the desktop, I now need to add a wildcard that if it doesn't fit the files with the prefixes then move them to folder that begins with "Mik


Is this something that can be easy to do?


Many Thanks






# the file matrix is a record of {destination folder name, {list of file prefixes to move}}

set fileMatrix to

          {folderName:"2_do_BHS", prefixes:{"BH70", "SM70", "AL70", "BH11"}}, ¬

          {folderName:"2_do_Bu", prefixes:{"BU40", "BU11"}}, ¬

          {folderName:"2_do_Ca", prefixes:{"CW"}}, ¬

          {folderName:"2_do_Da", prefixes:{"ES20", "AM20"}}, ¬

          {folderName:"2_do_Di", prefixes:{"DV25", "DV11"}}, ¬

          {folderName:"2_do_Do", prefixes:{"DJ30", "RA30", "DJ11"}}, ¬

          {folderName:"2_do_In", prefixes:{"GT55", "CC55"}}, ¬

          {folderName:"2_do_Fr", prefixes:{"FR10", "FR11"}}, ¬

          {folderName:"2_do_No", prefixes:{"TM17", "NN_"}}, ¬

          {folderName:"2_do_Ma", prefixes:{"MA65", "MF65", "FI65", "MC65", "MH65", "MB65"}}, ¬

          {folderName:"2_do_Pr", prefixes:{"PR_"}}, ¬

          {folderName:"2_do_To", prefixes:{"TM15", "TM11"}}, ¬

          {folderName:"2_do_Wa", prefixes:{"WA35", "WA11"}}, ¬

          {folderName:"2_do_Se", prefixes:{"SE_"}}}


tell application "Finder"

          set theHotFolder to folder "StudioA:Users:StudioA:Pictures:HotFolder2Do"


          repeat with matrixItem in fileMatrix -- look for folder

                    set destinationFolder to (folders of desktop whose name starts with folderName of matrixItem)

                    if destinationFolder is not {} then -- found one

                              set destinationFolder to first item of destinationFolder -- only one destination


                              repeat with aPrefix in prefixes of matrixItem -- look for files

                                        set theFiles to (files of theHotFolder whose name starts with aPrefix) as alias list


                                        if theFiles is not {} then repeat with aFile in theFiles -- move files


  move aFile to destinationFolder

                                                  on error errmess number errnum -- oops (should probably check for a specific error number)

                                                            log "Error " & errnum & " moving file: " & errmess

                                                            set newName to my getUniqueName(aFile, destinationFolder)

                                                            set name of aFile to "this is a unique name" -- or whatever

  move aFile to destinationFolder

                                                            set name of aFile to newName

                                                  end try

                                        end repeat

                              end repeat

                    end if

          end repeat

end tell



to getUniqueName(someFile, someFolder)


     check if someFile exists in someFolder, creating a new unique file name (if needed) by adding a suffix

          parameters -          someFile [mixed]: a source file path

                              someFolder [mixed]: a folder to check

          returns [list]:          a unique file name and extension


          set {counter, suffixes, divider} to {0, "abcdefghijklmnopqrstuvwxyz", "_"}

          set someFile to someFile as text -- System Events will use both Finder and POSIX text


          tell application "System Events" to tell disk item someFile to set {theName, theExtension} to {name, name extension}

          if theExtension is not "" then set theExtension to "." & theExtension

          set theName to text 1 thru -((length of theExtension) + 1) of theName -- just the name part


          set newName to theName & theExtension

          tell application "System Events" to tell (get name of files of folder (someFolder as text))

                    repeat while it contains newName

                              set counter to counter + 1 -- hopefully there aren't more than 26 duplicates (numbers are easier)

                              set newName to theName & divider & (item counter of suffixes) & theExtension

                    end repeat

          end tell


          return newName

end getUniqueName

iMac, Mac OS X (10.6.8)