Create an automator task that adds whole folders as mail attachments

Hello,
this is my first post here, I need a little help or hint:
I’m struggling making a super simple task in Automator: I need to create a Finder service that is able to add a whole folder as attachment to a mail with precompiled text. I don’t know why it works only if I select a file, when selecting a folder I get an error "OSStatus -10000”.
The reason why I may need to add the whole folder is because I can have files with the same name (e.g. Report.pdf) in different folders (e.g. 2024-12-31 and 2024-12-30).

E.g. I want to select both folders 2024-12-30 and 2024-12-31 and with a single click have a precompiled mail with a standard text and recipients ready to send with the two folders attached.

Of course the script must work in case I select a file, but this is already working.

To reproduce the error, the script can be extremely simple and easily reproducible:

  1. create new email
  2. get specified finder items (adding a folder)
  3. add attachment to front message.

The strange thing is that the “add attachment to front message” action should accept both files and folders, as stated in Automator.

When right clicking a folder and sharing with mail from Finder it gets added correctly to a new email, so there must be a way to solve this problem.

Thank you very much and happy new year everybody

Maybe add in a “create archive” in between “Get specified finder items” and “add attachment?”

Andrew

Hello Andrew, thank you for your nice suggestion.

This is a solution that I thought about, but when I attach simple files instead of folders I don’t like them to be compressed, because without compression is easier to preview them in sent mail folder with quicklook!

Maybe there is a way with AppleScript? I’m super newbie to AppleScript, I tried copying a script found somewhere on the internet that creates a precompiled mail with the attachment, but worked only with files and not with folders…I suspect this is a bug

I believe that folders can be anttached only if they are zipped first. Perhaps selecting the folder, then sorting through the files until a match for report.pdf is found and attaching only that file?

Andrew

This unfortunately would not work since “report.pdf” is an example name I used to explain, the actual name inside the folder can be different from case to case.
I think one possible workaround would be perform the zipping only if I select a folder, otherwise attach the plain file. And possibly automatically delete the zipped file after the mail is successfully sent.
Can this be achieved? I think this should be written in an AppleScript inside Automator

Yes, it does seem like this is possible. I’m wondering what else is in the folder. Are there other pdfs or just the report? If it’s the only pdf in the folder, that would be an easy filter. Alternatively, can the reports be named in such a fashion that they have something unique to the file name so they can be filtered out by the script? “Something something report.pdf” could be filtered for “report.pdf.”

The folder can contain 1, 2, 3 up to an indefinite number of files, all PDFs. When there is only one, I tend to attach the file directly to the mail, while if there are more than one I used to attach the folder, but attaching a zip file can work also.
I would not like to edit the file names, since they come out straight from a survey procedure (in my case is SafetyCulture)
I think it should be created an applescript that reads the kind of input file, if it’s “folder” → compress → mail, otherwise attach directly to mail

For your information, I just tested the script in an old mac running Monterey and it works well also with folders as I would expect, so it is a bug introduced with newer systems…will update my feedback report to Apple hoping they will solve

Hello, I come back after some months with some updates.

Since it’s still not possible to add plain folders to mail as attachments using the basic automator actions, surfing the web I found an ASObjC script that does exactly what I need. I can start from files/folders and have the mail composed, also formatted in HTML (with classic AS scripts I didn’t find how to automatically underline a text, only make it bold or italic) and with the possibility to automatically add recipients.

Here is the script (of course it’s a test which I will need to populate with actual body messages and recipients, so the text is still the one provided ):

use framework "Foundation"

use framework "AppKit"

use scripting additions

property NSUTF8StringEncoding : a reference to 4

property NSSharingServiceNameComposeEmail : a reference to current application's NSSharingServiceNameComposeEmail

property NSAttributedString : a reference to current application's NSAttributedString

property NSString : a reference to current application's NSString

property NSSharingService : a reference to current application's NSSharingService

on run {input, parameters}

display dialog "Message Subject" default answer ""

set subj to text returned of the result

set headlineText to "<p style=\"font-family:-apple-system,san-serif;font-size:64px;text-align:left;margin-bottom:0;\">&#63743;</p>"

set bodyText to "<h1 style=\"font-family:-apple-system;\">This is a headline level 1</h1><h2 style=\"font-family:-apple-system;\">This is a headline level 2</h2><p style=\"font-family:Helvetica;font-size:16px;color:red;text-align:left;\"> Nulla vitae elit libero, a pharetra augue. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec sed odio dui.</p>"

set messageSubject to subj

set recipientAddresses to {"johnny-appleseed@apple.com"}

set thisHTML to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\" /><title>MAIL TEMPLATE</title></head><body style=\"margin:0;width:100%;Height:100vh;\">" & headlineText & return & bodyText & return & "</body></html>"

set theSource to NSString's stringWithString:thisHTML

set theData to theSource's dataUsingEncoding:NSUTF8StringEncoding

set anAttributedString to NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:(missing value)

set attachmentsArray to {}

repeat with currentFile in input

set filePath to POSIX path of currentFile

set fileURL to (current application's NSURL's fileURLWithPath:filePath)

copy fileURL to end of attachmentsArray

end repeat

set aSharingService to NSSharingService's sharingServiceNamed:(NSSharingServiceNameComposeEmail)

if aSharingService's canPerformWithItems:{"someone@somewhere.com"} then

aSharingService's setSubject:messageSubject

aSharingService's setRecipients:recipientAddresses

set allItems to {anAttributedString} & attachmentsArray

aSharingService's performSelectorOnMainThread:"performWithItems:" withObject:(allItems) waitUntilDone:true

end if

end run

I have some questions on this script:

is there a method to automatically set the signature (equivalent to message signature:(signature named “signature”) used in classic AS scripts)?
is there a method to automatically populate also the CCs and BCCs fields?

Thank you

Edit: this script as-is works if used as Finder action or inside Automator

1 Like

aleottantotto. I looked at the documentation for the NSSharingService class (here), and there is a recipients property but not a ccRecipients or bccRecipients property. Also, I don’t see any support for signatures. I’ve never had occasion to use this class before, and perhaps another forum member will know how to do what you want.

Just for a project to work on, I wrote a barebones shortcut that does most of what you want, and this worked on my Tahoe computer. As written, this adds files in two folders, but this can be changed. However, If you’re on an older version of macOS this shortcut won’t work, and I don’t know how to add signatures.

Send Email with Attachments.shortcut (22.7 KB)

Hello peavine, thank you very much for your reply.
Unfortunately I wasn’t able to fully test your shortcut, because it doesn’t seem to add attachments for some reason. The main problem I would like to solve is to add entire folders directly as attachments, not their content (see picture)…
This was fully working up to Monterey also in Automator (which for this particular need is more than enough), but stopped working (only when input for attachments is a folder) starting from Ventura giving error OSStatus -10000.

On the other side, with my script that uses NSSharingService I can still attach a folder if I want to, without compressing or extracting its content, but it turns out I’m limited on adding CCs, BCCs and setting signature.

With AppleScript you can set all of these parameters, except for underlined text (which can be set in NSSHaringService with HTML), but starting from Ventura I’m limited with the attachments.

I don’t have Tahoe installed (I’m stuck with 2018 MBP), but I made a quick test using automator in an Apple Store and the bug is still there. I’m reporting the feedback to Apple that this is not working for every update they push out, with no success for the moment.

The thing that makes me sad is that it was working with previous OS, and in Automator it’s clearly stated that “create new email” action accepts files and folders as input, so there is absolutely no reason for this super simple task to not work anymore…!

Maybe someone knows another method that can solve this problem, this would be fantastic

1 Like

aleottantotto. Sorry! I should have read your posts more carefully. I wasn’t aware that you can attach a folder of files to an email.

I spent some time testing options (except Automator), and I can’t see a solution that does everything you want. My results (on Tahoe) are the same as yours:

  • A basic AppleScript. You can’t send a folder of files as an attachment.

  • A shortcut. You can’t send a folder of files as an attachment.

  • ASObjC script. You can send a folder of files as an attachment, but Cc and Bcc recipients cannot be set.

  • Use mailto URL with NSWorkspace. Supports Cc and Bcc recipients but not attachments (as far as I know).

It’s odd that you can manually attach a folder of files to an email in the Mail app, and that works fine, but you can’t attach a folder of files in an email with AppleScript. Apple’s support of AppleScript is spotty at best, so I guess this shouldn’t surprise.

Hopefully, someone has a solution for you.

2 Likes

It’s odd that you can manually attach a folder of files to an email in the Mail ap

When I drop the folder, it seems to just zip it and attach it.

20251024_001_32014_604x412-3

“Maybe that’s a Mail.app-specific setting that makes it look like you’re attaching a ‘folder,’ with the header x-mac-auto-archive=yes;?”

1 Like

Thanks IceFole. That explains things.

Using the ASObjC script (with NSSharingService), I created and sent myself an email with a folder attachment. After receiving the email, the folder attachment was in fact a zip file.

1 Like

I don’t think that the email standards allows for sending a folder or directory.

They aren’t really about the things you’re trying to send (ie files), rather they are about a hierarchy or relationship between things. None of the standards I’ve read (mail format, smtp, attachments, etc…) even discuss the concept (unless my eyes glazed over at the wrong moment).

1 Like

aleottantotto
script didn’t run for me(macOS26) (got error "" number -1721)
but I used it as a reference and confirmed that
I could generate an email with an attached folder using NSSharingService myself!
Aha! So it can be done. I thought it wasn’t possible since it fails with

make new attachment with properties {file name:AliasFilePath}.

end if
<--Haven't actually tried this yet, but...
<--"Could we use AS (AppleScript) to add a signature to the outgoing message here?"
end run

peavine
I’m always learning new things from your shortcut posts, so I’m happy if I could help even a little! :blush:

Mockman
I agree that attaching folders isn’t generally a great idea.

However
I realized one small good thing about this particular method

I saw that the attached folder is automatically converted to a ZIP file
but the process of unzipping the file back into a folder
(whether it’s saved to the Desktop or the Downloads folder)
is done in isolatio within
~/Library/Containers/com.apple.mail/Data/Library/Mail Downloads.

I think it’s just a tiny bit safer than unzipping a ZIP file directly on the Desktop.
but it means they’re being unzipped somewhat more safely
It’s just a small reassurance, I guess. That’s what I thought.
It’s a negligible safety margin, I know, but better than nothing.

FWIW, I edited the ASObjC solution as a standalone script, which worked as expected on my Tahoe computer. The canPerformWithItems method doesn’t actually do anything in my testing–it always returns true even when the email address is a faulty one. I need to spend some time with the NSSharingService class to better understand its operation.

--the following is an edit of an AppleScript posted by Sal Soghoian

use framework "AppKit"
use framework "Foundation"
use scripting additions

--set recipients (sender is set in Mail app)
set recipientAddresses to {"RecipientOne@SomeService.com", "RecipientTwo@SomeService.com"}

--set message subject
set messageSubject to "Test Email"

--set paths of message attachments
set attachmentPaths to {"/Users/robert/Test File.txt", "/Users/robert/Test Folder"}

--set formatted body text
set headlineText to "<p style=\"font-family:-apple-system,san-serif;font-size:64px;text-align:left;margin-bottom:0;\"></p>"
set bodyText to "<h1 style=\"font-family:-apple-system;\">This is a headline level 1</h1><h2 style=\"font-family:-apple-system;\">This is a headline level 2</h2><p style=\"font-family:Helvetica;font-size:16px;color:red;text-align:left;\"> Nulla vitae elit libero, a pharetra augue. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec sed odio dui.</p>"
set thisHTML to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\" /><title>MAIL TEMPLATE</title></head><body style=\"margin:0;width:100%;Height:100vh;\">" & headlineText & return & bodyText & return & "</body></html>"
set theSource to current application's NSString's stringWithString:thisHTML
set theData to theSource's dataUsingEncoding:4
set anAttributedString to current application's NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:(missing value)

--create array of file URLs of attachments
set attachmentURLs to current application's NSMutableArray's new()
repeat with aPath in attachmentPaths
	set aURL to (current application's NSURL's fileURLWithPath:aPath)
	(attachmentURLs's addObject:aURL)
end repeat

--merge formatted body text and file URLs of attachments
set allItems to current application's NSMutableArray's new()
allItems's addObject:anAttributedString
allItems's addObjectsFromArray:attachmentURLs

--make email
set aSharingService to current application's NSSharingService's sharingServiceNamed:(current application's NSSharingServiceNameComposeEmail)
if (aSharingService's canPerformWithItems:{"Sender@SomeService.com"}) is true then
	aSharingService's setSubject:messageSubject
	aSharingService's setRecipients:recipientAddresses
	aSharingService's performSelectorOnMainThread:"performWithItems:" withObject:(allItems) waitUntilDone:true
else
	display dialog "The email could not be created." buttons {"OK"} cancel button 1 default button 1
end if

I ran some tests, and I agree with IceFole that there is an advantage to attaching a folder of files to an email, instead of attaching a ZIP file that contains a folder of files. This means that there still is not a solution for aleottantotto. Perhaps using an AppleScript or Automator to compose the email including Cc and Bcc recipients and then manually attaching folders with the Mail app is the closest that can be achieved.

Thank you very much to all contributors that spent some time with this testing.

Honestly I never tried to e-mail myself with a folder attached, and never asked how recipients actually see the attachment (if folder or zip file), my idea is that when I forget what attachments I sent it’s half the time dragging a folder to the desktop to browse instead of dragging a zip file and expand it. In case you (correctly) wonder why I do not attach the files directly is because I have files with same name but different content sent at the same time, and it would be a total mess if they mix together. Of course this procedure is only when these conditions are met, not for every e-mail I send :smiley:

If you open Automator and look for “add attachments” action in mail, in its description it’s clearly stated it accepts files or folders as input (see picture). The proof is that up to macOS 12 it worked in Automator, and starting from Ventura the same script gives an error OSStatus -10000. So I’m confident that the basic AppleScript that creates an e-mail but now only seems to accept files as input, back to Monterey it would have accepted also folders.

That’s a very nice discovery! macOS never stops surprising me :slight_smile:

Of course the script I posted came from there, I remember that I found it also on other websites and initially I had the limitation that only one file can be added that now seems solved; when I posted I didn’t know if I had to credit since that script is spread in many places on the web, but if I had to and I didn’t, please excuse me

that’s exactly what I’m doing now, but it feels like being capped at 95%
Screenshot 2025-10-25 alle 00.52.42

1 Like

I’m still a bit mystified by how the canPerformWithItems method is supposed to work and what it’s purpose is when sending an email.

The following is the applicable section of Sal’s AppleScript:

set aSharingService to NSSharingService's sharingServiceNamed:(NSSharingServiceNameComposeEmail)
if aSharingService's canPerformWithItems:{"someone@somewhere.com"} then
	set aSharingService's subject to messageSubject
	set aSharingService's recipients to recipientAddresses
	tell aSharingService to performSelectorOnMainThread:"performWithItems:" withObject:{anAttributedString} waitUntilDone:false
end if

Google AI’s general discussion of this topic is:

When an application wants to offer the “Share via Mail” option, it follows these steps:

  1. Request a sharing service: The app first gets a reference to the NSSharingService object for the Mail client, using its service name.

  2. Pass items for validation: The app then calls the canPerformWithItems: method on the Mail service object, passing it an array of the data the user wants to share (e.g., NSString for text, NSImage for an image, or NSURL for a file).

  3. Validate compatibility: The canPerformWithItems: method returns a boolean (true or false) to indicate if the Mail service can handle all the items. This check can fail if:

  4. No email client is configured: The user has not set up a mail account on their Mac.

  5. Invalid item types: The items provided are of a type that the Mail service cannot process.

  6. Display or hide the option: Based on the method’s response, the app can decide whether to enable or disable the “Share via Mail” option in the user interface.

The Apple documentation simply defines the items paramameter as the items to share.

If the item checked is the email address, then an error should be reported when the item checked is not an email address–but that doesn’t happen in my testing. If the items checked are files, images, and so on, then those items should be specified. I haven’t tested this because I’m not sure how to reference those types of items as a value of the parameter. Sal obviously knows a huge amount more than I do about this topic, so I’m just missing something.

I don’t know that I’ll ever have reason to use this, but I was curious if anyone knows how this is supposed to work? Thanks!