Mail folder contents

I just came back to Mail (TIGER) from Mailsmith so I am still discovering Mail.

I have a database (Panorama) that I built for my contact manager and my project manager. I Want to have each record show my email correspondence in Mail for that records person.

I want to point the script at a contacts specific folder only, don’t really need to search the whole account - assuming I am at least that organized.

I am envisioning bringing back enough data to the DB to scan the thread in the contact DB, then clicking on a message which opens it again in Mail for reply or further reading.

How do I tell AS to go to a specific folder, and then to grab {messageID, Subject, Date Received, From, To, Subject and maybe 256 chars of the body}? If that isn’t clear enough, I imagine the results in my DB would look sort of like this;

“Steve Jobs” “RE: Your new product” “Nov 1, 2007” “Holy cow your new product is awesome.”
“My Business” “RE: Your new product” “Nov 1, 2007” “I would love to tell you all about it.”
“Steve Jobs” “RE: Your new product” “Nov 3, 2007” “How is 2010 look on your calendar?”
“My Business” “RE: Your new product” “Nov 3, 2007” “2010?”

I am pretty sure I can figure out what to do once I get it into the DB, especially if I include the message ID. I can probably also figure out how to tell Mail to collect data from one message. What I can’t understand is how to do that for the contents of a folder, and to set the data to every message in that folder.

Thanks if you can help.

Try something like this:

tell application "Mail"
	tell mailbox "Mailbox Name" of account "Account Name"
		set {idList, senderList, subjectList, receivedList, contentList} to ¬
			{id, sender, subject, date received, content} of messages
	end tell
end tell

repeat with i from 1 to (count idList)
	set thisID to item i of idList
	set thisSender to item i of senderList
	set thisSubject to item i of subjectList
	set thisDate to item i of receivedList
	set thisContent to item i of contentList
	try
		set thisContent to text 1 thru 256 of thisContent
	end try
	
	-- Put this message into your database (or whatever)
end repeat

Thanks very much. Looks like I am well on the way with all that.

Curious if I need to specify an acount as this is for the end result folders after they are filtered in from different accounts. Just nudge me if I missed an important point but I dont think I need to do that.

Again, thanks.

Ah, that’s a good point. If it’s a folder on your Mac, then you don’t need an account.

Thanks. I am having a hiccup on this still. :frowning:

Running the scripts you have offered it in the script editor gives me an error. It messages the error:
“NSReceiverEvaluationScriptError: 4”

And the script stops, highlighting the following area:
{id, sender, subject, date received, content}

I am having another error I can’t resolve yet although I don’t think it is for you to solve… In my database app, which can run Applescripts from inside it’s procedures, I am not making it work yet. I cant quite tell if that is about AppleScript or Panorama and I have a message in to the Pan talk list to ask for help.

It should give a result on your example on the script editor though, shouldn’t it? The very first time I ran it, I did see a result. But now even after quit and restart of the editor, I get the error I posed above.

Curious. Can you tell what I am doing wrong?

Thanks.

Are you trying to use a Smart Mailbox for this?

No. I ran applejack to make sure everything was OK, seems to be. Pointing to a test folder caled “Family” right now. I was trying for a freinds mailbox who was under the folder “Freinds”. I was a litle uncertain about stating the hierarchy of a subfolder till I got it worlking so I use a tp level folder to test.

OK, something seems to be in the results window now. Just taking a very long time. I thought it was freezing.

I realize that as my database (Panorama) can read the applescriptresults, all I realy have to do is make this script parse it’s own varible durring the result. I really dont think it is going to work to have the script pass to pan each time it loops.

The following two lines, which are not producing results I can use, are my latest attempt to solve this. How do I use the repeat loop to go through the targeted mail folder, and build a list (an array) of all the elements specified int the first part of the script?


set pregrandevariable to thisID & tab & thisContent & tab & thisSender & tab & thisSubject & tab & thisDate & tab & thisContent
set grandevariable to pregrandevariable & return

The result should show all the data; for all the messages.
{id, sender, subject, date received, content} of messages
Preferably tab delimited for each record and return separated for the array.

tell application “Mail”
tell mailbox “Friends/Belinda”
set {idList, senderList, subjectList, receivedList, contentList} to ¬
{id, sender, subject, date received, content} of messages
end tell
end tell

repeat with i from 1 to (count idList)
set thisID to item i of idList
set thisSender to item i of senderList
set thisSubject to item i of subjectList
set thisDate to item i of receivedList
set thisContent to item i of contentList
try
set thisContent to text 1 thru 256 of thisContent
end try

set pregrandevariable to thisID & tab & thisContent & tab & thisSender & tab & thisSubject & tab & thisDate & tab & thisContent
set grandevariable to pregrandevariable & return

end repeat

Can you say how this is done please? In Panorama, my only real programming experience, I would set a variable equal to it’s self plus the updated value and build an array that way. Testing in the above example, I can not compile without an error if I set;

set grandevariable to grandevariable &  pregrandevariable & return

Thanks

the problem is, you have multiple classes (number, text, date) and AppleScript can’t concatenate a number (the first element) with text or date and will create a list with the values instead

try something like this

tell application "Mail"
	set pregrandevariable to {}
	repeat with oneMessage in messages of mailbox "Friends/Belinda"
		tell contents of oneMessage
			set end of pregrandevariable to (id as text) & tab & text 1 thru 256 of content & tab & sender & tab & subject & tab & date as text
		end tell
	end repeat
end tell
set end of pregrandevariable to return

Edit: if the text content contains less then 256 characters, an error occurs.
Here a version to avoid this error


tell application "Mail"
	set pregrandevariable to {}
	repeat with oneMessage in messages of mailbox "Friends/Belinda"
		tell contents of oneMessage
			set c to content
			if (count c) > 256 then set c to text 1 thru 256 of c
			set end of pregrandevariable to (id as text) & tab & c & tab & sender & tab & subject & tab & date as text
		end tell
	end repeat
end tell
set end of pregrandevariable to return

I should say that I am following directions here, trying to understand more about AS, but it would help if you could say you meant your example to completely replace my example, or be added to it and if so, specifically where should I have added it?

The error is from running your example in a new editor as is.

Thanks

see my edited version above. It replaces your example completely

OK, extrapolation from your suggestion, adding to Bruces kind suggested script, I think this works. I have to carefull look through to see what it is actually doing, but it is already better than it was. - THanks.

set grandevariable to {}
repeat with i from 1 to (count idList)
set thisID to item (i as text) of idList
set thisSender to item (i as text) of senderList
set thisSubject to item (i as text) of subjectList
set thisDate to item (i as text) of receivedList
set thisContent to item (i as text) of contentList
try
set thisContent to text 1 thru 256 of thisContent
end try
set pregrandevariable to thisID & tab & thisContent & tab & thisSender & tab & thisSubject & tab & thisDate & tab & thisContent
set grandevariable to grandevariable & return & pregrandevariable

end repeat