AppleScript: Get highlighted Apple Mail message - applescript

I need to be able to get the currently highlighted message from the current message thread.
ie
thread 1
message 1
message 2
message 3 <- highlighted message
message 4
I have tried:
tell application "Mail"
set messages to selected messages of first message viewer
set msg to first item of messages
end tell
but this just returns the thread of the currently selected message. It also does not include any outgoing messages in the thread.

You don't say what version of Mail or Mac OS X you're using, but that script doesn't run with Mail 10.2 / OS 10.12.2. The error is "Mail got an error: Can’t set every message to selected messages of message viewer 1."
This slight modification works for me:
tell application "Mail"
set theMessages to selected messages of first message viewer
set theMessage to first item of theMessages
end tell
If I run it against the message structure you describe then it sets theMessage to whichever one is selected. If multiples are selected then in my testing the order is as expected (from top to bottom), unless "thread 1" is also selected. In that case the order could vary.

Related

Applescript, Mail.app's property "background activity count" is broken?

I have read these four questions with examples for how to use the background activity count Applescript property in Mail.app:
Check for New Mail of specific Mailbox/Folder Applescript
AppleScript and Mail.app: checking if new messages contain a string
AppleScript can't read Mail.app's messages
Applescript to wait for AppleMail to download messages.
It seems simple enough but when I do I get an error:
error "Mail got an error: AppleEvent handler failed." number -10000
Some code:
tell application "Mail"
activate
set bg_count to background activity count
repeat until background activity count = 0
delay 0.1 --wait until all new messages are in the box
end repeat
end tell
Both the set- and the repeat-statement give the same error message.
What is going on here? Is this broken?

One click forward marked messages. AppleScript

I have an AppleScript whose task is to forward flagged messages to another email (in the standard Apple Mail application), but it does not work as I need. I added this script to the Automator as a service.
I have to
1) flag the message.
2) go to the services and select the "Forward marked messages" service that I need, and only then they are forwarded all at once.
My idea is to auto forward a incoming message to the assistant immediately after marked the message with a flag.
Could someone correct my AppleScript?
Or am I mistaken in introducing it into services?
Help me please!
set toAddress to "alex#example.com"
set toName to "Alex"
tell application "Mail"
repeat with _acct in imap accounts
--Look For Flagged Messages in the INBOX
set _acct_name to name of _acct
set _inbox to _acct's mailbox "INBOX"
set _msgs_to_capture to (a reference to ¬
(every message of _inbox ¬
whose flagged status is true))
repeat with _msg in _msgs_to_capture
set _fwdmsg to forward _msg with opening window
tell _fwdmsg
make new to recipient at end of ¬
to recipients with properties {name:toName, address:toAddress}
end tell
activate
send _fwdmsg
end repeat
end repeat
end tell

Outlook 2011: Adding some messages to "Waiting for reply" folder

My use case is that I want to track for responses on some messages I write. My methodology now is to wait for the message to be sent the move it from the sent folder to the "Waiting for reply" folder which I go over periodically.
I'm looking for a way to automate this. Best would be if I press a key which makes Outlook both send the message and put it in the Waiting folder. E.g., by using an applescript.
Alternatively, I thought that pressing a key will add me as BCC, and also add a "WF" string at the bottom of the message. Again, with applescript. Then when I send the message it will also arrive at my Inbox where I'll have a rule to move messages to "Waiting" if they contain "WF"
I expanded the script from your other thread to this here:
tell application "Microsoft Outlook"
-- Simple definition of target mail folder
-- Works fine this way if only one folder with this name is available
set waitingForReplyFolder to folder "Waiting for reply"
-- bring Outlook to front
activate
-- remember the front window
set theWindow to window 1
-- check it's really draft
if class of theWindow is not draft window then
display dialog "Not a draft"
return
end if
-- save the draft
save theWindow
-- get the id of the object of the draft window
set myObjectID to id of (object of theWindow)
-- close the message window
close theWindow
-- checking the message' subject
set theSubject to subject of message id myObjectID
-- send the message
send message id myObjectID
-- check and wait until Outlook has moved the mail to the sent folder
-- move it to target folder after we have found it
set mailFoundAndMoved to false
repeat 20 times
-- check the next 20 message ids
repeat with idCounter from 1 to 20
try
set freshSentMail to outgoing message id (myObjectID + idCounter)
-- check if the subject is the same (just to be safe)
if subject of freshSentMail is equal to theSubject then
-- move the sent mail to the "waiting for reply" folder
move freshSentMail to waitingForReplyFolder
set mailFoundAndMoved to true
exit repeat
end if
on error errstr
end try
end repeat
if mailFoundAndMoved then exit repeat
delay 0.5
end repeat
end tell
Now you must just see how to trigger this. Open a new message, write the content etc. and run this script. It will send the mail and move it to your target folder, just after it appeared inside the sent folder.
Cheers,
Michael / Hamburg

How do I stop AppleScript from selecting all messages in a Mail.app conversation when I only want one from the group?

Here's my itch: I want to add emails to Reminders.app as todo items. I figured that part out. My next goal was to be able to select multiple emails and have Reminders.app create todos for each email selected. I figured that part out.
The problem: When I select an email that is part of a conversation ALL messages from that conversation are added as individual reminders/todos. This part may be confusing but I'll try to be as detailed as possible in describing how I'm selecting messages. In Mail.app I'm selecting a message in the far right-side pane where all emails that part of the conversation are presented in a scrollable list. It's this area where I'm selecting a particular message. So even though I'm selecting one message from a conversation, all messages in that conversation are added to my AppleScript list variable and, subsequently, turned into reminders/todos.
If I turn off 'Organize by Conversation' in Mail.app the problem goes away. I like the cleanliness of organizing my emails by conversation so if there's a scripting solution to this problem I'd prefer that route. However, I can't think of any way to fix this so I'm hoping someone here has some thoughts.
Here's my script:
property defaultList : "Parking Lot"
on newReminder(theBody, theTitle)
tell application "Reminders"
tell list defaultList
make new reminder with properties {body:theBody, name:theTitle}
end tell
end tell
end newReminder
tell application "Mail"
set selectedMessages to {}
set selectedMessages to selection
if (count of selectedMessages) is 0 then
return "Please select a message in Mail.app and try again."
else
repeat with i from 1 to (count of selectedMessages)
tell item i of selectedMessages
set messageid to message id
set urlText to "message://" & "%3c" & messageid & "%3e"
set theSender to extract name from sender
set theSubject to subject
my newReminder((theSender & " " & urlText), theSubject)
end tell
end repeat
end if
end tell
The AppleScript property selection for the Mail application appears to ignore whether a single message from a conversation is highlighted in the preview pane (the rightmost pane in OS X Lion's layout). The selection is determined solely by which messages are selected in the message list (the middle pane). If you want to utilize selection in your AppleScript for a single message of a conversation, you'll have to select the single message from the message list,
instead of the preview pane.

How to set the sender of the current Mail.app outgoing message via AppleScript?

I'd like to write an AppleScript to set the sender of the current outgoing message in Apple's Mail.app.
I've tried this:
tell application "Mail" to set sender of front outgoing message to "<my email address>"
but I get the error message error "Mail got an error: Can’t set sender of item to any." number -10006 from sender of item to any which doesn't make sense to me.
When I try to interrogate the front outgoing message as follows:
tell application "Mail" to get properties of front outgoing message
I get {class:item} in return, instead of an "outgoing message" object like I'd expect.
Any ideas?
Unfortunately, you cannot get or set the properties of the outgoing message object of Mail with Applescript.
Instead, you can accomplish this with GUI scripting, a workaround that directly manipulates window elements.
This code should work for you:
tell application "System Events"
tell process "Mail"
click pop up button 1 of window 1
click menu item 6 of menu 1 of pop up button 1 of window 1
end tell
end tell
Change the menu item 6 on the fourth line to whichever number in the list your desired sender is (e.g., if the sender you want to change to with this script is the fourth one listed, change menu item 6 to menu item 4).
Update: If you're curious, since this answer is over two years old: as of 2014-04-26, getting/setting the properties of outgoing messages is still impossible and this workaround still works in OS X 10.9 Mavericks.
I think it's a little more complicated. My own experiments agree with the conclusion of Alan Kimelman in this thread, that AppleScript works as expected for outgoing messages created from scratch by the script. For example, the following code works:
tell application "Mail"
set newMessage to (a reference to (make new outgoing message))
tell newMessage
make new to recipient at beginning of to recipients ¬
with properties {address:"hello#world.com", name:"The World"}
set the sender to "Jerry Krinock <jerry#sheepsystems.com>"
set the subject to "A Test"
set the content to "This is only a test."
send
end tell
end tell
However, if the message is created by other means (for example, I create HTML messages by telling Safari to Share via Email), then Matthew McVickar is correct that AppleScript is broken. You can't set or get any properties, and also it refuses the send command.
Actually you can set the sender of an outgoing message as described here:
Incorrect sender when sending Email via Applescript
It is more than a little frustrating that you can't set the sender of an outgoing message.
Here's a more general version of Matthew's script that I wrote many years ago and have used frequently. The argument is a string that contains exactly what you see in the "From:" pop up, e.g.
"Mitchell L Model <dev#software-concepts.org>"
(It is very tricky to get the details of something like this right if you don't do a lot of GUI scripting.)
to setFrom(address)
tell application "System Events"
activate application "Mail"
tell pop up button "From:" of window 1 of application process "Mail"
click
tell menu item address of menu 1 to click
end tell
end tell
end setFrom
The reason I use a parameterized handler is that I use keystroke macro facilities to bind a keystroke combination for each of my email addresses. Each executes an AppleScript that loads the file containing the above handler and invokes it with a specific email address. For example:
property util : load script alias (((path to home folder) as text) & "Scripts:Utilities.scpt")
util's 's setFrom("Mitchell L Model <dev#software-concepts.org>")
It's trivial to use AppleScript to create a new outgoing message with any outgoing address that you like (well, at least, the ones configured in your Mail.app preferences).
The discussions here hinge around trying (incorrectly) to change it after the message is created. Instead, specify it when you create the message:
set new_m to make new outgoing message with properties {sender:"My Name <me#my.com>"}
The text in quotes needs to match both the real name and the email address (in chevrons) of any configured account. If there's no match, Mail.app will use the default address
Try this instead.
tell application "Mail" to make new outgoing message with properties {sender:"<your_email_address>", visible:true}
UPDATE: I would look in Mail's scripting dictionary and see what you can and cannot do. The information provided there might help. :)
An alternative to the GUI scripting example I provided would be an Automator script that utilizes the 'Watch Me Do' command. I don't understand exactly how it works underneath the hood, but it ostensibly records mouse movement and GUI interaction and then reenacts what you recorded it when it runs. Unfortunately, I experienced significant lag when running the script. After asking it to run, it would sometimes execute after 5 or more seconds, which is clearly unusable.
The GUI scripting method is almost instantaneous and cleaner-looking (no mouse movement) to boot. I recommend it.

Resources