FYI, this forum is aimed at helping people with their scripting problems, not writing scripts for people from scratch for free. ie, in general, you should be posting code you tried to create to accomplish the task and asking for help, not asking for scripts from scratch.
But it’s a short script so I wrote it anyway in this case.
This assumes the consistent part of the name you want to sort by will always be delimited by the first underscore in the name, as shown in your example.
tell application "Finder"
set sortFolder to choose folder
set theFiles to every file of sortFolder
repeat with aFile in theFiles
set fileName to the name of aFile
set baseName to (characters 1 through ((offset of "_" in fileName) - 1) of fileName) as text
if not ((((POSIX path of sortFolder) & baseName) as POSIX file) exists) then make new folder at sortFolder with properties {name:baseName}
move aFile to folder baseName of sortFolder
end repeat
end tell
tell application "Finder"
set sortFolder to choose folder
set theFiles to every file of sortFolder
repeat with aFile in theFiles
set fileName to the name of aFile
set baseName to text 1 through ((offset of "_" in fileName) - 1) of fileName -- Not 'characters … as text'.
if not (folder baseName of sortFolder exists) then make new folder at sortFolder with properties {name:baseName}
move aFile to folder baseName of sortFolder
end repeat
end tell
Thanks. The “text” instead of “characters” I definitely should have known better.
The “POSIX file” coercion is because I grabbed the line from another script which looks for a much longer path of subfolders, where converting it to text and appending the long path in one go seemed preferable to me compared to “folder x of folder y of folder z.”
Another sometimes advantage of the POSIX path construction is that the check to see if it exists works outside a Finder tell statement, unlike the “(folder baseName of sortFolder exists).” That’s not relevant here, since we’re inside a Finder tell anyway, but is useful when I’m checking to see if the correct path exists to save a file just before performing a save inside a “tell” for another application.
Thanks for your generous help!
Have always been interesting in scripting from scratch, mostly just cobble stuff together.
But I shall learn more so I can contribute to the site.
It won’t make any performance difference here, however, inserting a standard additional call like choose folder into a tell block is technically a no-no, as an error is returned in the events.
tell application "Finder"
set sortFolder to (choose folder)
--etc.
end tell
Standard additions should either exist outside the tell or be escaped. I generally do the latter.
tell application "Finder"
set sortFolder to my (choose folder)
end tell
While it’s generally a good idea not to use scripting addition commands in application ‘tell’ statements (in order to avoid possible terminology clashes), user-interaction commands like the ‘choose…’ and ‘display…’ ones are exceptions. The dialogs are displayed by applications — either the one running the script or one targeted in the code — so targeting these commands at specific applications (say one known to be frontmost at the time or to be involved in what follows) can be perfectly legitimate.