Automator: E-Mail generation with variable

Ralfissimo

New member
Joined
May 22, 2022
Messages
2
Reaction score
0
Points
1
Hi,

I'm doing my first steps with Automator, Javascript and Applescript and have the following problem:

I built an Automator Service which does the following:

  • Getting a confirmation if the service really has to run (Automator Action "Confirmation...")
  • Run Java Script to calculate a date (Last Friday of current month) except of July and December
  • Getting the date into Automator variable event_date (Automator Action "Set variable")
  • Run Applescript to show Error message to user if there is no date ("No event this month")
Now I want to do two things, which I does not know, how to do it:
  • Creating a new (invitation) E-Mail message with a predefined Rich Text content.
  • Getting the date into a well defined position of this Mail.

For creating the Rich Text E-Mail, I tried three ways:
  • Automator Action "New E-Mail", Text in Field "Text"
  • Automator Action "Input Text", create a new mail with applescript, containing this text
  • Saving the rich text into an RTF file, reading the content with Applescript and create a new mail with the same applescript, containing the RTF text
    • In this case, I don't want the text editor to open visibly for the user (or at least it should close again immediately)
I always got a plain text E-Mail, all text attributes are lost.

If I get the rich text E-Mail, I have to place the calculated date into that E-Mail text. But I have no idea, how to get the variable (calculated in Javascript) into that text.

I hope there is anyone out there who can help me.

Ralf

PS: I am running Mac OS in German, so I had to translate some Keywords to English (names of automator actions, etc.). I hope, my translations are correct ;)

This is the Javascript to get the last Friday of current month:

JavaScript:
function run(input, parameters) {
// Current date and time
heute=new Date();
// Date (German format)
date=heute.getDate()+"."+(heute.getMonth()+1)+"."+heute.getFullYear();
// determine last day of month
lastDay=new Date(heute.getFullYear(),heute.getMonth()+1,0);
date=lastDay.getDate()+"."+(lastDay.getMonth()+1)+"."+lastDay.getFullYear();
// determine last Frieday
while (lastDay.getDay()!=5){
lastDay.setDate(lastDay.getDate()-1);
}
date=lastDay.getDate()+"."+(lastDay.getMonth()+1)+"."+lastDay.getFullYear();
if (heute.getMonth() === '07') {
// Leave date empty as error message indicator
} else if (heute.getMonth() == '12') {
// Leave date empty as error message indicator
} else {
    return date;
}
}

This is the Applescript to show the error message

AppleScript:
on run {input, parameters}
    if input is {} then
        display dialog "In diesem Monat findet kein SHG-Treffen statt, daher wird keine Mail erzeugt!" buttons {"Abbrechen"} default button {"Abbrechen"}
    end if
end run

And the Applescript to create the Mail from RTF-file:
(Can I place a special string into the RTF-file (like "@date@") to replace it in Applescript with the calculated date?)

AppleScript:
on run {input, parameters}    
    set InvitationDate to input as string
    set MailSubject to {"Invitation"}
    set MailFrom to {"[email protected]"}
    set MailRecipient to {"Mail Test Gruppe"}
    set Mailtext to ((path to desktop as Unicode text) & "Mailtext.rtf")
    
    tell application "TextEdit"
        open file Mailtext
        set TextContent to the text of document 1
    end tell
    
    tell application "Mail"
        set newMessage to make new outgoing message with properties {sender:MailFrom, subject:MailSubject, content:TextContent}        
        tell newMessage
            set visible to true
            set html content to textBody
            make new bcc recipient at end of bcc recipients with properties {address:MailRecipient}
        end tell
    end tell
    return input
end run
 
OP
R

Ralfissimo

New member
Joined
May 22, 2022
Messages
2
Reaction score
0
Points
1
No one here who can help me? :-(((

Problem solved: Inserting of calculated date. I placed @date@ into my mail text and with

AppleScript:
set newText to do shell script "sed 's|" & quoted form of "@date@" & "|" & quoted form of event_date & "|g' <<< " & quoted form of mail_Text

I replace it with the calculated date.

But I need an answer how I can get the text attributes (color, size, etc.) of my rich text into my mail..... The Mail body is reduced to simple text....

Ralf
 
Last edited:

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top