How to automatically add the first name of an email recipient

Hi MacScripter members!

I’m new to AppleScript and I’m blocking with an existing script that I’m trying to use.

Some context:

I want to automatically add the first name of an email recipient to the body of an email and I’ve found a post from MacSparky here: http://macsparky.com/blog/2015/6/automatically-add-recipients-name-to-email-with-textexapnder to do just that, but, for some reason it’s not working on my system.

The AppleScript code is the following:

tell application "System Events"
    tell process "Mail"
        tell text field "To:" of window 1
            if UI element 1 exists then
                set theToRecipient to (value of UI element 1)
                if (count words of theToRecipient) is greater than 0 then return word 1 of theToRecipient
            end if
        end tell
    end tell
end tell

I’m using the app “Editeur de script” to test this script, (I’m french but I guess it’s “Script Editor” in english), and, my system is in French.

Maybe that’s the problem and I should replace the “To:” on the third line by “À :” which is what I see on the “To:” line in French?

Thank for your help and advices, if you need more details, let me know!

Cheers,

Pierre

Model: iMac
AppleScript: 2.4
Browser: Safari 601.1.56
Operating System: Mac OS X (10.10)

Hey Pierre,

You hit it.

Your script uses System Events and a labeled field. Since the label is French SEV will not understand the English one.

You can reassure yourself of the labels by running this:

tell application "System Events"
	tell process "Mail"
		name of text fields of window 1
	end tell
end tell


Chris


MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.11

Bonjour Pierrodu21

When we use GUI Scripting, it’s often useful to extract the localized strings from the applications resources as I do below.

tell application "Mail" to set TO_loc to localized string "ToIndicator"
log result (*À*)

tell application "System Events"
	tell process "Mail"
		set frontmost to true
		tell window 1
			set ToName to (get name of first text field whose name starts with TO_loc)
			log result (*À :*)
		end tell
		tell text field ToName of window 1
			if UI element 1 exists then
				set theToRecipient to (value of UI element 1)
				if (count words of theToRecipient) is greater than 0 then return word 1 of theToRecipient
			end if
		end tell
	end tell
end tell

The first instruction
tell application “Mail” to set TO_loc to localized string “ToIndicator”
extracts a localized string whose value is “TO” in English but is “À” in French.
As things aren’t simple in GUI Scripting area, we can’t build the name of the text field by ourselves because in English there is no space between the name and the trailing colon but there is one in French.
This is why I extract the localized name of the text field with :
set ToName to (get name of first text field whose name starts with TO_loc)
When it’s done we may use the true name to trigger the text item.

Yvan KOENIG running El Capitan 10.11.0 in French (VALLAURIS, France) mardi 20 octobre 2015 09:52:54

Hi Chris, hi Yvan!

Thank you both very much, it’s working now! :slight_smile:

I’m glad I was on the correct path but I didn’t know where to find the labels…

Yvan, two additional questions if possible:

  • I guess that now, thanks to “TO_loc to localized string “ToIndicator”” this script can works with every languages?
  • and, second question, where do you fin the “TO_loc”? For instance, if I want to find the localization of the “Cc” field, is it “CC_loc”?

Anyway, i’m really happy!

Cheers,

Pierre