Hello,
The script in question is as follows. It’s rather simple:
tell application "Mail"
set TheSgn to get content of signature YourSignature -- a workaround of extracting the content of a message signature because using directly the class "signature" in further statements brings about an AppleScript handler error. Add any signature of yours in place of YourSignature.
set MyiCloudAccount to the iCloud account named NameOfYouriCloudAccount -- your may set to any other account and rename this variable accordingly.
set TheSenderAddress to set_to_any_sender -- type sender's e-mail address
set MyiCloudMailbox to the first item of (every mailbox of MyiCloudAccount whose name is "INBOX")
set iCloudMsgs to every message of MyiCloudMailbox whose date received is greater than (current date) - 2 * days -- this speeds up the script lifting the burden of going through a multitude of messages by filtering out dates beyong the defined range. You may set any date range.
set MyMessages to {}
repeat with i from 1 to number of items in iCloudMsgs
set this_item to item i of iCloudMsgs
if this_item's sender is TheSenderAddress then
set end of MyMessages to this_item -- since AppleScript returns a list pre-sorted by date received the first message of this list is also the most recent one.
exit repeat
end if
end repeat
set MsgToReply to the first item of MyMessages -- a standard Mail message
tell (reply MsgToReply) -- a new outgoing message is created between the brackets
set content to "This is an automated test message. Please don't respond." & return & return & return & return & TheSgn -- you may supply any other content.
send
end tell
end tell
No matter what I try I can’t tell the outgoing message MsgToReply to set its contents even though the dictionary tells that its class is namely what it is – an outgoing message.
If I create an outgoing message in a ordinary way (
)
then I’m able to populate its body but not if I make it as
And I do need it as a reply message with all its attributes – original text, Re: in the subject etc. How to I achieve that?
Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 9537.86.7
Operating System: macOS 10.9
property TheSenderAddress : "Firefox <Mozilla@e.mozilla.org>"
property NameOfYouriCloudAccount : "iCloud"
property YourSignature : "Signature #4"
tell application "Mail"
activate
set MyiCloudAccount to the iCloud account named NameOfYouriCloudAccount
set MyiCloudMailbox to the first item of (every mailbox of MyiCloudAccount whose name is "INBOX")
set iCloudMsgs to every message of MyiCloudMailbox whose date received is greater than (current date) - 2
repeat with aMessage in iCloudMsgs
if (aMessage's sender) is TheSenderAddress then
set MsgToReply to aMessage
exit repeat
end if
end repeat
set windowName to "Re: " & subject of MsgToReply
set MsgToReply to reply MsgToReply
tell MsgToReply -- a new outgoing message is created between the brackets
set content to "This is an automated test message. Please don't respond."
end tell
end tell
tell application "System Events" to tell application process "Mail"
repeat until (window windowName exists)
delay 0.1
end repeat
tell window windowName
click pop up button "Signature:"
click menu item YourSignature of menu 1 of pop up button "Signature:"
end tell
end tell
tell application "Mail" to send MsgToReply
Something weird happens. As of the moment when I created this thread I was able to retrieve the content of a signature (otherwise I wouldn’t share my experience). Today, getting down to re-writing some part of my script I ran into the wall because every time I attempted to run the statements
Mail would hang until I kill it in Terminal. That’s not the end of the story, though. I then quickly composed another snippet consisting of GUI scripting lines. I use “Accessibility Inspector”. Proceeding through the hierarchy I passed every turn successfully up to the point when I needed just an element before the last one in a line. The element was a “menu”, preceded by a “popup button” and succeeded by the desired “menu item” – the target signature. When I clicked the menu in “Accessibility Inspector” it told me (in red text) about an error that there were no attributes: the menu object lacks attributes though it sits on the tree. Furthermore, I turned off the element lock in the inspector and only that way was able to move the cursor to one of the signatures in that menu: this was impossible in a locked mode where you plod through the branches by clicking elements and following the violet marker because every time I would reach this menu I was greeted by aforementioned warning. Despite the fact that each of menu items had “AXPress” actions listed clicking on them ended up in the same warning: no attributes.
This is the dead end. I’m not able to attach a signature neither by using AppleScript nor by GUI scripting in 10.9.5. Apple broke it. And I’m yet to double-check and verify in other 2 systems I run. Impossible to use AppleScript to script scriptable applications. Ineptitude of the highest rank.
https://imgur.com/czHXDG7
Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 9537.86.7
Operating System: macOS 10.9