You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can I control Mail Message background color from Applescript?

In Mail, if you use the built-in Rules, you can change a messages "background color" to "other" and provide any value you want using Rules. In the Rules you select a color from the color wheel to define "other".


Using Applescript to drive Mail, you can change the background color using:


background color (blue/‌gray/‌green/‌none/‌orange/‌other/‌purple/‌red/‌yellow) : The background color of the message


Which seems straightforward and works with the predefined colors. But I'm having difficulty with setting a custom color using "other"


For example


set background color of msg to other

generates an error


And other is a constant that can't be changed.

set other to "#FF0000"
Can’t set «constant qqclccot» to "#FF0000". Access not allowed.


So maybe it's a hash key??


Trying various list formats also fails (other:"#ff0000"), {other:"#ff0000"}, etc.


What I'm looking for is something like:


set background color of msg to other:"#ffff00"

or

set background color of msg to "#ffff00"


Does anyone know how to assign a custom background color from Applescript to a message in Mail?





MacBook Pro

Posted on Aug 14, 2022 12:36 PM

Reply
Question marked as Top-ranking reply

Posted on Aug 16, 2022 10:44 AM

The correct syntax, which still introduces a -10000 AppleEvent handler failed message is:


use scripting additions

tell application "Mail"
	if it is not running then activate
	if not (get selection) is {} then
		set thisMsg to item 1 of (get selection)
		tell thisMsg
            -- the other enumerator forces Mail to open a color chooser dialog
			set its background color to other
		end tell
	else
		return
	end if
end tell
return


When it is in the proper mood, this script will open a color chooser panel when it detects the other enumerator key word and will use your choice of custom color once set on that color panel. You cannot assign a color directly to background color as it expects an enumerator constant from the color panel, not from you.

9 replies
Question marked as Top-ranking reply

Aug 16, 2022 10:44 AM in response to kurta

The correct syntax, which still introduces a -10000 AppleEvent handler failed message is:


use scripting additions

tell application "Mail"
	if it is not running then activate
	if not (get selection) is {} then
		set thisMsg to item 1 of (get selection)
		tell thisMsg
            -- the other enumerator forces Mail to open a color chooser dialog
			set its background color to other
		end tell
	else
		return
	end if
end tell
return


When it is in the proper mood, this script will open a color chooser panel when it detects the other enumerator key word and will use your choice of custom color once set on that color panel. You cannot assign a color directly to background color as it expects an enumerator constant from the color panel, not from you.

Aug 15, 2022 6:13 AM in response to kurta

Ibid, Urguhart1244.


Here is some AppleScript that I rescued from MacScripter days that allows you to pick a color on the color chooser and convert that to 16-bit RGB and hex color codes, that are presented in a dialog.


-- StefanK: http://macscripter.net/viewtopic.php?pid=95047

property hexList : "0123456789ABCDEF"


set defaultHex to "#FFFFFF"
set r to ""
set g to ""
set b to ""

if defaultHex starts with "#" then
	try
		set chosenColor to choose color default color Hex8toRGB16(defaultHex)
		-- log chosenColor
		set {r, g, b} to items of chosenColor
		set rgb16Value to "{" & r & ", " & g & ", " & b & "}"
	on error
		return
	end try
	set hexValue to RBG16toHex8(chosenColor)
	log hexValue
	
end if

display dialog "RGB 16: " & tab & rgb16Value & return & "     Hex:" & tab & "#" & hexValue
return

to RBG16toHex8(AppleRGB16)
	set hexString to ""
	repeat with primary in AppleRGB16
		set hexString to hexString & character (primary div 4096 + 1) of hexList & character (primary div 256 mod 16 + 1) of hexList
	end repeat
	return hexString
end RBG16toHex8
--
to Hex8toRGB16(htmColor) -- format #xxxxxx
	set RGB16 to {}
	repeat with i from 2 to 6 by 2
		set end of RGB16 to ((((offset of htmColor's character i in hexList) - 1) * 16) + (offset of htmColor's character (i + 1) in hexList) - 1) * 257
	end repeat
	return RGB16
end Hex8toRGB16


Aug 16, 2022 8:43 AM in response to Urquhart1244

Here is some AppleScript that I rescued from MacScripter days that allows you to pick a color on the color chooser and convert that to 16-bit RGB and hex color codes, that are presented in a dialog.


Urquhart1244 wrote:

That works very well, thanks!



How does it work to select a custom background color for Mail?


** confused **

Aug 16, 2022 3:08 PM in response to VikingOSX

In other words: "It's broken".


Thanks so much for the very detailed answer and explanation.


I guess my computer isn't "in the mood", since all I get is the


-10000 AppleEvent handler failed message


Don't understand why Apple made this reliant on "constants" instead of simply assigning values to default colors and allowing users to pass values for their OWN colors.


Not the answer I was HOPING for, but at least now I know that it's APPLE'S failure and not my own.


Thanks again!

How can I control Mail Message background color from Applescript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.