Ah, this is such a relief! I have spent hours trying to access the content of a newly created email message, assuming I couldn’t do it due to my crappy scripting, but it turns out it can’t be done. Thank you Mockman and robertfern.
I got it all working now.
Two problems I ran into:
First, it takes Mail some time (whole minutes in some cases) to put a new message into the Drafts folder, but thanks to your example script I knew how to force Mail to save it using Command-S.
Second: the formatted text got pasted into the address field of the email message, but again that was solved by four consequetive keystrokes of TAB.
The same trick could also be used to quit TextEdit after it had done its job.
Plus I finally learned how these repeat loops work. Indeed, it is a brute force approach and a lot happens on screen in a few seconds, but it gets the job done and I learned a lot. Thanks!
For the record, I include the final version.
‘’’
– VARIABLES – Phrases that must be formatted
set makeBold to {“Important data”, “Pay attention”, “Please reply”}
set makeRed to “tbc”
– VARIABLES – Formats to be used
set boldType to “Calibri-Bold”
set redColor to {65535, 0, 0} – RGB colour Red
– Save new email message to Drafts
tell application “Mail” to activate
delay 0.3
tell application “System Events” to tell process “Mail”
key code 1 using command down – command-s or Save
delay 0.3
end tell
– Get text from email message in front window
tell application “Mail”
set accountId to id of account “iCloud”
set draftBox to mailbox “Drafts” of account id accountId – drafts of account
set theMessage to message 1 of draftBox – most recent draft
set messageContent to content of theMessage – content of most recent draft message
end tell
– Paste text into new TextEdit document
tell application “TextEdit”
activate
make new document at the front
set the text of the front document to messageContent
set doc1 to document 1
set text of doc1 to messageContent -- use text of mail message for textedit document
set font of text of doc1 to "Calibri"
set size of text of doc1 to 15
-- Set indicated phrases to Bold
repeat with p from 1 to count of paragraphs of doc1
repeat with k from 1 to length of makeBold
if paragraph p of doc1 begins with item k of makeBold then
set font of paragraph p of doc1 to boldType
end if
end repeat
end repeat
-- Set indicated word to Red
if text of doc1 contains makeRed then
set w to words of text of doc1
repeat with x from 1 to length of w
if item x of w is makeRed then
set color of word x of text of doc1 to redColor
set font of word x of text of doc1 to boldType -- optional
end if
end repeat
end if
delay 1
--Copy formatted text from TextEdit document
tell application "System Events" to tell process "TextEdit"
key code 0 using command down -- command-a or Select All
delay 0.3
key code 8 using command down -- command-c or Copy
end tell
end tell
– Paste formatted text into email message
tell application “Mail”
activate
delay 0.3
tell application “System Events” to tell process “Mail”
key code 48 – 4 x TAB to get to the text field
key code 48
key code 48
key code 48
key code 0 using command down – command-a or Select All
–delay 0.3
key code 9 using command down – command-v or Paste
end tell
end tell
– Quit TextEdit
tell application “TextEdit” to activate
delay 0.3
tell application “System Events” to tell process “TextEdit”
key code 12 using command down – command-q or Quit
delay 1
key code 51 using command down – command-delete or Don’t Save
delay 0.3
end tell
– Back to email-message for last edit
tell application “Mail” to activate
‘’’