Exporting Mail Attachments

Hi, after googling in vain … I need help if possible.
I use Apple Mail and I hope to find a script which would allow me to copy certain attachments to a folder I specify
The attachment are all Premiere Pro projects and the share the same extension “.prproj”
I am unable to find a way to create the script to copy the attachments from the list of found files in MAIL.
I only need the script to copy the files to the folder adding the timestamp as a prefix to the file.
The result should be something as follows
dd-mm-yy-hh-mm-ss-attachmentname

as such
09-12-17-14-23-12 example.prpoj

this file should be clickable to open in Premiere

The dictionary for MAIL is very complex for my skills but it seems possible to download attachments with a script.

Would anyone help me?

Thanks a lot in advance

Dan

Model: MACBOOKPRO
Browser: Safari 537.36
Operating System: Mac OS X (10.11.6)

Hello

I don’t remember where I got the script below.

set mailBoxName to "fournisseurs"
set catName to "Apple"
set attFolder to (path to desktop as text) & "Mail attachments"
set emailApp to "Mail.app"

tell application "Mail"
	activate
	try
		set emailCount to count of messages of mailbox (mailBoxName & "/" & catName)
		set attachmentCount to 0
		set errorCount to 0
		repeat with i from 1 to emailCount
			set theMessage to item i of messages of mailbox (mailBoxName & "/" & catName)
			set theAttachments to every mail attachment of theMessage
			repeat with a from 1 to length of theAttachments
				set theAttachment to item a of theAttachments
				try
					set theAttachmentName to name of theAttachment
					set theSavePath to attFolder & ":" & theAttachmentName
					close access (open for access theSavePath)
					save theAttachment in file theSavePath
					set attachmentCount to attachmentCount + 1
				on error errMsg number errNum
					set errorCount to errorCount + 1
				end try
			end repeat
		end repeat
		display dialog "Saved " & attachmentCount & " files, with " & errorCount & " errors" with title "Attachment Report" buttons "OK"
	on error errMsg number errNum
		display dialog "No message selected or error " & return & errNum & space & errMsg ¬
			buttons {"Quit"} default button 1 with title emailApp
		return {false, "", ""}
	end try
end tell

Yvan KOENIG running High Sierra 10.13.2 in French (VALLAURIS, France) vendredi 12 janvier 2018 10:25:41

Thanks for your time and reply
unfortunately on EL Capitan and with AppleScript ver 2.5 and Script Editor Version 2.8.1 (183.1) receive this error

No message selected or error
-1728 Mail got an error: Can’t get mailbox “Icloud/Apple”.

Originally I used:
set mailBoxName to “fournisseurs”
as in the script you suggested but I got exacly the same error as after changing the name to
Inbox, or Icloud or Danwan I aways with the same error as a result

I select all the messages I need but the script tells me NO MESSAGE SELECTED …

Any ways I can make this script work?

Thanks again for your time

I apologizes, I don’t use iCloud.

Here I got this events log :

tell current application
	path to desktop as text
end tell
tell application "Mail"
	activate
	count every message of mailbox "fournisseurs/Apple"
	get item 1 of every message of mailbox "fournisseurs/Apple"
	#…
	get item 33 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 109061 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2.3" of message id 109061 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:bug-recherche-apple-music-450d2.jpg"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:bug-recherche-apple-music-450d2.jpg"
end tell
tell application "Mail"
	close access 109
end tell
tell current application
	close access 109
end tell
tell application "Mail"
	save mail attachment id "2.3" of message id 109061 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:bug-recherche-apple-music-450d2.jpg"
	get name of mail attachment id "2.2" of message id 109061 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:resultats-recherche-apple-music-e0f9b.jpg"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:resultats-recherche-apple-music-e0f9b.jpg"
end tell
tell application "Mail"
	close access 110
end tell
tell current application
	close access 110
end tell
tell application "Mail"
	save mail attachment id "2.2" of message id 109061 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:resultats-recherche-apple-music-e0f9b.jpg"
	get item 34 of every message of mailbox "fournisseurs/Apple"
	#…
	get item 38 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 109059 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2.2" of message id 109059 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.23.51.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.23.51.png"
end tell
tell application "Mail"
	close access 111
end tell
tell current application
	close access 111
end tell
tell application "Mail"
	save mail attachment id "2.2" of message id 109059 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.23.51.png"
	get name of mail attachment id "2.4" of message id 109059 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.52.49.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.52.49.png"
end tell
tell application "Mail"
	close access 112
end tell
tell current application
	close access 112
end tell
tell application "Mail"
	save mail attachment id "2.4" of message id 109059 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.52.49.png"
	get name of mail attachment id "2.3" of message id 109059 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.36.34.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.36.34.png"
end tell
tell application "Mail"
	close access 113
end tell
tell current application
	close access 113
end tell
tell application "Mail"
	save mail attachment id "2.3" of message id 109059 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.36.34.png"
	get item 39 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 109559 of mailbox "fournisseurs/Apple"
	get item 40 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 109058 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2.2" of message id 109058 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.23.51.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.23.51.png"
end tell
tell application "Mail"
	close access 114
end tell
tell current application
	close access 114
end tell
tell application "Mail"
	save mail attachment id "2.2" of message id 109058 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.23.51.png"
	get name of mail attachment id "2.4" of message id 109058 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.52.49.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.52.49.png"
end tell
tell application "Mail"
	close access 115
end tell
tell current application
	close access 115
end tell
tell application "Mail"
	save mail attachment id "2.4" of message id 109058 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.52.49.png"
	get name of mail attachment id "2.3" of message id 109058 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.36.34.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.36.34.png"
end tell
tell application "Mail"
	close access 116
end tell
tell current application
	close access 116
end tell
tell application "Mail"
	save mail attachment id "2.3" of message id 109058 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T16.36.34.png"
	get item 41 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 109057 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2.2" of message id 109057 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T15.52.51.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T15.52.51.png"
end tell
tell application "Mail"
	close access 117
end tell
tell current application
	close access 117
end tell
tell application "Mail"
	save mail attachment id "2.2" of message id 109057 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T15.52.51.png"
	get name of mail attachment id "2.3" of message id 109057 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T15.34.35.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T15.34.35.png"
end tell
tell application "Mail"
	close access 118
end tell
tell current application
	close access 118
end tell
tell application "Mail"
	save mail attachment id "2.3" of message id 109057 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2015-09-28T15.34.35.png"
	get item 42 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 109558 of mailbox "fournisseurs/Apple"
	#…
	get item 71 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11968 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2.2" of message id 11968 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2013-10-25T14.19.39.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2013-10-25T14.19.39.png"
end tell
tell application "Mail"
	close access 119
end tell
tell current application
	close access 119
end tell
tell application "Mail"
	save mail attachment id "2.2" of message id 11968 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2013-10-25T14.19.39.png"
	get item 72 of every message of mailbox "fournisseurs/Apple"
	#…
	get item 93 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11923 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2.2" of message id 11923 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2012-04-28T16.17.00.png"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2012-04-28T16.17.00.png"
end tell
tell application "Mail"
	close access 120
end tell
tell current application
	close access 120
end tell
tell application "Mail"
	save mail attachment id "2.2" of message id 11923 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2012-04-28T16.17.00.png"
	get item 94 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11906 of mailbox "fournisseurs/Apple"
	#…
	get item 96 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11934 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2" of message id 11934 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:merge_CSV.numbers.zip"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:merge_CSV.numbers.zip"
end tell
tell application "Mail"
	close access 121
end tell
tell current application
	close access 121
end tell
tell application "Mail"
	save mail attachment id "2" of message id 11934 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:merge_CSV.numbers.zip"
	get name of mail attachment id "4" of message id 11934 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:csv-template.csv"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:csv-template.csv"
end tell
tell application "Mail"
	close access 122
end tell
tell current application
	close access 122
end tell
tell application "Mail"
	save mail attachment id "4" of message id 11934 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:csv-template.csv"
	get item 97 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11907 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2" of message id 11907 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_08 à 10.46.46.jpg"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_08 à 10.46.46.jpg"
end tell
tell application "Mail"
	close access 123
end tell
tell current application
	close access 123
end tell
tell application "Mail"
	save mail attachment id "2" of message id 11907 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_08 à 10.46.46.jpg"
	get item 98 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11908 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2" of message id 11908 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_08 à 10.41.09.jpg"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_08 à 10.41.09.jpg"
end tell
tell application "Mail"
	close access 124
end tell
tell current application
	close access 124
end tell
tell application "Mail"
	save mail attachment id "2" of message id 11908 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_08 à 10.41.09.jpg"
	get item 99 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11909 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2.3" of message id 11909 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_07 à 23.20.48.jpg"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_07 à 23.20.48.jpg"
end tell
tell application "Mail"
	close access 125
end tell
tell current application
	close access 125
end tell
tell application "Mail"
	save mail attachment id "2.3" of message id 11909 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_07 à 23.20.48.jpg"
	get name of mail attachment id "2.2" of message id 11909 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_07 à 23.11.51.jpg"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_07 à 23.11.51.jpg"
end tell
tell application "Mail"
	close access 126
end tell
tell current application
	close access 126
end tell
tell application "Mail"
	save mail attachment id "2.2" of message id 11909 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2011_09_07 à 23.11.51.jpg"
	get item 100 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11936 of mailbox "fournisseurs/Apple"
	get item 101 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11910 of mailbox "fournisseurs/Apple"
	get name of mail attachment id "2" of message id 11910 of mailbox "fournisseurs/Apple"
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_05_03 à 15.03.05.jpg"
end tell
tell current application
	open for access "SSD 500:Users:??????????:Desktop:Mail attachments:2011_05_03 à 15.03.05.jpg"
end tell
tell application "Mail"
	close access 127
end tell
tell current application
	close access 127
end tell
tell application "Mail"
	save mail attachment id "2" of message id 11910 of mailbox "fournisseurs/Apple" in file "SSD 500:Users:??????????:Desktop:Mail attachments:2011_05_03 à 15.03.05.jpg"
	get item 102 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11918 of mailbox "fournisseurs/Apple"
	#…
	get item 119 of every message of mailbox "fournisseurs/Apple"
	get every mail attachment of message id 11930 of mailbox "fournisseurs/Apple"
	display dialog "Saved 19 files, with 0 errors" with title "Attachment Report" buttons "OK"
end tell
Résultat :
{button returned:"OK"}

As you may see, I extracted attachments from mailbox “fournisseurs/Apple”.

CAUTION, the destination folder must exist before running the script.

Yvan KOENIG running High Sierra 10.13.2 in French (VALLAURIS, France) vendredi 12 janvier 2018 18:41:45

Thank you again and sorry if I am unable to sort the problems myself.
I am using EL Capitan which might have a different way to handle MAIL app and the ScriptEditor
I added “Icloud/inbox” to the script you sent me because I thought I needed to
set mailBoxName to “fournisseurs”
with a mailboxname from my USER/MAIL/
shall I
set mailBoxName to “dantem”?

I have three accounts in my Mail application one is dantem@mac.com the others are gmail accounts which currently I do not need to import …

How can I modify your original script to download the messages and attachments creating (as from your script) a folder in my desktop as attfolder & “Mail attachments”

set attFolder to (path to desktop as text) & “Mail attachments”

Thanks

To get the names of the mailboxes I used this script:

tell application "Mail"
	name of every mailbox
	--> {" laPoste…", …, "fournisseurs", …, "Outbox", "Sent Messages", "Deleted Messages", "Junk"}
	
	name of mailboxes of mailbox "fournisseurs"
	
	--> {"Amapage", "Amazon", "Apple", "aVosMac", "Avosmac2AntiSpam", "batteries-piles-cables", "Carrefour", "CDs-DVDs", "CreaMagic", "Dell", "EDF", "Epson", "FNAC", "FranceTelecom", "free", "Impots", "la_poste", "Lapeyre", "logiciels achetés", "Lyonnaise des eaux", "MAAF", "macway", "magma", "Manutan", "MyFonts", "Nikon", "Orange", "paypal", "Phase One", "PHP", "RAM", "Revues", "Ronchamp", "SFR", "Sonnet", "yousendit"}
end tell

Yvan KOENIG running High Sierra 10.13.2 in French (VALLAURIS, France) vendredi 12 janvier 2018 20:46:09

Thank you I will try and let you know
Regards

Thanks again, and I am sorry if I seem unable to adapt this script.
Do I need to insert this before the actual script you sent me?
In the mail box i only have the standard MAIL boxes as:
Apple Mail To Do.mbox
Archive.mbox
Deleted Messages.mbox
Drafts.mbox
INBOX.mbox
Junk.mbox
Sent Messages.mbox
Trash.mbox
Unwanted.mbox

as such
tell application “Mail”
name of every mailbox
→ {" Apple Mail To Do", “Archive”, “INBOX”, “Junk”, “Sent Messages”, “Deleted Messages”, “Junk”, Unwanted}

name of mailboxes of mailbox “fournisseurs” [do i ned to leave mailbox “fournisseurs” or change it, creating a folder with whichever name on the desktop?

Also I don’t have any name but the original default names Apple assigns to my mailboxes. As I use different email account for different tasks, some from Apple others from Gmail and everything goes automatically to the specific mail box.

WHat to I need to write here then?

→ {“Amapage”, “Amazon”, “Apple”, “aVosMac”, “Avosmac2AntiSpam”, “batteries-piles-cables”, “Carrefour”, “CDs-DVDs”, “CreaMagic”, “Dell”, “EDF”, “Epson”, “FNAC”, “FranceTelecom”, “free”, “Impots”, “la_poste”, “Lapeyre”, “logiciels achetés”, “Lyonnaise des eaux”, “MAAF”, “macway”, “magma”, “Manutan”, “MyFonts”, “Nikon”, “Orange”, “paypal”, “Phase One”, “PHP”, “RAM”, “Revues”, “Ronchamp”, “SFR”, “Sonnet”, “yousendit”}
end tell

Regards

It seems that you didn’t understood what I posted.

I asked you to run the short script :

tell application "Mail"
	name of every mailbox
	name of mailboxes of mailbox "fournisseurs"
end tell

The text appearing in gray :

--> {" laPoste…", …, "fournisseurs", …, "Outbox", "Sent Messages", "Deleted Messages", "Junk"}
--> {"Amapage", "Amazon", "Apple", "aVosMac", "Avosmac2AntiSpam", "batteries-piles-cables", "Carrefour", "CDs-DVDs", "CreaMagic", "Dell", "EDF", "Epson", "FNAC", "FranceTelecom", "free", "Impots", "la_poste", "Lapeyre", "logiciels achetés", "Lyonnaise des eaux", "MAAF", "macway", "magma", "Manutan", "MyFonts", "Nikon", "Orange", "paypal", "Phase One", "PHP", "RAM", "Revues", "Ronchamp", "SFR", "Sonnet", "yousendit"}

is what returned the instructions.
The first one returns the first level of mailboxes names.
The second one returns the list of the mailboxes available in the mailbox named “fournisseurs”.

Hoping to be able to help, I ask you to run the simple script :

tell application "Mail"
	name of every mailbox
end tell

and post here the list of mailboxes which it will return in the Log history.

Yvan KOENIG running High Sierra 10.13.2 in French (VALLAURIS, France) lundi 15 janvier 2018 11:37:25