Friday, 23 August 2013

Applescript - move files to folder which name begins with first 7 characters of filename

Applescript - move files to folder which name begins with first 7
characters of filename

I've got 2 folders
the first folder contains all the files which I'd like to move to a
subfolder of the second folder. I'd like to this based on the first 7
characters of both the file & folder names.
So if the first 7 characters of the subfolder in FOLDER 2 matches the
first 7 characters of the file in FOLDER 1. The file get's moved into the
subfolder of FOLDER 2
Right now I've got an Applescript which works, but it's terribly slow.
Here's the script so far:
set mgFileList to ""
set mgDestFolder to ""
set mgFilesFolder to (choose folder with prompt "Where are the files
stored which you would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination
folder?")
tell application "Finder" to set fileList to files of mgFilesFolder
repeat with aFile in fileList
set prefix to getPrefix(name of aFile)
tell application "Finder"
try
set destinationFolder to (do shell script "find " & quoted form of
(POSIX path of mgDestFolder) & "-name '" begins with prefix)
move aFile to destinationFolder
end try
end tell
end repeat
on getPrefix(aName)
set prefix to text 1 thru 7 of aName
return prefix
end getPrefix
I've only recently got into Applescripting. Can this be done more
efficiently within Applescript? I've searched around for a solution which
works with a shell script (which I think will probably be a lot faster),
but haven't been able to get any of them working.
I'm working on OSX Mountain Lion 10.8.4

No comments:

Post a Comment