In order to answer your question, please post your macro.
See
Sorry â I thought the previous response wouldâve been attached to my query.
Below is the script that I attempted to add the time received to, with faulty resultsâŚ
â
Tom <https://forum.keyboardmaestro.com/users/tom>
January 28
You can get the cc recipients and the bcc recipients in exactly the same way as the to recipients.
Get the list of all the recipients
Copy the list as formatted text to a variable
Integrate the variable into your header element
Here is a heavily commented form of the complete script, explaining each step:
tell application "Mail"
# Get the first selected message
set selMsg to the selection
try
set theMsg to first item of selMsg
on error
display alert "Probably No Message Selected!"
error number -128
end try
# Get the desired header elements
# Elements containing only one item
# Sender
set theSender to the sender of theMsg
# Subject
set theSubject to subject of theMsg
# Date received
set theDate to date string of (get date received of theMsg)
# Elements containing multiple items (recipients)
# Normal (âToâ) Recipients
set toAddresses to {}
repeat with i in to recipients of theMsg
set end of toAddresses to address of i
end repeat
# CC Recipients
set ccAddresses to {}
repeat with i in cc recipients of theMsg
set end of ccAddresses to address of i
end repeat
# BCC Recipients
set bccAddresses to {}
repeat with i in bcc recipients of theMsg
set end of bccAddresses to address of i
end repeat
# Format the recipients
# Save current text item delimiters
set saveTID to AppleScript's text item delimiters
# Set the desired delimiters for the representation, e.g. {", "}, {"; "}, {" â "}, {" | "}
set AppleScript's text item delimiters to {", "}
# Format the recipients lists as delimited text
set toRecipients to toAddresses as rich text
set ccRecipients to ccAddresses as rich text
set bccRecipients to bccAddresses as rich text
# Restore text item delimiters
set AppleScript's text item delimiters to saveTID
# Compose the header
set theHeader to "From: " & theSender & linefeed & "To: " & toRecipients & linefeed & "CC: " & ccRecipients & linefeed & "BCC: " & bccRecipients & linefeed & "Subject: " & theSubject & linefeed & "Date received: " & theDate
# Get the content
set theContent to the content of theMsg as rich text
# Compose complete message (header + content)
set finalMsg to theHeader & linefeed & linefeed & theContent
# Transfer it to the clipboard
set the clipboard to finalMsg
end tell
It doesnât matter how you name the variable. The important things is to set it.
You can do it in an analogous way as the theDate
variable is set in line 20 of the script:
set theDate to date string of (get date received of theMsg)
So, add this:
set theTime to time string of (get date received of theMsg)
And then âthe same as with the dateâ add the theTime
variable to the theHeader
variable, which will produce the output (line 52, marked with â# Compose the headerâ in the script).
For example, to add the time at the end:
set theHeader to "From: " & theSender & linefeed & "To: " & toRecipients & linefeed & "CC: " & ccRecipients & linefeed & "BCC: " & bccRecipients & linefeed & "Subject: " & theSubject & linefeed & "Date received: " & theDate & linefeed & "Time received: " & theTime
This will produce a header like this (example):
From: xxx xxx xxx.xxx@xxx.xxx
To: bbedit@googlegroups.com
CC:
BCC:
Subject: Manipulating buffer in window with AppleScript?
Date received: Wednesday, 12. April 2017
Time received: 00:38:45â
â
Or, if you prefer to have date and time in one line:
set theHeader to "From: " & theSender & linefeed & "To: " & toRecipients & linefeed & "CC: " & ccRecipients & linefeed & "BCC: " & bccRecipients & linefeed & "Subject: " & theSubject & linefeed & "Received: " & theDate & "; " & theTime
which will produce this:
From: xxx xxx xxx.xxx@xxx.xxx
To: bbedit@googlegroups.com
CC:
BCC:
Subject: Manipulating buffer in window with AppleScript?
Received: Wednesday, 12. April 2017; 00:38:45
Probably you have these problems because you are posting from your mail client. If you have access to a web browser, itâs better to communicate with the forum via web browser.
The mail notifications are fine to get notified about new posts/topics, but not so much to post or to respond to posts. As you see, via mail the format gets messed up (quoted text, code blocks, inline code, etc.) and apparently you donât have much control over your post.
Besides that you wonât see when a post has been updated or edited, I think.
HmmâŚ
I tried to introduce the changes to the script (below), and apparently something is amiss.
I was also wondering if there's a way to not put "CC" or "BCC" if those fields are empty in the received e-mail.
NextâŚI'll be wanting a script that writes a reply!
Thank you for your expertise!
Paul
tell application "Mail"
# Get the first selected message
set selMsg to the selection
try
set theMsg to first item of selMsg
on error
display alert "Probably No Message Selected!"
error number -128
end try
# Get the desired header elements
# Elements containing only one item
# Sender
set theSender to the sender of theMsg
# Subject
set theSubject to subject of theMsg
# Date received
set theDate to date string of (get date received of theMsg)
# Time received
set theTime to time string of (get time received of theMsg)
# Elements containing multiple items (recipients)
# Normal (âToâ) Recipients
set toAddresses to {}
repeat with i in to recipients of theMsg
set end of toAddresses to address of i
end repeat
# CC Recipients
set ccAddresses to {}
repeat with i in cc recipients of theMsg
set end of ccAddresses to address of i
end repeat
# BCC Recipients
set bccAddresses to {}
repeat with i in bcc recipients of theMsg
set end of bccAddresses to address of i
end repeat
# Format the recipients
# Save current text item delimiters
set saveTID to AppleScript's text item delimiters
# Set the desired delimiters for the representation, e.g. {", "}, {"; "}, {" â "}, {" | "}
set AppleScript's text item delimiters to {", "}
# Format the recipients lists as delimited text
set toRecipients to toAddresses as rich text
set ccRecipients to ccAddresses as rich text
set bccRecipients to bccAddresses as rich text
# Restore text item delimiters
set AppleScript's text item delimiters to saveTID
# Compose the header
set theHeader to "From: " & theSender & linefeed & "To: " & toRecipients & linefeed & "CC: " & ccRecipients & linefeed & "BCC: " & bccRecipients & linefeed & "Subject: " & theSubject & linefeed & "Date received: " & theDate & " Time: " & theTime
# Get the content
set theContent to the content of theMsg as rich text
# Compose complete message (header + content)
set finalMsg to theHeader & linefeed & linefeed & theContent
# Transfer it to the clipboard
set the clipboard to finalMsg
end tell
The script as you have quoted it stops halfway at ~line 56, that is, the last part of the script is missing. Of course, it wonât work like this.
Please use the whole script code, as posted in the earlier post, and add your changes. When copying from the post make sure to copy the whole script code, from line 1 to the last line.
Well, I (or somebody else) could modify the script to not show any âCCâ or âBCCâ if there arenât any. But the question is: is it useful to provide you with pre-made solutions?
Your âNextâŚI'll be wanting a script that writes a reply!â tells me that you have further ambitions with the script. So, I really think it would be worth that you learn at least some AppleScript basics, which will allow you to build/modify the script yourself. Itâs not necessary that your script works flawlessly. In case of any problems, post here.
Read the scripting dictionary of the Mail app and try to learn enough about AS to build a script that âin theory should workâ. Then post the questions about the missing details here. Or, even when you come across a problem already in the building process, you can ask here.
But, asking for complete, working scripts, without understanding the basics, is something I wonât deliver to. I donât have the time for this, and â as already mentioned â I doubt that it is a good way.
####Edit:
Sorry, if this sounded harsh, but indeed Iâm a bit angry disappointed. Iâve written the script for you (Iâm not using it, nor was I using a similar script before). I even added very explicit comments, to facilitate your understanding of how it works. And you donât even pay attention when copy/pasting the script code, posting incomplete code here, and asking why it doesnât work!?
Sorry, this was harsh again. Iâm calming down nowâŚ
Hello Tom,
I always draft and proof in a WP application, then copy and paste into either e-mail, web response, etcetera. So, naturally, I was alarmed when you wrote that only part of my novice AS revisions made it to my forum entry.
If I were you, I too would be angry/disappointed if somebody kept hitting my favor bank with carelessly composed queries (which, as you know, happens all the time).
[Irony is, I just attempted to edit the post to make sure my entire script was included (which, it turns out, would fail regardless, per the correct code in your response), and it didnât take either.]
I agree: nobody learns anything by being spoon-fed answers. Certainly this is not my method in dealing with clients or students, and I apologize if I come across as some lazy ingrate.
When I wrote âNextâŚIâll be wanting a script that writes a reply!â it was intended to be facetious, playing on the very I-Donât-Want-To-Learn; Just-Give-Me-The-Answer attitude that irks me (and you as well, so it would seem).
Iâll keep hacking away at my imperfect script, the irony being that in pre-Yosemite versions of Mac Mail, you could simply copy and paste a message.
Thank you for your help and forbearance.
PPS
As said, Iâm always happy to help you find errors in your script. So:
I proposed you to add this line:
set theTime to time string of (get date received of theMsg)
Now have quick look at your code and Iâm sure youâll spot the error
You are right, I could have spotted that error already in the first version of your post. But since I saw that a part of the script was completely missing I didnât bother trying to find any errors.
As a side note, the âtell applicationâ (line 1) and the âend tellâ (last line) are an obligatory part of the script code. So, when posting you shouldnât exclude these lines from the code block formatting. Sure, itâs just a formal thing, but chances are good that other users who are going to copy the code block will end up with an incomplete script.
Here a short explanation of your error, or âhow to find out how to get the Received Timeâ:
When I said that the name of a variable doesnât matter, I literally meant âvariableâ. The date received
is not a variable, itâs a property of message
and it is part of Mailâs scripting dictionary.
You cannot simply change the names of these things. (Well, you can, but this would mean working by trial and error.) To see the scripting dictionary of an app, do the following:
- Launch Script Editor (/Applications/Utilities/Script Editor.app)
- Go to File > Open DictionaryâŚ
- Choose the Mail app from the list.
- In the toolbar of the now open window make sure that âAppleScriptâ is selected as language.
- You now see the AppleScript dictionary of Mail
- Go through the dictionary and look for the thing of interest, or use the search field.
- To stay with the example: If you type âreceivedâ into the search field you will only get one hit: âdate receivedâ. This tells us that a property âtime receivedâ doesnât exist in Mailâs dictionary.
That means that we cannot change date received
to âtime receivedâ, because Mail can not understand that. (Itâs not part of its dictionary.)
OK, fine. But how to get the Time?
Common sense tells us that it is very unlikely that there would be no way to get the Received Time. And, since date received
seems to be the only thing related to âreceivedâ, we should suspect now that the Time is somehow part of that date received
property.
Since we already had a working script line with date received
, things are easy here:
The date was obtained with date string of (get date received of theMsg)
. The âofâ tells us that date string
must be a part of date received
, and since âdateâ is a specifier of âstringâ we can assume that there must be other strings besides the date string
.[1]
Unfortunately Script Editorâs dictionary viewer gives us no hint here[2], but in this case a trial and error approach seems promising: VoilĂ , time string of (get date received of theMsg)
will work as expected.
I hope this helps a bit for future adventures
[1] One of the cases where the human-language-like syntax of AppleScript is beneficialâŚ
[2] This is because time string
is a property of the date
class which is part of the AppleScript types suite. I have found no way to display this in Script Editorâs dictionary viewer. One more case where Script Debugger makes life easier: if you type âtimeâ in Script Debuggerâs dictionary viewerâs search field you get âtime stringâ as hit #2.
OK, here is a new version with an optional display of the CC and BCC Recipients:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Mail"
# Get the first selected message
set selMsg to the selection
try
set theMsg to first item of selMsg
on error
display alert "Probably No Message Selected!"
error number -128
end try
# Get the desired header elements
# Elements containing only one item
# Sender
set theSender to the sender of theMsg
# Subject
set theSubject to subject of theMsg
# Date received
set theDate to date string of (get date received of theMsg)
# Time received
set theTime to time string of (get date received of theMsg)
# Elements containing multiple items (recipients)
# Normal (âToâ) Recipients
set toAddresses to {}
repeat with i in to recipients of theMsg
set end of toAddresses to address of i
end repeat
# CC Recipients
set ccAddresses to {}
repeat with i in cc recipients of theMsg
set end of ccAddresses to address of i
end repeat
# BCC Recipients
set bccAddresses to {}
repeat with i in bcc recipients of theMsg
set end of bccAddresses to address of i
end repeat
# Format the recipients
# Save current text item delimiters
set saveTID to AppleScript's text item delimiters
# Set the desired delimiters for the representation, e.g. {", "}, {"; "}, {" â "}, {" | "}
set AppleScript's text item delimiters to {", "}
# Format the recipients lists as delimited text
set toRecipients to toAddresses as rich text
set ccRecipients to ccAddresses as rich text
set bccRecipients to bccAddresses as rich text
# Restore text item delimiters
set AppleScript's text item delimiters to saveTID
# CC and BCC are optional. So letâs precompose the variables to display these two strings, in function of if there are any CC/BCC recipients
if ccRecipients is not "" then
set ccRecipientsDisplay to "CC: " & ccRecipients & linefeed
else
set ccRecipientsDisplay to ""
end if
if bccRecipients is not "" then
set bccRecipientsDisplay to "BCC: " & bccRecipients & linefeed
else
set bccRecipientsDisplay to ""
end if
# Compose the header
set theHeader to "From: " & theSender & linefeed & "To: " & toRecipients & linefeed & ccRecipientsDisplay & bccRecipientsDisplay & "Subject: " & theSubject & linefeed & "Date received: " & theDate & " Time: " & theTime
# Get the content
set theContent to the content of theMsg as rich text
# Compose complete message (header + content)
set finalMsg to theHeader & linefeed & linefeed & theContent
# Transfer it to the clipboard
set the clipboard to finalMsg
end tell
The changes areâŚ
- the new block at lines 56 to 65, and
- a changed header composition (line 68).
See the comment at line 55.
What I have done:
- I introduced a new variable
ccRecipientsDisplay
. - If there are any CC Recipients than this variable is set to the CC Recipients (as saved in the
ccRecipients
variable) plus the formatting elements for the display (the âCC:â and a following linefeed). - If there arenât any CC Recipients than the variable is set to an empty string.
- The composed header (line 68) now uses the precomposed
ccRecipientsDisplay
variable, so that there will be displayed nothing if there isnât any CC.
The same for the BCC Recipients (bccRecipientsDisplay
).
As luck would have it, I finally got around to posting a macro here that may cover your needs. I saw your post subsequently
9 posts were split to a new topic: How to Copy Outlook (2011/2016) Message to Clipboard
Yes, you guys (e.g. @charliez1030) should really post new questions in new threads.
If you have found a thread that is related or otherwise close to your subject, then just link it in the intro, for example:
I wish to do this and that and Iâve seen the thread [link here to the thread]. Based on that idea, how can I implement/adapt that for my [your case here].
This has several advantages:
-
Different subjects/topics are kept apart. For example, as you have learned now, scripting Outlook is different from scripting Apple Mail.
-
âHijackingâ another thread results in misleading thread titles (like here: the thread title is âCopy Mac Mail Messageâ, but the thread now also covers Outlook.)
- Therad titles that correspond to the content make it much more easier to browse the forum.
-
The Discourse forum software allows to mark an answer as âSolutionâ (green checkmark). But this works once per thread. So, looking at the present thread it is impossible to set a solution mark, since the tread covers two different topics/questions and answers. (And, only the OP can set the checkmark, anyway.)
- Here, for example, @JMichaelTXâs script really deserves a âSolutionâ checkmark, but you canât set it, because you are not the original poster of the thread.
Thanks for that. Good idea!
Good point @Tom. I have moved @charliez1030's question and replies to a new topic.
Works for me!
Here we are, almost a decade later. Sequoia. What's next? Poison oak?
Mac Mail has gone through numerous "improvements", rendering some of Tom's and others' excellent and helpful scripts dodgy as far as copying a Mac Mail message with headers to the clipboard while maintaining the formatting of the message.
Any updates to the above script(s) to accommodate for the Mac Mail ersatz "evolution"?
Thank you in advance for your patience,
Paul