One click forward marked messages. AppleScript - 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

Related

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.

Applescript for creating New Message with Mail application

I have an AppleScript for Mail.app which opens a new message window with pre-defined recipient address and subject. This script opens a new window every time I run it:
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
end tell
But I want the script to open a new message window only when the previously opened window is closed – otherwise, the previously opened window should come to the front.
Can anyone please help me in modifying this script to achieve the above mentioned functionality?
I didn't test this but it should do what you need... at least it shows you the proper approach. You basically use a "property" to keep track of some value from the last time the script was run. In this case we check for the name of the frontmost window and see if it matches your criteria. If the window name doesn't do what you need then just find some other value to track between launches of the script. The basic approach should work.
EDIT: using the ID of the message, which is unique, the following will do what you want:
property lastWindowID : missing value
tell application "Mail"
set windowIDs to id of windows
if windowIDs does not contain lastWindowID then
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
set lastWindowID to id of window 1
else
tell window id lastWindowID
set visible to false
set visible to true
end tell
activate
end if
end tell
the visibility toggle seems to be the only way to get the window in front, as frontmost is a read-only property. The lastWindowID property will store the ID as long as the script is not re-compiled (caveat empteor: do not put this into an Automator service, as these get re-compiled every time the service is loaded).

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.

How do I select the next message outside of a selection in Mail using Applescript?

I wrote a short Applescript to move the selected messages to the relevant archive for that mailbox. However when it runs there is no longer a selection. (If I then press up or down, it selects either the first or last message in the entire mailbox). I can't work out how to select the next message outside of the current selection using AppleScript. I don't really mind whether 'next' is the message before or after as long as it is near the selection (which is about to disappear).
If it helps, here is my Applescript:
tell application "Mail"
set selectedMessages to selection
repeat with theMessage in selectedMessages
set theAccount to account of mailbox of theMessage
set read status of theMessage to true
set mailbox of theMessage to mailbox "_old" of theAccount
end repeat
end tell
Try something like this:
tell application "Mail"
set theSelection to selection
set theMessage to item 1 of theSelection
set theMailbox to theMessage's mailbox
set theMessageID to theMessage's id
set theMessageIDs to (id of every message of theMailbox)
-- do work here
set theMessages to (every message of theMailbox)
repeat with i from 1 to (count theMessageIDs)
if item i of theMessageIDs is theMessageID then
set message viewer 1's selected messages to {item i of theMessages}
exit repeat
end if
end repeat
end tell
Note that this assumes a few things—only one mailbox is selected, you're not moving the last message, and so forth.

Resources