This question is probably better posted in the Mac OS X Technologies section of these forums, but here goes...
You don't have to select anything (in the sense of clicking on it or highlighting it). But you do need to specify what you want to work on, and for that you need to read Mail's AppleScript Dictionary.
Basically you specify the inbox of the mail account you want to work on, and then cast the senders of those messages into a list.
tell application "Mail"
set the_senders to sender of every message of mailbox "INBOX" of account "iCloud"
This may take a minute or two depending on how many messages there are in the Inbox and how powerful your Mac is.
The senders in the resulting AppleScript list will be text strings in the form:
"Apple Support Communities Updates <email@example.com>"
or just the email address if the sender doesn't have a display name.
AppleScript has fairly limited text parsing tools but you should be able to loop through the list casting these into plain email addresses and write them to a text file.
Hope this helps!