Open Mail Attachment on MacOS 15.5 Sequoia

I am looking for a script to open the (pdf) attachment in a (new) eMail using Mail.

Panascan. You can easily open an attachment in a new email with the context menu. Perhaps you could explain a bit more what you want the AppleScript to do.

I want to open the Mail attachment on command, not manually by using the mouse…
Any programming hints are welcome (which tool, which way?)

panascan. I am still not clear what specifically you want to do. FWIW, the following script opens every PDF attachment in a selected email with the default app (normally Preview). This creates temporary files for all the attachments, but I don’t know a way around that. Hopefully, another forum member will have a better understanding of your request and will be able to help.

set destinationFolder to (path to temporary items as text)

tell application "Mail"
	set selectedMessages to selection
	if selectedMessages is {} then display dialog "No Mail attachments were found" buttons {"OK"} cancel button 1 default button 1
	repeat with aMessage in selectedMessages
		repeat with anAttachment in mail attachments of aMessage
			set theFile to (destinationFolder & (name of anAttachment))
			save anAttachment in file theFile
			openPDF(theFile) of me
		end repeat
	end repeat
end tell

on openPDF(theFile)
	tell application "Finder"
		if (name extension of file theFile) is "pdf" then open file theFile
	end tell
end openPDF

We are on the right way. Thanks!
But this script just opens the attachment of a selected email.

But what I intend to do is to write a new email with an attached pdf-document. Before sending the email I would like to check the content of the pdf by calling a script to open the pdf (in Preview).

How could this been done?

panascan. There is an outgoing message class that should allow an AppleScript to do what you want, but I could not get it to work reliably. Sorry about that. Hopefully another forum member will be able to assist you.

Have you tried Automator?

The problem with AppleScript or Automator seems to be:

How is it possible to select the attachment of an outgoing message with a script?

I haven’t used Mail in years, so take this with a large grain of salt. :grin: But if the new email is in your Drafts folder, would @peavine’s script still apply?

This would appear to be a solution but there are three issues:

  1. When you create a new message, a draft is not immediately saved (at least on my Sequoia computer).
  2. A draft of a new message created automatically by the Mail app does not contain any attachments that are present in the new message.
  3. I have not been able to use an AppleScript to save a draft of a new message.

An unusable workaround is to:

  1. Modify my earlier script as shown below.
  2. Create the new message.
  3. Quit the message, which displays a prompt that inquires ā€œSave this message as a draft?ā€. Click on ā€œSaveā€.
  4. Run my modified script.

The key to this (as panascan pointed out) is the outgoing message class. Get that to work, and I suspect a solution is at hand.

set destinationFolder to (path to temporary items as text)

tell application "Mail"
	activate
	set selectedMessages to messages of drafts mailbox
	if selectedMessages is {} then display dialog "No Mail attachments were found" buttons {"OK"} cancel button 1 default button 1
	repeat with aMessage in selectedMessages
		repeat with anAttachment in mail attachments of aMessage
			set theFile to (destinationFolder & (name of anAttachment))
			save anAttachment in file theFile
			openPDF(theFile) of me
		end repeat
	end repeat
end tell

on openPDF(theFile)
	tell application "Finder"
		if (name extension of file theFile) is "pdf" then open file theFile
	end tell
end openPDF
1 Like

peavine
Your script is great!

I added this at the end to enlarge the document:

delay 0.5
tell application "System Events"
	tell process "Preview"
		repeat 2 times -- 2x ⌘+
			keystroke "+" using command down
			delay 0.2
		end repeat
	end tell
end tell

panascan. I ran a test by creating and saving as a draft a new Mail messaage with two PDF attachments, each of which contained four pages. I then ran my script, and the PDF attachments were both opened in the Preview app, and they had four pages each. So, I don’t know why you’re only seeing one page.

It was just a temporary problem - your script is fine!