Selecting the first two words of a Mail subject in naming a folder

This script provides a drop down menu to choose an existing “parent” folder where a new sub-folder is created. The new sub-folder is named after the subject of the first message and then files the selected message/s in the new sub-folder. I have it working, but sometimes additional text follows the first two words in the subject (which is the only text I want the sub-folder called) and either causes an exsessively long sub-folder name or it can even cause an error if the subject is to long resulting in multiple sub-folders of the new sub-folder.

So using “text” or even setting the new sub-folder as the subject of the message will work, but I’m trying to use just the first 2 to 3 words of the subject. Ive tried the below with no luck. Thoughts on how to identify the words (not text) of the subject? Thanks!

set theSubject to words of theSubject
set AppleScript's text item delimiters to ASTID

set theSubject to word 1 thru 3 of theSubject

set folderName to theSubject as Unicode text

Here’s the working script thus far:


tell application "Mail"
	activate
	set theSelectedMessages to selection
	set theselectedmessage to item 1 of theSelectedMessages
	set theSubject to the subject of item 1 of theselectedmessage
	set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "Fwd:"}
	set AppleScript's text item delimiters to ""
	set theSubject to text of theSubject
	set AppleScript's text item delimiters to ASTID
	
	set theSubject to text 1 thru 20 of theSubject
	
	set folderName to theSubject as Unicode text
	(*Drop down list of folders to choose where sub-folder will be created*)
	
	set myList to {"2012/Group 1", "2012/Group 2", "2012/Group 3", "2012/Group 4", "2012/Group 5", "2012/Group 6", "2012/Group 7", "2012/Group 8"}
	
	
	
	choose from list myList with title "Move Message(s)" with prompt "Choose which group" default items {"aaWork/zApplications/2012"}
	set selectedMailBox to result as string
	
	
	(*Making the new subfolder and moving the message/s into it*)
	
	
	if selectedMailBox is "2012/Group 1" then tell application "Mail"
		
		set Customfolder to "aaWork/zApplications/2012/Group 1"
		if not (mailbox folderName exists) then
			make new mailbox with properties {name:Customfolder & "/" & folderName}
			
			repeat with eachMessage in theSelectedMessages
				move eachMessage to mailbox (Customfolder & "/" & folderName)
			end repeat
			
		end if
	end tell

(*...for all 8 groups*)

tell application "Mail"
		activate
	end tell
end tell

Hi,

try this


property theYear : 2012
property numberOfGroups : 8

tell application "Mail"
	-- activate
	set theSelectedMessages to selection
	set theselectedmessage to item 1 of theSelectedMessages
	set theSubject to the subject of theselectedmessage
	
end tell
-- omit Fwd: or Re: Prefix
if theSubject starts with "Fwd: " then
	set theSubject to text 6 thru -1 of theSubject
else if theSubject starts with "Re: " then
	set theSubject to text 5 thru -1 of theSubject
end if
set countWords to count words of theSubject
if countWords > 3 then
	set folderName to text 1 thru word 3 of theSubject
else
	set folderName to text 1 thru word countWords of theSubject
end if

(*Drop down list of folders to choose where sub-folder will be created*)
set groupList to {}
repeat with i from 1 to numberOfGroups
	set end of groupList to ((theYear as text) & "/Group " & i)
end repeat

set chosenGroup to choose from list groupList with title "Move Message(s)" with prompt "Choose which group"
if chosenGroup is false then return
set selectedMailBox to item 1 of chosenGroup

(*Making the new subfolder and moving the message/s into it*)

set Customfolder to "aaWork/zApplications/" & selectedMailBox
tell application "Mail"
	set newMailBox to Customfolder & "/" & folderName
	if not (mailbox newMailBox exists) then
		make new mailbox with properties {name:newMailBox}
		
		repeat with eachMessage in theSelectedMessages
			move eachMessage to mailbox newMailBox
		end repeat
		
	end if
end tell

Worked great, thank you!

This script has been running great. I have encountered one issue I’m wondering if anyone knows how to resolve.

When the new sub-mailbox is created it shows up at the bottom of all the other sub-mailboxes in it’s parent mailbox. Ideally it would be placed in alphabetical order with the other existing sub-mailboxes. Any idea how to do that?