Removing text from a subject in Mail

I’m very new to AppleScript and still trying to find learning sources online (any direction?). I downloaded a simple script and have been modifying it to do exactly what I want. The purpose of the script is to run in Mail to turn an email into a todo. In Mail I will set a rule that will be based on text found in the subject and will trigger this script. So far so good, however, I would like the script to remove that trigger text before adding the todo to iCal. I have everything done but I can’t figure out how to tell it to remove the trigger text. Below is a portion of the script. (The lines with the asterisks are what I was trying with no luck):


	  tell application "Mail"
		set selectedMails to the selection
		repeat with eachMessage in selectedMails
			set theEmailAdd to reply to of eachMessage
			set theSenderTest to extract name from sender of eachMessage
			if theSenderTest is not equal to "" then set theSender to theSenderTest
			if theSenderTest is equal to "" then set theSender to theEmailAdd
		***	if subject contains "91550 " then set theSubject to the subject & remove "91550 " from theSubject
		***	if subject does not contain "91550 " then set theSubject to subject of eachMessage & " - " & theSender
			set message_url to "message://%3c" & (the message id of eachMessage) & "%3e"
			set theContent to content of eachMessage
			try

Model: MacBook Pro 15"
AppleScript: 2.0.1
Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

Hi,

try this


tell application "Mail"
	set selectedMails to the selection
	repeat with eachMessage in selectedMails
		set theEmailAdd to reply to of eachMessage
		set theSenderTest to extract name from sender of eachMessage
		if theSenderTest is not equal to "" then set theSender to theSenderTest
		if theSenderTest is equal to "" then set theSender to theEmailAdd
		if subject of eachMessage contains "91550 " then
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "91550 "
			set theSubject to text items of subject of eachMessage
			set AppleScript's text item delimiters to ""
			set theSubject to theSubject as text
			set AppleScript's text item delimiters to ASTID
		else
			set theSubject to subject of eachMessage & " - " & theSender
		end if
		set message_url to "message://%3c" & (the message id of eachMessage) & "%3e"
		set theContent to content of eachMessage
	end repeat
end tell

I get this error when I run it:

“Mail got an error: Can’t make text item of subject of message id 264451 of mailbox “INBOX” of account “kerio IMAP” into type reference.”
It works fine if I don’t have "Trigger " in the subject of the mail message, so the problem seems to be in the “then” of the if - then - else statement.
I tested it on an email with a subject of “Trigger test 01”
Here is the entire code if that helps:


tell application "GrowlHelperApp"
	set the allNotificationsList to ¬
		{"New Task Notification", "Error adding task"}
	set the enabledNotificationsList to ¬
		{"New Task Notification", "Error adding task"}
	register as application ¬
		"Make todo from mail" all notifications allNotificationsList ¬
		default notifications enabledNotificationsList ¬
		icon of application "iCal"
end tell
using terms from application "Mail"
	tell application "Mail"
		set selectedMails to the selection
		repeat with eachMessage in selectedMails
			set mailRuleCreated to false
			set theEmailAdd to reply to of eachMessage
			set theSenderTest to extract name from sender of eachMessage
			if theSenderTest is not equal to "" then
				set theSender to theSenderTest
			else
				set theSender to theEmailAdd
			end if
			if subject of eachMessage contains "Trigger " then
				set mailRuleCreated to true
				set ASTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to "Trigger "
				set theSubject to text item of subject of eachMessage
				set AppleScript's text item delimiters to ""
				set theSubject to theSubject as text
				set AppleScript's text item delimiters to ASTID
			else
				set theSubject to subject of eachMessage & " from " & theSender
			end if
			set message_url to "message://%3c" & (the message id of eachMessage) & "%3e"
			
			set theContent to content of eachMessage
			try
				tell application "iCal"
					set newtodo to (make new todo at end of todos of calendar "Beta") -- change the calendar name if necessary
					tell newtodo
						set summary to theSubject
						set due date to ((current date) + (72 as integer) * hours)
						set url to message_url
						if mailRuleCreated is true then
							set description to "Trigger wrote:
						" & theContent
						else
							set description to theEmailAdd & " wrote:
							" & theContent
						end if
					end tell
				end tell
				tell application "GrowlHelperApp"
					notify with name ¬
						"New Task Notification" title ¬
						"New task added" description theSubject ¬
						application name "Make todo from mail"
				end tell
			on error errmessg
				tell application "GrowlHelperApp"
					notify with name ¬
						"Error adding task" title ¬
						"Failed to add new task" description errmessg ¬
						application name "Make todo from mail"
				end tell
			end try
		end repeat
	end tell
end using terms from

Also, I know that this is probably a pretty basic question but how do I cleanly enter a inside of “” (quotations)? If you look at the mailRuleCreated if - then - else statement towards the bottom, you see that I just entered a hard return within the quotations but that doesn’t seem right. There must be a correct way to achieve this, right?

 set theSubject to text items of subject of eachMessage

it’s text items, not text item

You can’t enter a return within quotes except \r, but you can do it this way

set description to "Trigger wrote:" & return & theContent

Still getting the same error:

“Mail got an error: Can’t make every text item of subject of message id 264451 of mailbox “INBOX” of account “kerio IMAP” into type reference.”

I originally had the line typed correctly since I had cut and paste your text into the script but I was trying things when it didn’t work. I had thought that maybe that had been a typo in your script. I’ve made the adjustment as you can see below and I try running it on a email with a subject of “Trigger Test 01” but still no luck. Any other suggestions?


tell application "GrowlHelperApp"
	set the allNotificationsList to ¬
		{"New Task Notification", "Error adding task"}
	set the enabledNotificationsList to ¬
		{"New Task Notification", "Error adding task"}
	register as application ¬
		"Make todo from mail" all notifications allNotificationsList ¬
		default notifications enabledNotificationsList ¬
		icon of application "iCal"
end tell
using terms from application "Mail"
	tell application "Mail"
		set selectedMails to the selection
		repeat with eachMessage in selectedMails
			set mailRuleCreated to false
			set theEmailAdd to reply to of eachMessage
			set theSenderTest to extract name from sender of eachMessage
			if theSenderTest is not equal to "" then
				set theSender to theSenderTest
			else
				set theSender to theEmailAdd
			end if
			if subject of eachMessage contains "Trigger " then
				set mailRuleCreated to true
				set ASTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to "Trigger "
				set theSubject to text items of subject of eachMessage
				set AppleScript's text item delimiters to ""
				set theSubject to theSubject as text
				set AppleScript's text item delimiters to ASTID
			else
				set theSubject to subject of eachMessage & " from " & theSender
			end if
			set message_url to "message://%3c" & (the message id of eachMessage) & "%3e"
			
			set theContent to content of eachMessage
			try
				tell application "iCal"
					set newtodo to (make new todo at end of todos of calendar "Beta") -- change the calendar name if necessary
					tell newtodo
						set summary to theSubject
						set due date to ((current date) + (72 as integer) * hours)
						set url to message_url
						if mailRuleCreated is true then
							set description to "Trigger wrote:
						" & theContent
						else
							set description to theEmailAdd & " wrote:" & return & theContent
						end if
					end tell
				end tell
				tell application "GrowlHelperApp"
					notify with name ¬
						"New Task Notification" title ¬
						"New task added" description theSubject ¬
						application name "Make todo from mail"
				end tell
			on error errmessg
				tell application "GrowlHelperApp"
					notify with name ¬
						"Error adding task" title ¬
						"Failed to add new task" description errmessg ¬
						application name "Make todo from mail"
				end tell
			end try
		end repeat
	end tell
end using terms from

Model: MacBook Pro 15"
AppleScript: 2.0.1
Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

then try this


.
if theSenderTest is not equal to "" then
			set theSender to theSenderTest
		else
			set theSender to theEmailAdd
		end if
		set theSubject to subject of eachMessage
		if theSubject contains "Trigger " then
			set mailRuleCreated to true
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "Trigger "
			set theSubject to text items of theSubject
			set AppleScript's text item delimiters to ""
			set theSubject to theSubject as text
			set AppleScript's text item delimiters to ASTID
		else
			set theSubject to theSubject & " from " & theSender
		end if
.

It works!!! Thank you so much! Do you have a website where you accept donations? I don’t mind trying to send a few dollars for your help!

tell application "GrowlHelperApp"
	set the allNotificationsList to ¬
		{"New Task Notification", "Error adding task"}
	set the enabledNotificationsList to ¬
		{"New Task Notification", "Error adding task"}
	register as application ¬
		"Make todo from mail" all notifications allNotificationsList ¬
		default notifications enabledNotificationsList ¬
		icon of application "iCal"
end tell
using terms from application "Mail"
	tell application "Mail"
		set selectedMails to the selection
		repeat with eachMessage in selectedMails
			set mailRuleCreated to false
			set theEmailAdd to reply to of eachMessage
			set theSenderTest to extract name from sender of eachMessage
			if theSenderTest is not equal to "" then
				set theSender to theSenderTest
			else
				set theSender to theEmailAdd
			end if
			set theSubject to subject of eachMessage
			if subject of eachMessage contains "Trigger " then
				set mailRuleCreated to true
				set ASTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to "Trigger "
				set theSubject to text items of theSubject
				set AppleScript's text item delimiters to ""
				set theSubject to theSubject as text
				set AppleScript's text item delimiters to ASTID
			else
				set theSubject to subject of eachMessage & " from " & theSender
			end if
			set message_url to "message://%3c" & (the message id of eachMessage) & "%3e"
			
			set theContent to content of eachMessage
			try
				tell application "iCal"
					set newtodo to (make new todo at end of todos of calendar "Beta") -- change the calendar name if necessary
					tell newtodo
						set summary to theSubject
						set due date to ((current date) + (72 as integer) * hours)
						set url to message_url
						if mailRuleCreated is true then
							set description to "Trigger wrote:
						" & theContent
						else
							set description to theEmailAdd & " wrote:" & return & theContent
						end if
					end tell
				end tell
				tell application "GrowlHelperApp"
					notify with name ¬
						"New Task Notification" title ¬
						"New task added" description theSubject ¬
						application name "Make todo from mail"
				end tell
			on error errmessg
				tell application "GrowlHelperApp"
					notify with name ¬
						"Error adding task" title ¬
						"Failed to add new task" description errmessg ¬
						application name "Make todo from mail"
				end tell
			end try
		end repeat
	end tell
end using terms from

You’re welcome :slight_smile:

The effort was quasi zero

Premature celebration. Upon further testing, I realized that the script was grabbing both the income message and whatever message was currently selected. Thus creating two todos. I broke out the selected message into a script of own to be triggered by Mail-Act On or QuicKeys but now I’m getting a “Can’t get reply to of message.” error. Apparently, I am not sending the script any message now. How do I tell Mail to send any other messages, such as an incoming message or rule-triggering message? I looked in the Open Dictionary and could not find any references for an incoming message.

tell application "GrowlHelperApp"
	set the allNotificationsList to ¬
		{"New Task Notification", "Error adding task"}
	set the enabledNotificationsList to ¬
		{"New Task Notification", "Error adding task"}
	register as application ¬
		"Make todo from mail" all notifications allNotificationsList ¬
		default notifications enabledNotificationsList ¬
		icon of application "iCal"
end tell
using terms from application "Mail"
	tell application "Mail"
		set theEmailAdd to reply to of message
		set theSenderTest to extract name from sender of message
		if theSenderTest is not equal to "" then
			set theSender to theSenderTest
		else
			set theSender to theEmailAdd
			set theSubject to subject of message
			if subject of message contains "Trigger0 " then -- to set a todo with no due date
				set myDueDate to 0
				set ASTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to "Trigger0 "
				set theSubject to text items of theSubject
				set AppleScript's text item delimiters to ""
				set theSubject to theSubject as text
				set AppleScript's text item delimiters to ASTID
			end if
			if subject of message contains "Trigger1 " then -- to set a todo due in 1 day
				set myDueDate to 24
				set ASTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to "Trigger1 "
				set theSubject to text items of theSubject
				set AppleScript's text item delimiters to ""
				set theSubject to theSubject as text
				set AppleScript's text item delimiters to ASTID
			end if
			if subject of message contains "Trigger2 " then -- to set a todo due in 2 days
				set myDueDate to 48
				set ASTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to "Trigger2 "
				set theSubject to text items of theSubject
				set AppleScript's text item delimiters to ""
				set theSubject to theSubject as text
				set AppleScript's text item delimiters to ASTID
			end if
			if subject of message contains "Trigger3 " then -- to set a todo due in 3 days
				set myDueDate to 72
				set ASTID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to "Trigger3 "
				set theSubject to text items of theSubject
				set AppleScript's text item delimiters to ""
				set theSubject to theSubject as text
				set AppleScript's text item delimiters to ASTID
			end if
			set message_url to "message://%3c" & (the message id of eachMessage) & "%3e"
			
			set theContent to content of message
			try
				tell application "iCal"
					set newtodo to (make new todo at end of todos of calendar "Beta") -- change the calendar name if necessary
					tell newtodo
						set summary to theSubject
						if myDueDate is not equal to 0 then
							set due date to ((current date) + (myDueDate as integer) * hours) -- to input the correct due date based on the trigger
						end if
						set url to message_url
						set description to theEmailAdd & " wrote:" & return & theContent
					end tell
				end tell
				tell application "GrowlHelperApp"
					notify with name ¬
						"New Task Notification" title ¬
						"New task added" description theSubject ¬
						application name "Make todo from mail"
				end tell
			on error errmessg
				tell application "GrowlHelperApp"
					notify with name ¬
						"Error adding task" title ¬
						"Failed to add new task" description errmessg ¬
						application name "Make todo from mail"
				end tell
			end try
		end if
	end tell
end using terms from

message is no valid reference.
You don’t specify the message you want to process.
To run the script from a rule an event handler must be added.

I haven’t tested this


using terms from application "Mail"
	on perform mail action with messages theMessages
		my registerGrowl()
		repeat with oneMessage in theMessages
			tell application "Mail"
				set theEmailAdd to reply to of oneMessage
				set theSenderTest to extract name from sender of oneMessage
			end tell
			if theSenderTest is not equal to "" then
				set theSender to theSenderTest
			else
				set theSender to theEmailAdd
				tell application "Mail" to set theSubject to subject of oneMessage
				if theSubject contains "Trigger0 " then -- to set a todo with no due date
					set myDueDate to 0
					set theSubject to my extractTrigger(theSubject, "Trigger0 ")
				else if theSubject contains "Trigger1 " then -- to set a todo due in 1 day
					set myDueDate to 24
					set theSubject to my extractTrigger(theSubject, "Trigger1 ")
				else if theSubject contains "Trigger2 " then -- to set a todo due in 2 days
					set myDueDate to 48
					set theSubject to my extractTrigger(theSubject, "Trigger2 ")
				else if theSubject contains "Trigger3 " then -- to set a todo due in 3 days
					set myDueDate to 72
					set theSubject to my extractTrigger(theSubject, "Trigger3 ")
				end if
				tell application "Mail"
					set message_url to "message://%3c" & (the message id of oneMessage) & "%3e"
					set theContent to content of oneMessage
				end tell
				try
					tell application "iCal"
						set newtodo to (make new todo at end of todos of calendar "Beta") -- change the calendar name if necessary
						tell newtodo
							set summary to theSubject
							if myDueDate is not equal to 0 then
								set due date to ((current date) + (myDueDate as integer) * hours) -- to input the correct due date based on the trigger
							end if
							set url to message_url
							set description to theEmailAdd & " wrote:" & return & theContent
						end tell
					end tell
					tell application "GrowlHelperApp"
						notify with name ¬
							"New Task Notification" title ¬
							"New task added" description theSubject ¬
							application name "Make todo from mail"
					end tell
				on error errmessg
					tell application "GrowlHelperApp"
						notify with name ¬
							"Error adding task" title ¬
							"Failed to add new task" description errmessg ¬
							application name "Make todo from mail"
					end tell
				end try
			end if
		end repeat
	end perform mail action with messages
end using terms from

on extractTrigger(theText, theTrigger)
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theTrigger
	set theText to text items of theText
	set AppleScript's text item delimiters to ""
	set theText to theText as text
	set AppleScript's text item delimiters to ASTID
	return theText
end extractTrigger

on registerGrowl()
	tell application "GrowlHelperApp"
		set the allNotificationsList to ¬
			{"New Task Notification", "Error adding task"}
		set the enabledNotificationsList to ¬
			{"New Task Notification", "Error adding task"}
		register as application ¬
			"Make todo from mail" all notifications allNotificationsList ¬
			default notifications enabledNotificationsList ¬
			icon of application "iCal"
	end tell
end registerGrowl