Create an email using Mail's stationery

This handler allows you to apply stationery to the front message in Mail. It will work in 10.5, 10.6 or 10.7. Note that if the email message has text then that text is automatically applied to the stationery. Therefore, you could create an applescript workflow to generate the email message and apply some stationery for a one-stop solution to create an awesome email.

In this example, open a new message in Mail (add some text to it if you like) and run this code. It will apply the “Gift” stationery from the “Birthday” category.

-- this will select a stationery for the front Mail window
-- to use you must first create a new email message in Mail
-- if you add text and images to the new mail message first, then when a template is chosen that information is automatically put into the template

set stationeryCategory to "Birthday"
set stationeryName to "Gift"
chooseMailStationery(stationeryCategory, stationeryName)


on chooseMailStationery(stationeryCategory, stationeryName)
	-- this requires "access for assistive devices" to be checked in the "Universal Access" system preference pane
	-- this will work on MacOSX 10.5 through 10.7
	-- stationeryCategory is the category name containing the stationery to choose
	-- stationeryName  is the name of the stationery to choose, set it to false if you want a random stationery to be chosen
	
	set shortDelay to 0.2
	
	-- determine the OS version and set the proper section numbers accordingly
	set sysVersion to the system version of (system info)
	if sysVersion begins with "10.5" then
		set categorySectionNumber to 5
		set templatesSectionNumber to 6
	else -- 10.6 or 10.7
		set categorySectionNumber to 4
		set templatesSectionNumber to 5
	end if
	
	tell application "Mail" to activate
	delay shortDelay
	tell application "System Events"
		tell process "Mail"
			tell window 1
				-- show the stationery
				if exists button "Show Stationery" of tool bar 1 then click button "Show Stationery" of tool bar 1
				delay shortDelay
				
				-- select the stationeryCategory
				set categorySection to table 1 of scroll area categorySectionNumber
				set categoryList to the value of static text of every row of categorySection
				repeat with i from 1 to the count of categoryList
					set thisCategory to item 1 of (item i of categoryList)
					if thisCategory is stationeryCategory then exit repeat
				end repeat
				select row i of categorySection
				delay shortDelay
				
				-- select the stationeryName
				set templatesSection to radio group 1 of scroll area templatesSectionNumber
				if stationeryName is false then -- we pick a random template
					set templatesButtons to name of buttons of templatesSection
					set templateButton to some item of templatesButtons
				else
					set templateButton to stationeryName
				end if
				click button templateButton of templatesSection
				delay shortDelay
				
				-- hide the stationery
				if exists button "Hide Stationery" of tool bar 1 then click button "Hide Stationery" of tool bar 1
			end tell
		end tell
	end tell
end chooseMailStationery

I’ve tried this script and can’t get it to work.
I’m on Lion OS 10.7.2 and when looking at the mail message interface quite a few things are not what the script uses:

No longer radio button for templates but rows in a table, scroll area for this is 6 when the script makes it 5, (could manage these) but more difficult is that there are no refs to buttons to pick the templates…(UI browser just can’t identify what’s in this area).

And I end up with this error message:

error “Erreur dans System Events : Il est impossible d’obtenir button "Adret-envoi2010_07" of table 1 of scroll area 6 of window "Nouveau message" of application process "Mail".” number -1728 from button “Adret-envoi2010_07” of table 1 of scroll area 6 of window “Nouveau message” of application process “Mail”

http://www.flickr.com/photos/33469248@N05/6335184286/

This still works for me running 10.7.3 and Mail version 5.2. Note that the script tells you that you must first open a new email message before running the script. If you don’t do that then errors occur.

Reworked for Mavericks (10.9.5)

For Mavericks Mail, I had to rework the original script above to identify the button, scroll area and stationery button, because mail’s message editing window uses graphic buttons and the category list is different as well. Showing and hiding required getting the actual button object, rather than referencing it by name, because the name is empty since Apple changed Mail to use graphic buttons. (The addresses have been changed to protect the not-so-innocent.;))

set shortDelay to 0.2
set longDelay to 0.5
set cat to "Custom"
set snm to "My Custom Stationery"

-- Set global scope:
set msg to ""

-- Create the new message and add recipients:
tell application "Mail"
	activate
	delay shortDelay
	
	--set sgs to the name of every signature of application "Mail"
	make new outgoing message with properties {visible:true, subject:"My Subject", content:""}
	set msg to the result
	tell msg
		set sender to "Joe Sender<joe.sender@sender-domain.com>"
		-- DISABLED: Stationery already includes recipient.
		--make new to recipient at end of to recipients with properties {name:"Jim Recipient", address:"jim.recipient@recipient-domain.com"}
		--set message signature of msg to signature "Signature #1" of application "Mail"
	end tell
end tell

-- Set the Stationery:
tell application "System Events"
	tell process "Mail"
		set wnd to window 1
		tell wnd
			-- Select the signature. (DISABLED: our stationery already includes signature.)
			--if exists pop up button "Signature:" then
			--	tell pop up button "Signature:"
			--		perform action "AXPress"
			--		delay longDelay
			--		click menu item "Signature #1" of menu 1
			--	end tell
			--end if
						
			if exists toolbar 1 then
				set tbr to toolbar 1
				
				-- Show the stationery selection area.
				repeat with i from 1 to the count of buttons of toolbar 1
					set btn to button i of toolbar 1
					if the description of btn is "Show Stationery" then
						click btn
					end if
				end repeat
				delay shortDelay
				
				-- Select the Custom category
				if exists scroll area 2 of and then
                                        -- I really hate this hard-coding of object numbers.
                                        -- Every time mail is upgraded, our script might break.
                                        -- Need to find a way to at least allow for minor changes, such as sequence number changes.
					set cts to the name of static text of every row of table 1 of scroll area 2 of wnd
					repeat with j from 1 to count of items of cts
						set opt to item j of cts
						if item 1 of opt is "Custom" then
							select row j of table 1 of scroll area 2 of wnd
							delay longDelay
							
							-- Select my custom stationery within the category.
							if exists scroll area 3 of and then
                                                                -- Hard coded radio group #, since there's nothing else I can identify it by.
                                                                -- Maybe it's less error prone if I search all the radio groups for the
                                                                -- specific stationery button name I want, but I'll get to that later.
								set grp to radio group 1 of scroll area 3 of wnd
								click button snm of grp
								delay longDelay
							end if
						end if
					end repeat
				end if
				
                                -- Hide the stationery selection area.
				click btn
				delay shortDelay
			else
				display dialog "No toolbar 1 found."
			end if
		end tell
	end tell
end tell

Note: Ignore the Mac OS version below. I’m on 10.9.5, but the site’s OS selection doesn’t yet go past 10.8.

Model: MacBook Air
AppleScript: 2.3.2
Browser: Safari 537.85.11
Operating System: Mac OS X (10.8)