Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Extracting all email addresses contained in emails.

Canyou help me, I extracted "from" addresses with the following script:


--script begins

property theBoxes : {"mailbox1", "mailbox2", "mailbox3", "mailbox4", "mailbox5", "mailbox6", "mailbox7"}


on run


set theMessages to {}


tell application "Microsoft Excel"

set theSpamFile to make new workbook

set theSheet to active sheet of theSpamFile

set formula of range "B1" of theSheet to "Subject"

set formula of range "A1" of theSheet to "From"

end tell


tell application "Mail"

set theRow to 2

get mailboxes


--set theMessages to messages of junk mailbox

repeat with aBox in theBoxes

set theMessages to theMessages & messages of mailbox aBox

end repeat


repeat with aMessage in theMessages

my SetFrom(sender of aMessage, theRow, theSheet)

my SetSubject(subject of aMessage, theRow, theSheet)

set theRow to theRow + 1

end repeat

end tell


end run


on SetFrom(theSender, theRow, theSheet)

tell application "Microsoft Excel"

set theRange to "A" & theRow

set formula of (range theRange) of theSheet to theSender

end tell

endSetFrom


on SetSubject(theSubject, theRow, theSheet)

tell application "Microsoft Excel"

set theRange to "B" & theRow

set formula of range theRange of theSheet to theSubject

end tell

endSetSubject

--script ends


Which worked very well.


I now want to extract all of the to, cc and BCC emails and changed the above script to:


--script begins

property theBoxes : {"RWG Archive", "NRW Archive", "Contacts#Newspaper List", "JOBS R", "JOBS N"}


on run


set theMessages to {}


tell application "Microsoft Excel"

set theSpamFile to make new workbook

set theSheet to active sheet of theSpamFile

set formula of range "B1" of theSheet to "Subject"

set formula of range "A1" of theSheet to "Recipients"

end tell


tell application "Mail"

set theRow to 2

get mailboxes

--set theMessages to messages of junk mailbox

repeat with aBox in theBoxes

set theMessages to theMessages & messages of mailbox aBox

end repeat


repeat with aMessage in theMessages

my SetRecipients(recipients of aMessage, theRow, theSheet)

my SetSubject(subject of aMessage, theRow, theSheet)

set theRow to theRow + 1

end repeat

end tell

end run


on SetRecipients(theRecipients, theRow, theSheet)

tell application "Microsoft Excel"

set theRange to "A" & theRow

set formula of (range theRange) of theSheet to theRecipients

end tell

endSetRecipients


on SetSubject(theSubject, theRow, theSheet)

tell application "Microsoft Excel"

set theRange to "B" & theRow

set formula of range theRange of theSheet to theSubject

end tell

endSetSubject

--script ends


It does not work, I get the following error:


error "Can’t make «class trcp» 1 of «class mssg» id 135522 of «class mbxp» \"RWG Archive\" of application \"Mail\" into the expected type." number -1700 from «class trcp» 1 of «class mssg» id 135522 of «class mbxp» "RWG Archive"


I am not good at this and would appreciate help to get TO, CC and BCC address.


Kind Regard



o




o

Mac OS X (10.7.4), Help!!!!

Posted on May 22, 2012 2:12 AM

Reply
Question marked as Best reply

Posted on May 23, 2012 6:13 PM

Hi,


recipients is a element, to get what you want we must obtain values ​​from the property address of this element

The result will be a list, this list must be converted to string, otherwise the value of the cell will be the first item in the list of addresses


--------------------------------

--extract all of the to, cc and BCC emails


settheBoxesto {"Videotron", "Yahoo"} --{"RWG Archive", "NRW Archive", "Contacts#Newspaper List", "JOBS R", "JOBS N"}

settheRowto 2

tell application "Microsoft Excel"

settheSpamFiletomakenewworkbook

settheSheettoactive sheetoftheSpamFile

set formula of range "B1" of theSheet to "Subject"

set formula of range "A1" of theSheet to "Recipients"

endtell

tellapplication "Mail"

repeatwithaBoxintheBoxes

repeat with aMessage in (get messages of mailbox aBox)

tell aMessage

mysetRecipientsAndSubject(addressofitsrecipients, theRow, theSheet, subject)

end tell

set theRow to theRow + 1

end repeat

endrepeat

endtell


onsetRecipientsAndSubject(theRecipients, theRow, theSheet, theSubject)

setTIDtotext item delimiters

settext item delimitersto ", " -- separator (comma and space)

settheRecipientstotheRecipientsastext -- convert AppleScript's list to string --> (address separated by comma)

settext item delimiterstoTID


tell application "Microsoft Excel"

setvalueofrange ("A" & theRow & ":B" & theRow) oftheSheetto {theRecipients, theSubject}

end tell

endsetRecipientsAndSubject

10 replies
Question marked as Best reply

May 23, 2012 6:13 PM in response to jlantern

Hi,


recipients is a element, to get what you want we must obtain values ​​from the property address of this element

The result will be a list, this list must be converted to string, otherwise the value of the cell will be the first item in the list of addresses


--------------------------------

--extract all of the to, cc and BCC emails


settheBoxesto {"Videotron", "Yahoo"} --{"RWG Archive", "NRW Archive", "Contacts#Newspaper List", "JOBS R", "JOBS N"}

settheRowto 2

tell application "Microsoft Excel"

settheSpamFiletomakenewworkbook

settheSheettoactive sheetoftheSpamFile

set formula of range "B1" of theSheet to "Subject"

set formula of range "A1" of theSheet to "Recipients"

endtell

tellapplication "Mail"

repeatwithaBoxintheBoxes

repeat with aMessage in (get messages of mailbox aBox)

tell aMessage

mysetRecipientsAndSubject(addressofitsrecipients, theRow, theSheet, subject)

end tell

set theRow to theRow + 1

end repeat

endrepeat

endtell


onsetRecipientsAndSubject(theRecipients, theRow, theSheet, theSubject)

setTIDtotext item delimiters

settext item delimitersto ", " -- separator (comma and space)

settheRecipientstotheRecipientsastext -- convert AppleScript's list to string --> (address separated by comma)

settext item delimiterstoTID


tell application "Microsoft Excel"

setvalueofrange ("A" & theRow & ":B" & theRow) oftheSheetto {theRecipients, theSubject}

end tell

endsetRecipientsAndSubject

May 25, 2012 6:55 PM in response to Jacques Rioux

Hi Jacques, I very much appreciate the time you have taken to assist me.


I copied to script above.


Each time I run it the same error occurs.


It begins, opens up excel, extracts email addresses until line 4087 [excel]. The spript result is as follows:

"error "Microsoft Excel got an error: Connection is invalid." number -609". Each time.


Can you offer any advise?


Regards

May 26, 2012 9:13 AM in response to jlantern

Hi,



The error number -609 means that the application will not responds.

I don't know why, because I tested in an infinite loop, I have no error, I stop the script after the row 8000.


Another solution is to fill the sheet only one time.

Try this :

---------------------------------------------------------------

set theBoxes to {"RWG Archive", "NRW Archive", "Contacts#Newspaper List", "JOBS R", "JOBS N"}

settheRowto 2

tell application "Microsoft Excel"

settheSpamFiletomakenewworkbook

settheSheettoactive sheet --of theSpamFile

set formula of range "B1" of theSheet to "Subject"

set formula of range "A1" of theSheet to "Recipients"

endtell

setallValuestomygetRecipientsAndSubject(theBoxes)


tell application "Microsoft Excel" to set value of range ("A" & theRow & ":B" & (count allValues)) of theSheet to allValues


ongetRecipientsAndSubject(L)

script o

property tValues : {}

endscript

tell application "Mail"

repeat with aBox in L

repeat with aMessage in (get messages of mailbox aBox)

tell aMessage

set end of o's tValues to {my convertList(address of its recipients), subject}

end tell

end repeat

end repeat

endtell

return o's tValues

endgetRecipientsAndSubject


onconvertList(theRecipients)

settidtotext item delimiters

settext item delimitersto ", " -- separator (comma and space)

settheRecipientstotheRecipientsastext -- convert AppleScript's list to string --> (address separated by comma)

settext item delimiterstotid

returntheRecipients

endconvertList

Jan 3, 2013 6:37 AM in response to jlantern

Hi guys, I'm trying to use this script to export all senders email addresses from various mailboxes in Mac mail. Each time I run the script, Applescript tells me: Mail got an error: Can’t get mailbox "Name of the mailbox". What can I do? Is the name of the mailbox the name in the description field? Please help.

Jul 10, 2014 3:19 PM in response to Jacques Rioux

Hi Jacques,


I want to do the same but also pull the date the emails are sent along with subject, content and recipients. I have this script but it does not work for me. I am novice. Any help is appreciated.


tell application "Microsoft Excel"
  set LinkRemoval to make new workbook
  set theSheet to active sheet of LinkRemoval
  set formula of range "D1" of theSheet to "Message"
  set formula of range "C1" of theSheet to "Subject"
  set formula of range "B1" of theSheet to "Recipients"
  set formula of range "A1" of theSheet to "Date"
end tell


tell application "Mail"
  set theRow to 2
  set theAccount to "Work"
  get account theAccount
  set theMessages to messages of inbox
  repeat with aMessage in theMessages
  my SetDate(date received of aMessage, theRow, theSheet)
  my SetRecipients(recipients of aMessage, theRow, theSheet)
  my SetSubject(subject of aMessage, theRow, theSheet)
  my SetMessage(content of aMessage, theRow, theSheet)
  set theRow to theRow + 1
  end repeat
end tell


on SetDate(theDate, theRow, theSheet)
  tell application "Microsoft Excel"
  set theRange to "A" & theRow
  set formula of range theRange of theSheet to theDate
  end tell
end SetDate


on SetRecipients(theRecipients, theRow, theSheet)
  tell application "Microsoft Excel"
  set theRange to "B" & theRow
  set formula of range theRange of theSheet to theRecipients
  end tell
end SetRecipients


on SetSubject(theSubject, theRow, theSheet)
  tell application "Microsoft Excel"
  set theRange to "C" & theRow
  set formula of range theRange of theSheet to theSubject
  end tell
end SetSubject


on SetMessage(theMessage, theRow, theSheet)
  tell application "Microsoft Excel"
  set theRange to "D" & theRow
  set formula of range theRange of theSheet to theMessage
  end tell
end SetMessage

Extracting all email addresses contained in emails.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.