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
I'm trying to execute the following Applescript within a Rule I set up in Mac Mail. I'm running OS 10.10.1 Yosemite on my iMac. My Rule looks for a particular email I receive every day, then instructs Mail to execute my Applescript. The Rule works perfectly when I manually highlight the email and click on "Apply Rules"; however, when I start up my computer in the morning and receive my first mail of the day. The Applescript program gets caught in never ending loop, which is evidenced by the whirling icon that appears in the menu bar. BTW: My automator routine works perfectly. My thinking is that the Applescript gets confused trying to execute while Mail is downloading all my mail???? Any suggestions?? Oh, I'm a novice...Thx
tell application "System Events"
tell process "Mail"
-- Select the Print menu item
click (first menu item of menu "File" of menu bar 1 whose name begins with "Print")
tell window 1
-- Wait until the print sheet appears
repeat until sheet 1 exists
end repeat
tell sheet 1
-- Click the PDF button
click menu button "PDF"
-- Select the PDF to SBS Dropbox menu item
click (first menu item of menu 1 of menu button "PDF" whose name begins with "PDF to SBS Dropbox")
end tell
end tell
end tell
end tell
First you need to use the Applescript handler called by mail rules:
on perform mail action with messages theMessages for rule theRule
Inside this handler you have access to the mails that matched your mail rule conditions and do whatever you like. Here is the skeleton you can use to add your code:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with thisMessage in theMessages
-- place your code here
end repeat
end tell
end perform mail action with messages
end using terms from
In your special case I think we can try to put your code inside the repeat-loop and let mail open the mail before execution and close it after printing:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
-- walk through all matching messages
repeat with thisMessage in theMessages
-- open the message
set openedMail to open thisMessage
-- perform your UI scripting
tell application "System Events"
tell process "Mail"
-- Select the Print menu item
click (first menu item of menu "File" of menu bar 1 whose name begins with "Print")
tell window 1
-- Wait until the print sheet appears
repeat 30 times
if sheet 1 exists then exit repeat
delay 0.5
end repeat
tell sheet 1
-- Click the PDF button
click menu button "PDF"
-- Select the PDF to SBS Dropbox menu item
click (first menu item of menu 1 of menu button "PDF" whose name begins with "PDF to SBS Dropbox")
end tell
end tell
end tell
end tell
-- close the message
close openedMail
end repeat
end tell
end perform mail action with messages
end using terms from
It's not tested yet! And I prevent the script from running in an infinite loop when trying to access the print sheet now.
Give it a try! Enjoy, Michael / Hamburg
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.
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).
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.