Apple’s Worldwide Developers Conference to kick off June 10 at 10 a.m. PDT with Keynote address

The Keynote will be available to stream on apple.com, the Apple Developer app, the Apple TV app, and the Apple YouTube channel. On-demand playback will be available after the conclusion of the stream.

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

applescript for extracting attachments from mail

Hi everyone, I'm using this Applescript to extract all attachments from selected Emails:


set p2d to (path to) / Users / x / y / z / as text
tell application "Mail"
	set theMessages to the selection
	repeat with aMessage in theMessages
		set attachCount to count of (mail attachments of aMessage)
		if attachCount is not 0 then
			tell aMessage
				repeat with anAttachment in (mail attachments)
					-- check that your system return the name with its extension
					set theName to name of anAttachment
					tell me to set aFile to (p2d & theName) as «class furl»
					save anAttachment in aFile
				end repeat
			end tell
		end if
	end repeat
end tell


This works great, except if there are two attachments with the same filename. Then the one saved first gets overwritten by the second.


Can you advise me on how to add code to either prompt me, or automatically add a random string to the file name?


thanks,


geb



MacBook Pro 15″, macOS 10.13

Posted on Sep 4, 2021 10:05 AM

Reply
Question marked as Best reply

Posted on Sep 7, 2021 11:53 AM

> Can you tell me how to append "attachCount" to the file name?


Conceptually, that's really easy - just use AppleScript's text concatenation ' & ' to combine any number of variables.


For example, instead of:


	set theName to name of anAttachment 


You'd write something like:


	set theName to name of anAttachment & " some other string " & (random number from 1 to 10)


The tricky part is dealing with filename extensions. In the first case you might get something like 'filename.pdf', in the second to you'd get 'filename.pdf someotherstring 7"


What you really need to do is insert the id/randomizer into the middle of the string, and that's a little more complicated, and for that you need to get a little more creative.


You also need to determine if you want the ID appended all the time, or only when there is a name conflict, since that affects the flow.


In short, it's all possible, but you need to detail the expectations in order to get a result that works.




Similar questions

7 replies
Question marked as Best reply

Sep 7, 2021 11:53 AM in response to gebseng2

> Can you tell me how to append "attachCount" to the file name?


Conceptually, that's really easy - just use AppleScript's text concatenation ' & ' to combine any number of variables.


For example, instead of:


	set theName to name of anAttachment 


You'd write something like:


	set theName to name of anAttachment & " some other string " & (random number from 1 to 10)


The tricky part is dealing with filename extensions. In the first case you might get something like 'filename.pdf', in the second to you'd get 'filename.pdf someotherstring 7"


What you really need to do is insert the id/randomizer into the middle of the string, and that's a little more complicated, and for that you need to get a little more creative.


You also need to determine if you want the ID appended all the time, or only when there is a name conflict, since that affects the flow.


In short, it's all possible, but you need to detail the expectations in order to get a result that works.




Sep 4, 2021 6:23 PM in response to gebseng2

gebseng2 wrote:

I didn't find any tools to do this so far.

Technically, you are already using one. It just isn't a very good one.

Decoding doesn't seem to be the problem, I kust want to change or add to the file name when saving the attachment

In other words, you aren't able to correctly decode it.


You have "attachCount" already. Just append that to the file name. You will need to split the name apart into base and extension. It might be easier to just create a new directory using the attachment's "id" property.

applescript for extracting attachments from mail

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