Jump to content

Create new email with snippet text in body / content

Recommended Posts

I want to run an Alfred command and it generate a new email for me, including text that I've already saved in a snippet. I run a martial arts club and when we get new members trying out I need to send them all the new joiner information after the session. So, the content is always the same, and the people are never in my contacts. I have got it working using a workflow as follows, which uses my snippet "mawelcome" to populate the body of the email: 


A keyword run a Script /usr/bin/oascript 


tell application "AirMail 3" 
       set newMail to make new outgoing message with properties {subject: "Joining our martial arts club", sender:"my.email@myemail.com"}
    compose newMail
end tell
tell application "System Events"
    keystroke "mawelcome"
end tell


My question: is there a better way? I couldn't find anything that really described doing this, but I can imagine a lot of people want to do it, which made me think I was missing something obvious to approach it another way! 


Thank you!

Link to comment

Hi Jemima, welcome to the forum!


Using System Events to simulate keystrokes is rather hacky (and a last resort of automation), especially as AirMail's API already supports setting the text of the new message.


The issue is that you can't really control when and where the keystrokes go. You just have to fire them off and hope the window you're trying to type in actually has focus. If your machine is under load and AirMail doesn't create the new window immediately—or if another window/dialog pops up and grabs focus—the keypresses will be sent to the wrong window.


The obvious question is: why don't you put the mail body text in the script instead of a snippet? Presumably, you aren't actually using the snippet as a snippet because you've written this script to automate the situations where you'd use it.

Edited by deanishe
Link to comment

Hi, thanks for your reply, and for the explanation of why it's not a great solution. 


I tried putting the mail body text in the script, but it lost all the carriage returns. It sounds like I should investigate further down that route. (I did also have in mind to use it as a snippet separately, but could live without that for this to work smoothly.) 

Link to comment
2 minutes ago, deanishe said:

Sounds like an Airmail bug, tbh.


No, I think it was a user not reading enough documentation bug :-) It works fine if I put <br>s in the text. (Very much my first attempts with these sorts of scripts.)


On reading further, I also discovered that AirMail has introduced templates since I last looked, so I think there are at least three ways to approach this, which I'll put here in case anyone else is trying to do the same thing. 


1 - use an Alfred snippet (just open a new email in your email package and type in the snippet). You could have one snippet for the subject, and another for the body. 

2 - use a template in AirMail 3 (These can be called using keyboard shortcuts, as well as point and click, and can include attachments and subject lines and are very easy to make!). This is probably what I'll do. I couldn't find a way to generate an email using a Template from Alfred, although that would probably be my ideal as it would save a few keystrokes. 

3 - create a workflow along the lines discussed. When I do this I get an extra line break at the start of the body of the email, perhaps associated with the sig file, but I've not investigated further. This would look a bit like this:


tell application "AirMail 3" 

      set myContent to "Hi<br>


Thanks for your interest in our amazing club... etc. etc.

       set newMail to make new outgoing message with properties {subject: "Joining our martial arts club", sender:"my.email@myemail.com", content: myContent}
    compose newMail
end tell


Thanks very much for your help, deanishe. 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...