Solution for bulk mail content copying

Hello,

Description:

In “NARUSZENIA” inbox in mail.app I have a bunch of emails which content I need to copy and after that paste this text into a text editor (eg. TEXT EDIT). I need this in ONE TEXT FILE.

How It should works:

From the inbox called “NARUSZENIA” copy content of email message
Paste the copied text into a text editor (eg. text edit) in PLAN TEXT
Copy next message from inbox “NARUSZENIA” and paste the content into the same file as previously.
Repeat action until all emails will be copied and pasted.

I realize that it is time-consuming that why I offer to pay for this script. I’m not sure if this does not infringes forum rules- it it does, I can also donate this forum.

I can pay/donate 25USD for this work via BitCoin, PayPal, Revolut

Thank you!

You may try:


set everyText to {}
tell application "Mail"
	set allMessages to messages of  (first mailbox whose name is "NARUSZENIA") -- EDITED
	repeat with aMessage in allMessages
		set itsText to content of aMessage
		--set itsText to source of amessage
		set end of everyText to itsText
	end repeat
end tell

set targetFile to (path to desktop as text) & "My messages.txt"
set theData to my recolle(everyText, linefeed & "-----------------------" & linefeed)
my writeto(targetFile, theData, «class utf8», false)


tell application "TextEdit" to open targetFile as «class furl»

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====

Of course no paiement is required.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 14 avril 2020 14:46:18

Thank you. I really appreciate your reply, however there is an error.

imgur.com/a/ggOPYdE

Error state “Mail - error: Cannot reach (access) mailbox "NARUSZENIA”\

I apologize but if my script is urged to get the mailbox of an existing mailbox, it get them.
Of course, if it ask for a mailbox which doesn’t exists (here a box named “Unavailable”), I get the error which you got.

My understanding is that the mailbox whose name "NARUSZENIA " was given three times in your original message doesn’t exist.

tell application "Mail"
	messages of mailbox "Mosaic-Tumblr" -- mailbox used to test my original script
	--{message id 4652 of mailbox "Mosaic-Tumblr" of application "Mail", message id 4653 of mailbox "Mosaic-Tumblr" of application "Mail", message id 4654 of mailbox "Mosaic-Tumblr" of application "Mail"}
	messages of mailbox "Unavailable" -- fake mailbox which doesn't exists
	--> error "Erreur dans Mail : Il est impossible d’obtenir mailbox \"Unavailable\"." number -1728 from mailbox "Unavailable"
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 14 avril 2020 22:14:56

I edited message #2 because I missed that “NARUSZENIA” is not necessarily a first level mailbox.

Below is a version allowing you to choose the mailbox on the fly.

----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
property |⌘| : a reference to current application

tell application "Mail"
	set boxNames to name of every mailbox
end tell
set boxNames to my sortList:boxNames
set theBox to choose from list boxNames
if theBox is false then error number -128
set theBox to item 1 of theBox

set everyText to |⌘|'s NSMutableArray's new()
tell application "Mail"
	set allMessages to messages of (first mailbox whose name is theBox)
	repeat with aMessage in allMessages
		set itsText to content of aMessage
		--set itsText to source of amessage
		(everyText's addObject:itsText)
	end repeat
end tell

set targetFile to (path to desktop as text) & "My messages.txt"

my writeArray:everyText inFile:targetFile

tell application "TextEdit" to open targetFile as «class furl»

#=====

on writeArray:anArray inFile:hfsPath
	set theData to (anArray's componentsJoinedByString:(linefeed & linefeed))
	-- save data to new file
	(theData's writeToURL:(hfsPath as «class furl») atomically:true encoding:(|⌘|'s NSUTF8StringEncoding) |error|:(missing value))
end writeArray:inFile:

#=====

on sortList:aList
	set theArray to |⌘|'s NSArray's arrayWithArray:aList
	set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
	return theArray as list
end sortList:

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 15 avril 2020 11:24:04

Yvan,

Basically, the script is working well! Thank you it helps me VERY MUCH.

The problem I described in my previous message referred to inbox NARUSZENIA which was IMAP account. I created new mailbox (locally - ON MY MAC) and it works!

As you do not want payment I donated forum as I promise- 20USD.
Screen attached.

Thank you!

https://imgur.com/a/6iQYivm

The Mail.app has mailboxes of 2 kind: 1) its mailboxes 2) mailboxes of every account. So, to get all mailboxes it isn’t sufficient ask for every maibox. You should ask for every mailbox & every mailbox of every account:


tell application "Mail" to (its mailboxes) & (accounts's mailboxes)

The scripts of Yvan Koenig request only for mailboxes of Mail.app (root location). This is the reason why they can’t find nested mailbox “NARUSZENIA”. The corrected script should be like this:


set wholeText to "" -- set initially the whole text to empty

tell application "Mail"
	
	-- find the mailbox of nested structure of Mail.app, by name
	try -- first, search in the accounts's mailboxes (as the OP wants)
		set theBox to item 1 of (first mailbox of every account whose name is "NARUSZENIA")
	on error
		try -- second attempt: search in mailboxes of Mail.app itself
			set theBox to item 1 of (first mailbox whose name is "NARUSZENIA")
		on error
			display notification "Mailbox \"NARUSZENIA\" doesn't exist"
		end try
	end try
	
	-- append to the wholeText all messages's subject and text
	repeat with theMessage in (get messages of theBox)
		tell theMessage to set {theSubject, theContent} to {subject, content}
		if theContent is not "" then
			set wholeText to wholeText & ¬
				"___________   " & theSubject & "  ____________" & ¬
				theContent & return & return
		end if
	end repeat
	
end tell

-- make new TextEdit document with theText, and show it
tell application "TextEdit"
	set theDoc to make new document with properties {text:wholeText}
	activate
	save theDoc
end tell