mail app attachments and signatures

I’ve gleaned a lot of information searching these forums. Thanks all. Now I’ve hit a snag. Maybe not a snag, but a bug.

This script requires a a defined signature in apple mail (mail.app).

The snag/problem appears if the signature is formatted with underlining. Other formatting (colours, sizes, bolding etc.), seem to not have any problems.

The first part of the script populates the users signatures, and allows them to select one. It also allows them to select an attachment. It’s only for demonstration purposes.

  1. Create a signature in Mail with some formatting, including underlining.
  2. Run this script, and point it at the newly created signature (with underlining).
  3. The script will create a new message (no recipient, so no worries) and then stop before adding the attachment.
set list_SigNames to {}
set list_Signatures to {}
-- enumerate signatures
tell application "Mail"
	set sigcount to count of signatures
	repeat with i from 1 to sigcount
		copy name of signature i to end of list_SigNames
		copy signature i to end of list_Signatures
	end repeat
end tell

set this_SigName to (choose from list list_SigNames with prompt "Choose Your Signature:")
set this_Attachment to (choose file with prompt "Choose test file to attach:")

--- the above is just to get a valid signature, and test attachment

set this_Subject to "This is the Subject"
set this_Body to "This is the Message Body"


tell application "Mail"
	set sigcount to count of signatures
	repeat with i from 1 to sigcount
		set sig_i_Name to name of signature i
		if (sig_i_Name as text) is equal to (this_SigName as text) then
			set useSignature to signature i
			exit repeat -- found
		end if
	end repeat --  i from 1 to sigcount
	
	set theMessage to make new outgoing message with properties {visible:true, subject:this_Subject, content:this_Body}
	
	tell theMessage
		set message signature to useSignature
		display dialog "Look at Message Before Adding Attachement"
		-- add attachement and Underlining disappears.
		tell content to make new attachment with properties {file name:this_Attachment as alias} at after the last paragraph
		activate
	end tell -- theMessage
end tell -- "Mail"

Can anyone suggest a work around?

Thanks.

Hi,

your script works fine even with underlining and weird formats.
Is it really necessary to use rich text and formatted signatures?

btw: the double enumeration to identify the signature is not needed


set useSignature to ""
tell application "Mail" to set list_SigNames to name of every signature
set theResult to choose from list list_SigNames with prompt "Choose Your Signature:"
if theResult is not equal to false then
	tell application "Mail" to set useSignature to signature (item 1 of theResult)
end if

set this_Attachment to (choose file with prompt "Choose test file to attach:")

--- the above is just to get a valid signature, and test attachment
set this_Subject to "This is the Subject"
set this_Body to "This is the Message Body"

tell application "Mail"
	set theMessage to make new outgoing message with properties {visible:true, subject:this_Subject, content:this_Body}
	tell theMessage
		set message signature to useSignature
		display dialog "Look at Message Before Adding Attachement"
		-- add attachement and Underlining disappears.
		tell content to make new attachment with properties {file name:this_Attachment} at after the last paragraph -- choose file returns an alias
		activate
	end tell -- theMessage
end tell -- "Mail"

Thanks for the prompt reply.
Hmm. Underlines in signatures are constantly removed here (I created other signatures).

When the message is first created (and the display dialog pauses the script) the formatting is there.
After I press “OK”, the attachment is added and the underlining disappears. Does the same with your much cleaner version.

Not required, but preferred.
I tried creating formatted message bodies (without success).
Signatures worked (and also made customizing the text content easier).

Thanks. My actual script is reading a configuration file. The signature name is provided, and I go through the signatures looking for the matching name.

Been looking for a solution to this same problem. I am setting up a signature with Rich Text and HTML - Works perfect until I add an attachment - The styling is all lost

Anyone figure this out?


set useSignature to ""
tell application "Mail" to set list_SigNames to name of every signature
set theResult to choose from list list_SigNames with prompt "Choose Your Signature:"
if theResult is not equal to false then
	tell application "Mail" to set useSignature to signature (item 1 of theResult)
end if

tell application "Mail"
	activate
	set theSubject to "Today's Morning Brief"
	set theBody to " test"
	
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody, visible:true}
	tell newMessage
		make new to recipient at end of to recipients with properties {name:"DL", address:"somebody@gmail.com"}
		--make new cc recipient with properties {theGroup}
		set message signature to useSignature
		
		--Select a file to attach - I normally attach a PDF
		set myFile to choose file with prompt "Select attachment"
		--Choose which way to add the attachement, same result :(
		make new attachment with properties {file name:myFile as alias}
		--tell content to make new attachment with properties {file name:myFile} at after the last paragraph
		
	end tell
	
end tell