Import images from Mail to Photos


I am trying to import images from mail.
Almost done, but I am struggling with the itself Photos import command.
I cannot import individual attachment nor the attachment list.

Show us what you got so far

set targetParentFolderName to "- AAA práce AAA"
set targetNestedFolderName to "THMP VO"

tell application "Mail"
	set MailAccount to account "Google"
	set MailFolder to mailbox "INBOX" of MailAccount --account "Google"
	set Mailselection to (get selected messages of message viewer 1)
	repeat with Zprava in Mailselection
		if (count of mail attachments of Zprava) > 0 then
			set attachmentsList to mail attachments of Zprava
			set Predmet to subject of Zprava
			set ZM to rich text 4 thru 7 of Predmet

			tell application "Photos"
				set Jmeno_Alba to ZM
				if exists album Jmeno_Alba of folder targetNestedFolderName of folder targetParentFolderName then
					set Akt_Album to album Jmeno_Alba of folder targetNestedFolderName of folder targetParentFolderName
					set Akt_Album to make new album named Jmeno_Alba at folder targetNestedFolderName of folder targetParentFolderName
				end if
				-- This is the crucial point:
				import attachmentsList into Akt_Album without skip check duplicates
			end tell
		end if
	end repeat
end tell

(Backticks for formatting AppleScript code on this site added by NG. There’s a Markdown reference here.)

Some variables are in Czech, sorry. At this moment I have workaround.
I save the images to a folder on disk and the import to Photos. But seems to me complicated. Must be a simpler way…
My guess was to import images directly from mail message. Instead of save individual images as single files, get list of iamges form message and import this list to Album.
I didn’t found any way, to get the list of images form message.

Code is a lotmore readable if you enclose it in three back ticks:
code goes here
Also, I doubt that Photos can directly import mail attachments. Try saving the attachment to a file and then importing that file. But you found that out already.

A mail attachment is just “something”. In fact, an image is a bunch of base64 codes. Nothing that Photos would understand. Only when you save the attachment in a file does it become an image proper.


when I was looking into object dictionary, it seems to me, there are no methods to perform the action as I want - not on Mail or Photos app. So at the moment I work with the workaround.
Anyway - the attachment MUST be some kind of object, if we can save it as file…

It is represented as some kind of object in the scripting infrastructure. In the mail message, it is only base64 text.
Willst you can and can’t do with an object is up to the application providing the interface to it. And apparently. Apple’s Mail does not provide coercion of attachments to their underlying „real“ types.

OK. Thanks for explanation and confirmation that there is no better way.
I appreciate this sever with its knowledgebase and users to helping with Applescript.
I moved from VBA and Applescript is kind of strange language due its “naturality”, it is not so strict as other languages what makes me sometimes confusing. Especially the “alias” construction or month representation of date items. :slight_smile: .
Thanks for your help!

In most cases, you can use JavaScript instead of AppleScript. That’s what I do.

You have to check the specifications of each application.’s “import” commend is in AppleScript dictionary which can be read with Script Editor or Script Debugger.

“import” command requires file or list of file.

Photos require some image must be a file.

So, at first you have to save Mail attachment to appreciate path then you can import it to

In most cases your words seems not true.

This is 10 minites’ work.

To increase the processing speed, you can create a RAM disk, write files in it, import into You create a RAM disk, import files, destroy the RAM disk.

In this case, intermediate files will be written to RAM memory instead of hard disk. This is almost 100 times faster and also saves the hard disk.

I can’t Java Script. :neutral_face: Tried many times, no luck.

Hi, yes RAM disk is a clever solution, but it is about 15 images per e-mail, so everytime I save images to disk, import, erase.
The only thing, what bothers me is to set the Photos dialog when importing “former duplicates” which currently are not in Photos app, but was previously imported and then deleted. I have to confirm, “yes, for all cases”.

Is there need to speed up? It took about 3 seconds + 1 second’s delay.
I don’t think RAM disk does not solve this.

I don’t know why both of you are recommending the OP not to use a RAM disk. In addition to the fact that this will give speed, the hard disk will not deteriorate from repeated write-erase of intermediate files.

The user’s TMP directory is located on the hard drive. So you are giving very bad advice. Writing to a RAM disk is equivalent to storing the image in a runtime variable.

For my part, I am sure that this is one of the few cases when it is beneficial to use a RAM disk.

(1)Disk access is not the bottleneck

My machine is M1 Mac mini. This machine use SSD connected to M1 internal fabric. So, disk access is not the bottleneck. Other OSA language does not change the processing duration (JXA will take 2.5x time)

(2)’s internal processing is the bottleneck

Almost all of processing time is’s internal processing. This does not change if you use RAM disk in this case

(3)Faster processing method exists

If we talk with with AppleEvents, we can not process attached images speedy.
We have more faster way to do it.
MailCore2 framework can handle .eml files. .eml is exported email from
AppleScript with MailCore2 framework can extract attached files speedy and it does not require RAM disk.
I do not deny the value of RAM Disk, but rather negative for non-efficient use.

I see no reason to hide from the user the fact that in this case his script will repeatedly write / erase files on the hard drive.