I'm very new to scripting but thanks to google and forums like this, I was able to come up with a script that does exactly what I need, the only problem is that it is pretty slow and was hoping someone could help me optimize it.
I manage and organize thousands of files that have multiple versions of the same content. The script currently looks through all the selected files, and moves them all into a folder based on the last three numbers of the filename, that way all the different versions of the same content are collected in a single folder. Again, script works great, but was wondering if there was anything i could do to speed it up. Thanks in advance!
I manage and organize thousands of files that have multiple versions of the same content. The script currently looks through all the selected files, and moves them all into a folder based on the last three numbers of the filename, that way all the different versions of the same content are collected in a single folder. Again, script works great, but was wondering if there was anything i could do to speed it up. Thanks in advance!
Code:
on run {input, parameters} -- create folders from file names and move
set output to {} -- this will be a list of the moved files
repeat with anItem in the input -- step through each item in the input
set {theContainer, theName, theExtension} to (getTheNames from anItem)
try
set destination to (makeNewFolder for theName at theContainer)
tell application "Finder"
move anItem to destination
set the end of the output to the result as alias -- success
end tell
on error errorMessage -- duplicate name, permissions, etc
log errorMessage
# handle errors if desired - just skip for now
end try
end repeat
return the output -- pass on the results to following actions
end run
to getTheNames from someItem -- get a container, name, and extension from a file item
tell application "System Events" to tell disk item (someItem as text)
set theContainer to the path of the container
set {theName, theExtension} to {name, name extension}
end tell
if theExtension is not "" then
set theName to text -7 thru -((count theExtension) + 6) of theName -- just the name part
set theExtension to "." & theExtension
end if
return {theContainer, theName, theExtension}
end getTheNames
to makeNewFolder for theChild at theParent -- make a new child folder at the parent location if it doesn't already exist
set theParent to theParent as text
if theParent begins with "/" then set theParent to theParent as POSIX file as text
try
return (theParent & theChild) as alias
on error errorMessage -- no folder
log errorMessage
tell application "Finder" to make new folder at theParent with properties {name:theChild}
return the result as alias
end try
end makeNewFolder