How to set the sender of the current Mail.app message through AppleScript?

I would like to write AppleScript to set the sender of the current outgoing message to Apple Mail.app.

I tried this:

tell application "Mail" to set sender of front outgoing message to "<my email address>" 

but I get the error message error "Mail got an error: Can't set sender of item to any." number -10006 from sender of item to any error "Mail got an error: Can't set sender of item to any." number -10006 from sender of item to any , which does not make sense to me.

When I try to interrogate the front outgoing message as follows:

 tell application "Mail" to get properties of front outgoing message 

I get {class:item} in return, instead of the "outgoing message" object, as I expected.

Any ideas?

+6
source share
7 answers

Unfortunately, you cannot get or set the properties of an outgoing message Mail object with Applescript.

Instead, you can accomplish this using the graphical GUI, a workaround that directly controls window elements.

This code should work for you:

 tell application "System Events" tell process "Mail" click pop up button 1 of window 1 click menu item 6 of menu 1 of pop up button 1 of window 1 end tell end tell 

Change menu item 6 on the fourth line to any number in the list where your desired sender is located (for example, if the sender you want to change using this script is fourth, change menu item 6 to menu item 4 ).


Update: If you are interested, since this answer is older than two years: as of 2014-04-26, receiving / setting the outgoing message properties is still not possible, and this workaround still works in OS X 10.9 Mavericks.

+4
source

More than a little disappointing, you cannot set the sender of the outgoing message.

Here is a more general version of the Matthew script that I wrote many years ago and often used. An argument is a string containing exactly what you see in the From: pop-up window.

 "Mitchell L Model < dev@software-conce pts.org>" 

(It is very difficult to get information about something like this if you are not doing a lot of GUI).

 to setFrom(address) tell application "System Events" activate application "Mail" tell pop up button "From:" of window 1 of application process "Mail" click tell menu item address of menu 1 to click end tell end tell end setFrom 

The reason I use the parameterized handler is because I use the keystroke macros to bind a key combination for each of my email addresses. Each of them runs AppleScript, which downloads a file containing the aforementioned handler and invokes it with a specific email address. For instance:

 property util : load script alias (((path to home folder) as text) & "Scripts:Utilities.scpt") util setFrom("Mitchell L Model < dev@software-concepts.org >") 
+3
source

In fact, you can set the sender of the outgoing message, as described here: Incorrect sender when sending email through Applescript

+2
source

I think this is a little trickier. My own experiments are consistent with Alan Kimelman's conclusion on this topic that AppleScript works as expected, outgoing messages created from scratch using a script . For example, the following code works:

 tell application "Mail" set newMessage to (a reference to (make new outgoing message)) tell newMessage make new to recipient at beginning of to recipients Β¬ with properties {address:" hello@world.com ", name:"The World"} set the sender to "Jerry Krinock < jerry@sheepsystems.com >" set the subject to "A Test" set the content to "This is only a test." send end tell end tell 

However, if the message was created in other ways (for example, I create HTML messages by notifying Safari about Share via email), then Matthew McVickar is right that AppleScript is broken. You cannot set or get any properties, or refuse the send command.

+1
source

It is trivial to use AppleScript to create a new outgoing message with any outgoing address that you like (well, at least those configured in Mail.app settings).

The discussion here is about trying (incorrectly) to change it after creating a post. Instead, indicate this when creating the message:

 set new_m to make new outgoing message with properties {sender:"My Name < me@my.com >"} 

The text in quotation marks must match both the real name and the email address (in chevrons) of any configured account. If there is no match, Mail.app will use the default address.

+1
source

Try this instead.

 tell application "Mail" to make new outgoing message with properties {sender:"<your_email_address>", visible:true} 

UPDATE:. I would look into the dictionary of writing scripts and see what you can and cannot do. The information provided there may help. :)

0
source

An alternative GUI script example that I provided would be an Automator script that uses the "Watch Me Do" command. I don’t quite understand how this works under the hood, but it supposedly records the movement of the mouse and the interaction with the graphical interface, and then reproduces what you recorded when it works. Unfortunately, I experienced a significant lag when running the script. After he asked him to run, he sometimes ran after 5 or more seconds, which is clearly unsuitable.

The GUI script is almost instant and cleaner (no mouse movement) to load. I recommend it.

0
source

Source: https://habr.com/ru/post/892418/


All Articles