Attach Finder Selection(s) to New Apple Mail Message

Here is a "Vibe Coded" Applescript to attach files and/or folders to a new Apple Mail message.

To use it, select file(s) and/or folder(s) in the Finder, then run the script as a Keyboard Maestro "Execute An Applescript" action.

It will switch to Apple Mail and create a new message with the selected files and/or folders attached.

Apologies in advance to anyone who finds a Vibe Coded Applescript offensive.

As a low commitment, low investment, novice coder, I make no claims as to the quality or serviceability of this script.

It works and that is good enough for me. I will update this post if I find issues with the script.

27) Attach to Mail.kmmacros (21 KB)

While never averse to some AppleScript action -- don't forget that you can do the same by clicking the Finder toolbar "Share" button and selecting "Mail" from the list. It zips folders, too.

Absolutely awesome though, and a complete surprise to me, is that this works:

image

1 Like

I always appreciate a reminder. I did this to work around having the Finder window toolbar, and thereby share button, permanently covered by Keyboard Maestro palettes. Plus, this saves a click and a few microseconds (don't want to waste perfectly good microseconds, do you?) waiting for the share menu to pop up, locating the mail option, and clicking it.

Nice use of the Press Button action's relatively new ability to identify most if not all of the buttons of an app. Now if Peter would "just" do the same for Radio Buttons, Check and Text Boxes which I imagine are a nightmare of implementation schemes, UI programming would be awesome. Guess I'll have to wait for Apple to get AI game rather than AI lame. :wink:

1 Like

Ah, gotcha.

Actually, it didn't for the "Mail" button -- the Share pane would have to be open, but as soon as you switch out of Finder to KM it closes again. But it took only a couple of guesses after a quick dig using UIElementInspector.

This brings up a question I don't know the elements of to ask clearly, namely, what is identified as a button? I certainly wouldn’t think of all those options in the Share pane (or is it a menu or sheet?) as buttons.

Could you post the AppleScript code, please.

I should caution that this is unedited ChatGPT output. I know enough AppleScript to have this not appear as Greek to me, but nowhere near enough to know if this is good, decent, or maybe really bad coding. If it gives you some ideas that work in your own scripts, that’s great but I would not presume it's a good example to follow.

This is generally my thought about AI output. It's best for ideas and summaries or things that have been carefully curated for but outside of that, it’s still a very early work in progress.

-- Helper to zip a folder and return the path to the zip
on zipFolder(theFolderPOSIX)
	set zipName to (do shell script "basename " & quoted form of theFolderPOSIX) & ".zip"
	set tempDir to (do shell script "mktemp -d /tmp/mailattach.XXXXXXXX")
	set zipPath to tempDir & "/" & zipName
	-- Zip the folder
	do shell script "cd " & quoted form of (POSIX path of (theFolderPOSIX & "/..")) & " && zip -r " & quoted form of zipPath & " " & quoted form of (do shell script "basename " & quoted form of theFolderPOSIX)
	return zipPath
end zipFolder

tell application "Finder"
	set theSelection to selection
	if theSelection is {} then
		display dialog "No files or folders selected in Finder." buttons {"OK"} default button 1
		return
	end if
	set attachList to {}
	repeat with anItem in theSelection
		set itemAlias to (anItem as alias)
		set itemPOSIX to POSIX path of itemAlias
		if (kind of anItem) is "Folder" then
			-- Zip the folder and add the zip to the list
			set zipPath to my zipFolder(itemPOSIX)
			set end of attachList to zipPath
		else
			set end of attachList to itemPOSIX
		end if
	end repeat
end tell

tell application "Mail"
	set newMessage to make new outgoing message with properties {visible:true}
	repeat with aFile in attachList
		try
			tell newMessage to make new attachment with properties {file name:aFile} at after the last paragraph
		on error errMsg
			display dialog "Error attaching file: " & errMsg buttons {"OK"} default button 1
		end try
	end repeat
	activate
end tell
1 Like