Creating multiple folders with the first 3 words of a file name

Hello there, I’m a newbie here and normally search tirelessly on the web to find things. I noticed here that many will take the time to write a basic script to help others out and that was awesome to see! Well as you can guess, I’m asking for help…
I have ultimately thousands of files (in the end) that I have to take the first three words of the pdf file name and make a folder and put that file or files in the folder. Some times there are 2-6 files that will have the same first 3 words and will go into the same folder…
example: (made up names)
2026 John Doe intake.pdf - Folder would be 2026 John Doe
2026 John Doe application.pdf - File would go into above folder
2026 Mary Smith intake.pdf - Folder would be 2026 Mary Smith
2026 Jack Smith application.pdf. - Folder would be 2026 Jack Smith
The pdf files will all be in the same folder to begin with, but again, there may be several that will end up with the same first 3 words and should go into the same folder. I have found script on a site that names the folder after the file name, I just need that tweaked to only use the first 3 word essentially. or to gather the data before the third space…? Oh, just another bit of info, some names may have a hyphen in them ie 2026 Brenda Smith-Doe intake.pdf and if it make a difference, the file names may be a lot longer than my example like 2026 John Doe application for bluecross mapd direct abhhnthdksj.pdf if that makes any difference.

I need to automate this process so badly, I will make sure you guys are recognized also. My head can’t take more searching for the fix and I wish I knew how to write scripting…! ;/
Here is the script I mentioned that names the folder after the full file name.

tell application “Finder”

set selected to selection

set current_folder to item 1 of selected

set mlist to every file of current_folder

repeat with this_file in mlist

set cur_ext to name extension of this_file

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

set new_folder to make new folder with properties {name:new_name} at current_folder

move this_file to new_folder

end repeat

end tell

If anyone can help me out… Would help me and this small company immensely!!!

And thank you for at least taking the time to read my first post!!
Brian

Try this…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

local tid, current_folder, file_paths, file_names, this_file, new_name
set current_folder to choose folder
set current_folder to current_folder as text
set tid to text item delimiters
set text item delimiters to " "
tell application "System Events"
	set {file_paths, file_names} to {path, name} of files of folder current_folder
	repeat with i from 1 to count file_names
		set this_file to item i of file_paths
		set cur_ext to name extension of alias this_file
		set new_name to text 1 thru -((length of cur_ext) + 2) of (item i of file_names)
		set new_name to text items of new_name
		if item -1 of new_name is in {"intake", "application"} then
			set new_name to (items 1 thru -2 of new_name) as text
			if not (exists folder (current_folder & new_name)) then
				make new folder at folder current_folder with properties {name:new_name}
			end if
			move file this_file to (current_folder & new_name)
		end if
	end repeat
end tell
set text item delimiters to tid

Are there any sub-filenames that contain a word other than “intake” or “application”