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:
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.
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.
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