Send Files With Mail Using AppleScript

Dear Community,

I'm using KM to run a script to attach files to Apple Mail but I'm getting the body of mail text formatting issues and I didn't find a solution.

I use dark background for messages.

Anyone can give some idea?

The actual code is:

	set fileList to selection
	if fileList ≠ missing value then
		repeat with theItem in fileList
			set contents of theItem to (get POSIX path of (contents of theItem))
		end repeat
	else
		return 0
	end if
end tell

tell application "Mail"
	activate
	
	set theSubject to "Análise TE [ area ]" -- the subject
	set theContent to "" -- the content
	set TheSender to ""
	set TheReceiver to "" -- the receiver 
	set theSignatureName to "" -- the signature name
	set theAttachment to theItem
		
	tell application "Mail"
		activate
		
		set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theContent & return & return, visible:true}
		tell theNewMessage
			make new to recipient at end of to recipients with properties {address:TheReceiver}
			repeat with theItem in fileList
				set contents of theItem to (get POSIX path of (contents of theItem))
				make new attachment at end of paragraphs with properties {file name:theItem}
			end repeat
		end tell
		
		
	end tell
	
end tell

Thanks upfront.

Bruno

Difficult to say without knowing what those "formatting issues" are. If it's "white space at the top of the message body", you are starting your message content with two returns before adding the inline attachments.

Have you tried just setting your content during message creation? You can use rich text at that stage too.

I'm also guessing you've missed some stuff out -- you seem to be telling Mail inside a tell application "Mail" block, setting theAttachment to a non-existent variable, referencing a fileList that isn't there... Knowing only half the story makes replicating and troubleshooting a lot more difficult.

Hi @Nige_S,

Indeed there was missing a line code (the 1st one).

The problem is that the text gets invisible in the dark mode (dark background for messages).

The code does what's expected except the body text that get's invisible.

Hope you get all info needed.

Thanks

tell application "Path Finder"
	set fileList to selection
	set fileList to selection
	if fileList ≠ missing value then
		repeat with theItem in fileList
			set contents of theItem to (get POSIX path of (contents of theItem))
		end repeat
	else
		return 0
	end if
end tell

tell application "Mail"
	activate
	
	set theSubject to "Análise TE [ area ]" -- the subject
	set theContent to "" -- the content
	set TheSender to ""
	set TheReceiver to "" -- the receiver 
	set theSignatureName to "" -- the signature name
	set theAttachment to theItem
		
	tell application "Mail"
		activate
		
		set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theContent & return & return, visible:true}
		tell theNewMessage
			make new to recipient at end of to recipients with properties {address:TheReceiver}
			repeat with theItem in fileList
				set contents of theItem to (get POSIX path of (contents of theItem))
				make new attachment at end of paragraphs with properties {file name:theItem}
			end repeat
		end tell
		
		
	end tell
	
end tell

And how does it look in Light (also known as "Sensible" :wink: ) mode?

My complete guess is that you are defining a dark font colour in your RTF files, but you aren't setting a background colour. So everything's behaving just as you told it to, not knowing that you've decided to use a dark background in your Mail window.

Difficult to be sure without seeing the actual files you're inserting. But easy for you to test -- set the font colour in your RTFs to yellow and see what happens.

in light mode it works fine. I've tried to change the font colour in Mail preferences, as suggested, but it doesn't work. Somehow when running the code the text format changes.

Who suggested that?

You are inserting Rich Text Content into the body of your Mail message. That Rich Text Content has probably (you've not shown us it) defined text colour to be "black" or something similar. So you are putting black text on a black display background (note that the blackness of your Mail window is not a property of the Mail message itself).

If you are going to define the colour of a thing, you have to define the colours of all the things that might be effected by that choice. So if you force a text colour you should also force an appropriate text background (only behind text characters) or page (the whole document) colour.

Hi @Nige_S
I'm not an expert. I understood wrongly what you said. Don't bother more with this I will use the light version of Mail.

Thanks for your time,
Bruno

That could make things worse! Now you can see the text -- but if you send it to someone who is using Dark Mode they might not see anything!

It's my fault for not explaining properly. But I'm also having trouble replicating your problem. Looking at your code it appears that you are adding one or more inline attachments to an otherwise blank email -- whenever I do that it shows the icon of the attachment, there's no text to be invisible.

Perhaps we're using different versions of Mail. What version of macOS are you on? What format are the attachments?

MacOS Monterey 12.6 (21G115)
Mail Version 16.0 (3696.120.41.1.1)

Same as my test machine.

If I use your script and point it towards a text document the attachment shows as an icon and correctly recolours the icon's text label when moving between Light and Dark mode, or in Dark mode and changing between dark and light window backgrounds.

Are you instead attaching an image file? I could see that might cause problems if the image had dark text and a transparent background. Perhaps you could post one of your problem attachments, either here or in a DM to me.

It certainly seems to be the attachment that's the issue rather than KM, your script, or Mail.

yes. I think your right. the message creation seems ok (body text is white). When adding the attachment at the end it change the color of the body text!

The attachment are excel files .xlsx but I've tried also with .pdf and occurs the same.

Hi @Nige_S
I found this:
https://discussions.apple.com/thread/252375786

As you said I have to set the colour text.

I couldn't reach to this w/out your precious help. Thanks for your time.

I leave the script for someone that my interest.

tell application "Path Finder"
	set fileList to selection
	if fileList ≠ missing value then
		repeat with theItem in fileList
			set contents of theItem to (get POSIX path of (contents of theItem))
		end repeat
	else
		return 0
	end if
end tell

tell application "Mail"
	activate
	
	set theSubject to "" -- the subject
	set theContent to "" -- the content
	set TheSender to ""
	set TheReceiver to "" -- the receiver 
	set theSignatureName to "" -- the signature name
	set theAttachment to theItem
	set subjectline to ""
	
	tell application "Mail"
		activate
		
		set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theContent & return & return, visible:true}
		set color of words of theNewMessage to {224, 255, 255} -- White text
		tell theNewMessage
			make new to recipient at end of to recipients with properties {address:TheReceiver}
			repeat with theItem in fileList
				set contents of theItem to (get POSIX path of (contents of theItem))
				make new attachment at end of paragraphs with properties {file name:theItem}
			end repeat
		end tell
		
		
	end tell
	
end tell


Bruno

2 Likes

Nice find. I've been trying to find ways of doing it without forcing a colour, but no luck.

Definitely looks to be a bug when working in Dark Mode. You can make the email in Light Mode and switch to Dark and the text behaves appropriately. You can even be in Dark Mode but with Mail set to use white backgrounds when you make the message and it's still OK when you switch to dark backgrounds! It's only when you are in Dark Mode with a dark background and add the attachment via AppleScript that the problem occurs...

Setting the text to white (and I suggest you do make it white -- you've got 254 as the first number) is a bit of a lie -- when I send the email to myself and look at the source in Outlook, the words are color="#010101" while spaces are color="#ffffff"! But the good news is that the email seems to honour the recipient's display settings -- for me it was reversing colours when switching from Dark to Light and back so was always readable.

Mail and its "let's compose in RTF then translate to HTML to send" can really be a confusing mess -- congratulations on getting as far as you have.

2 Likes

Hey Bruno,

What version of macOS are you using?

-Chris

Hi Chris,

MacOS Monterey 12.6 (21G115)
Mail Version 16.0 (3696.120.41.1.1)

Bruno