Little experience - Need a script for Mail

(NOTE: I edited this request down - I realize the original goal was a bit expansive)

I’m new to working with Applescript. While I can tell that it’s definitely something I want to get more familiar with and use in the future, I just don’t have the time to learn it for the task at hand. I’m fine with picking apart an existing script to see how it works, but I’m starting from scratch here and completely overwhelmed.

I have emails containing contest entries submitted through an online form. Their intended target is a Microsoft Excel List.

I’ll be satisfied if, for now, I can just get the emails’ contents copied to a single BBEdit document. I’m comfortable, from my HTML editing, with managing a Find/Replace script once it’s in BBEdit.

I see some folks have had trouble running Applescripts using Mail rules, so if this were something I manually started when I got to work each morning, that would suit me.

I appreciate any help I can get - I inherited this project somewhat by accident, when my usual duties fall in the realms of the Adobe suite and Quark.

Model: G5
AppleScript: 2.0
Browser: Safari 312
Operating System: Mac OS X (10.3.9)

repost the extended info.
In the meantime work with this

tell application "Mail"
	set mails to every message of inbox whose subject is "contest submit"
	set contentsL to {}
	repeat with i from 1 to number of items in mails
		set this_item to item i of mails
		set end of contentsL to this_item
	end repeat
	return contentsL
end tell

Thank you for the kickstart!

What I receive as each entry is this:

(So the only constant is the ‘From’ of the email)
When it’s imported to a text document, I’d like it to read as:

All emails in the mailbox “Entries” would be converted to one line each in a document that I could then query from Excel to fill in the list.

Thanks for taking another look!

can you send two examples to chrisjshull at gmail dot com

I’ll do that in just a moment. They’ll appear exactly as they would were I to receive them, except that the “From” email will be different.

you must tell me if this works or not

property target_file : "Macintosh HD:contest.txt"

to switchText of t from s to r
	set d to text item delimiters
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to set t to beginning & ({""} & rest)
	set text item delimiters to d
	t
end switchText

tell application "Mail" to set mails to every message of inbox whose sender is "Becky Korol <becky@leverage-marketing.com>"
set TC to ""
repeat with i from 1 to number of items in mails
	set this_item to item i of mails
	tell application "Mail" to set contentsL to content of this_item
	if i is 1 then
		set TS to takeOut(contentsL)
	else
		set TS to TS & return & takeOut(contentsL)
	end if
end repeat
write_to_file(TS, target_file, false)


to takeOut(tt)
	set tt to switchText of tt from "Name:             " to ""
	set tt to switchText of tt from "
Phone:            " to tab
	set tt to switchText of tt from "
Email:            " to tab
	set tt to switchText of tt from "

Address:          " to tab
	set tt to switchText of tt from "
                  " to tab
	set tt to switchText of tt from "

Location:        " to tab
	set tt to switchText of tt from "
Opt-in:           " to tab
	set tt to switchText of tt from "
" to ""
	return tt
end takeOut

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

Thank you so much for taking the time to help with this!

I am told “The variable TS is not defined”

Also, Mail is already filing these emails in a new Mailbox called “DOA”. Should I change “inbox” to “DOA” in line “tell application “Mail” to set…”?

property target_file : "Macintosh HD:contest.txt"

to switchText of t from s to r
	set d to text item delimiters
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to set t to beginning & ({""} & rest)
	set text item delimiters to d
	t
end switchText

tell application "Mail" to set mails to every message of mailbox "DOA" whose sender is "Becky Korol <becky@leverage-marketing.com>"
set TC to ""
repeat with i from 1 to number of items in mails
	set this_item to item i of mails
	tell application "Mail" to set contentsL to content of this_item
	if i is 1 then
		set TS to takeOut(contentsL)
	else
		set TS to TS & return & takeOut(contentsL)
	end if
end repeat
write_to_file(TS, target_file, false)


to takeOut(tt)
	set tt to switchText of tt from "Name:             " to ""
	set tt to switchText of tt from "
Phone:            " to tab
	set tt to switchText of tt from "
Email:            " to tab
	set tt to switchText of tt from "

Address:          " to tab
	set tt to switchText of tt from "
                  " to tab
	set tt to switchText of tt from "

Location:        " to tab
	set tt to switchText of tt from "
Opt-in:           " to tab
	set tt to switchText of tt from "
" to ""
	return tt
end takeOut

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

I’m still getting a Script error that “The variable TS is not defined”

I really appreciate your help! Someday I’ll be able to troubleshoot on my own, I promise! :slight_smile:

what are you replacing the sender feild with? email me your answer so that spammers don’t get it

Even if I save the script just as you sent it, “TS” is still not defined for line:

write_to_file(TS, target_file, false)

can you email the event log?

very sorry i took so long responding.

what you need to do is change:

tell application "Mail" to set mails to every message of mailbox "DOA" whose sender is "Becky Korol <becky@leverage-marketing.com>"

to… (replace blah@blah.blah with the email address the emails are from)

tell application "Mail" to set mails to every message of mailbox "DOA" whose sender contains "blah@blah.blah"

I’ve tried that. I’m just using the example of my own email address because I can’t legally divulge any information particular to the project. :\

Could it have anything to do with the line ‘set TC to “”’? I don’t see a TC referred to anywhere else.

Just an interloper here but I may need to do some mail scripting so I just read through this script. What hit me was that TS was never defined. It is defined in the else statement as “Set TS to TS…” but TS has never been defined. However as you say there is a “Set TC to “”” statement. My guess, as an interloper not as the author, is that the author intended that to be TS. Why don’t you substitute TS for TC and see what happens.

Okay! I got it working!

Our ‘interloper’ friend, kenjanuski, and I were correct in assuming “TC” was meant to be “TS”. Also, specifying the email address in the ‘from’ field seemed to be a problem (the script ran but the ‘contest.txt’ file was still empty). Since the contents of that mailbox were all meant to be filed away with this script, I removed the “whose sender is…” line, and it worked perfectly.

Thank you SO much, themacgeek!

no problem, but thanks to kenjanuski. I was really stumped on my own script :rolleyes:

Sorry to stumble in on this thread so late, but I haven’t been following it until now. Here’s a variation that produces a similar end result, but using a different method of text manipulation to parse the data. (Before trying it, don’t forget to first amend any properties as appropriate - especially ‘sndr’.)

property mbox : "DOA" (* modify as required *)
property sndr : "someperson <someperson@somedomain.com>" (* modify as required *)
property outputString : false (* false preserves Mail's Unicode text, true converts output to string *)
property filterList : {"Name:", "Phone:", "Email:", "Address:", "Location:", "Opt-in:"}

to writeText from currText to openFile
	set stringList to {}
	if outputString then set currText to currText as string
	repeat with currFilter in filterList
		set text item delimiters to currFilter
		set stringList's end to ((currText's text item 2)'s paragraph 1)'s text from word 1 to word -1
	end repeat
	set text item delimiters to tab
	tell stringList to set currText to beginning & ({""} & rest)
	write currText & return to openFile
end writeText

set docFile to choose file name default name "Address Results " & ¬
	(current date)'s short date string & ".txt" default location path to desktop
set tid to text item delimiters
set openFile to open for access docFile with write permission

try
	set eof openFile to 0
	tell application "Mail" to repeat with msg in (messages of mailbox mbox whose sender is sndr)
		writeText of me from msg's content to openFile
	end repeat
	error number 501
on error errMsg number errNum
	close access openFile
	set text item delimiters to tid
	if errNum is not 501 then error errMsg
end try

tell application "Finder" to open docFile