I looked and looked but couldn't find an answer to this seemingly simple question (I'm also new to AppleScript). Basically, I just want a script that sets up a Rule and moves messages to mailbox "Foo" (a Mailbox in my IMAP account). Here's what I have:
tell application "Mail"
set newRule to make new rule at end of rules with properties {name:"Foo Internal", enabled:true}
tell newRule
make new rule condition at end of rule conditions with properties {rule type:any recipient, expression:"internal_foo#foo.com", qualifier:does contain value}
set move message to mailbox "Foo"
end tell
end tell
I've tried various combinations of this line...
set move message to mailbox "Foo"
...including specifying the IMAP account, setting it to a variable, and so on. I'm not a programmer but I really want to script these rules cause I setup rules all the time at my job. Any help?
Your script fails because of two things:
it omits to set the should move message property of the rule to true, which is needed for a move message action to actually “stick”;
it does not define the object hierarchy of the target mailbox correctly: you will need both the account and the application context, as you are inside a tell block targeting the rule, not Mail.
The following code:
tell application "Mail"
set newRule to make new rule at end of rules with properties {name:"Foo Internal", enabled:true, should move message:true}
tell newRule
make new rule condition at end of rule conditions with properties {rule type:any recipient, expression:"internal_foo#foo.com", qualifier:does contain value}
set move message to (mailbox "Foo" of account "Bar" of application "Mail")
end tell
end tell
will work in Mail 5.2 (stock as of OS X 10.7.4).
Related
sorry I didn't paste my piece of code but I don't know where to start... I'm talking about apple script. I've found some code to send emails and I would like to implement a script that does what I'm about to write.
I have a folder that contains about 1000 pdf files that I have to send in groups of 20 at a time via email.
I have a bit of confusion in my head but conceptually I think it can be done by counting all the files in the folder, then every 20 files I take the respective file names and set a variable with these names, including the path and I send the first email, then I proceed with the other 20 and so on until it has completed the number of files in the folder. Is there someone who can help me? A thousand thanks!!
I am attaching the portion of code relating to sending via email...
I tried the given code and it works. it sends the email but i didn't check if it sends attachment
`
tell application "Mail"
set theFrom to ""
set theTos to {""}
set theCcs to {}
set theBccs to {}
set theSubject to ""
set theContent to ""
set theSignature to ""
set theAttachments to {}
set theDelay to 1
set theMessage to make new outgoing message with properties {sender:theFrom, subject:theSubject, content:theContent, visible:false}
tell theMessage
repeat with theTo in theTos
make new recipient at end of to recipients with properties {address:theTo}
end repeat
repeat with theCc in theCcs
make new cc recipient at end of cc recipients with properties {address:theCc}
end repeat
repeat with theBcc in theBccs
make new bcc recipient at end of bcc recipients with properties {address:theBcc}
end repeat
repeat with theAttachment in theAttachments
make new attachment with properties {file name:theAttachment as alias} at after last paragraph
delay theDelay
end repeat
end tell
# macOS 10.12+ know bug
# https://stackoverflow.com/questions/39860859/setting-message-signature-of-outgoing-message-using-applescript
# set message signature of theMessage to signature theSignature
send theMessage
end tell
`
I have created a rule that messages move into a subfolder when they according to certain criteria.
Additionally, I have saved in this rule an AppleScript that this mail copied subsequently into an DMS.
It is working.
My problem is, that the name of the subfolder is (for now) not passed to the script. It contains only "INBOX":
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with theMessage in theMessages
set nameAccount to name of mailbox of theMessage # here is the problem !
try
tell theMessage
— at this point get the data out of the mail
end tell
tell application id „DMS“
— here the data will save to the dms
end tell
end try
end repeat
end tell
# end try
# end tell
end perform mail action with messages
end using terms from
How do I get the name of the destination folder from the active mail rule?
Instead of the name get the whole mailbox object, it contains the actual reference in the folder hierarchy, to create a subfolder you need the reference anyway.
set theMailbox to mailbox of theMessage
PS: the tell application "Mail" block is not needed, the using terms block is sufficient to evaluate the terminology.
I would like to send an email with applescript. This script will be used on a few computers and the email should be sent by a certain type of account containing "mynetwork.com" in the email address.
Is there a way to automatically select the computer email account containing "my network.com" ? Obviously the wildcard * is not working see code below
property faxboxEmail : {"fax#opilbox.com"}
property theNumber : ""
property theContent : ""
property theSender : "*#mynetwork.com"
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:theNumber, content:theContent, sender:theSender}
tell newMessage
make new to recipient with properties {address:faxboxEmail}
end tell
end tell
The sender address for accounts in Mac OS X Mail are stored in the sender addresses property in an account. So, technically what you want is something like get account whose email addresses ends with "#mynetwork.com". However, the email addresses property is a simple list, and AppleScript doesn’t have dynamic searches into lists like that.
However, the number of accounts on any personal computer should be fairly small, so looping through them shouldn’t be a problem.
property faxboxEmail : {"fax#opilbox.com"}
property theNumber : "Nine"
property theContent : "Hello, World"
set foundAddress to false
tell application "Mail"
repeat with potentialSender in accounts
tell potentialSender
repeat with potentialAddress in email addresses as list
if potentialAddress ends with "#mynetwork.com" then
set foundAddress to true
exit repeat
end if
end repeat
end tell
if foundAddress then
exit repeat
end if
end repeat
if foundAddress then
set senderName to full name of potentialSender
set senderAddress to senderName & " <" & potentialAddress & ">"
set newMessage to make new outgoing message with properties {visible:true, subject:theNumber, content:theContent, sender:senderAddress}
tell newMessage
make new to recipient with properties {address:faxboxEmail}
end tell
end if
end tell
You may also find it useful to look at the properties of each account, using something like:
tell application "Mail"
--or “account 1”, “account 3”, etc., up to the number of accounts
get properties of account 2
end tell
If you run that and then look in the results pane, you’ll see all of the properties, including the email addresses property. If your script isn’t doing what you expect it to do, post not just the script, but also the values of the property you’re looking at, in this case the email addresses property. You will probably want to use a test computer with fake example.com, example.org, and example.net email addresses, so as not to expose real email addresses to harvesters.
You also may find it easier to build your scripts from the ground up. For example, in this case, you would want to write a script that sends a hard-coded email from a hard-coded sender. Only once that aspect of the script is working, would you want to add the wrinkle about searching for a dynamic sender. This will simplify your programming process by making each task smaller, and will also make it more obvious where the code is going astray.
I want to have the Apple mail application accept and email from me and then email a file to me. For example, I want to send an email with the subject "#FILE myfile.doc" and have the script trigger because of the #FILE tag in the subject, and then email me back the file called "myfile.doc".
The files will always be in the same path, but it would be nice to be able to specify the path in the script so I can create different ones for different directories.
I know nothing about Applescript and have dabbled in Automator. I only see in Mail that you can trigger an Applescript from a rule. So I don't know if this can be done in Automator.
Please be basic with your responses because I am a newbie to this.
The purpose is to get a file off my computer when I am away.
Thanks,
ROY
You can do it using a mail rule in Mail. Specify your email address as sender and the subject starting with #FILE. Let the mail rule trigger a script like this:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with thisMessage in theMessages
set theSender to sender of thisMessage
set theSubject to subject of thisMessage
if theSubject begins with "#FILE " then
set theAnswer to make new outgoing message with properties {visible:true}
tell theAnswer
try
tell current application
set theFilePathToSend to (POSIX file (text 7 thru -1 of theSubject)) as alias
end tell
set content to "And here is your file: " & rich text 7 thru -1 of theSubject & return & return
tell content to make new attachment with properties {file name:theFilePathToSend} at after the last paragraph
on error errStr
set content to "An error occured: " & return & errStr
end try
set subject to "Re: " & theSubject
make new to recipient at end of to recipients with properties {address:theSender}
send
end tell
end if
end repeat
end tell
end perform mail action with messages
end using terms from
Using the reply-handler didn't work, but the make new outgoing message did! Specify the full path to the file as subject.
Be aware that this IS a security hole! Sender's email addresses can be faked and somebody knowing this can get nearly every file he (or she) wants. You could specify the allowed folders in this script, i.e. if theSubject begins with "#FILE /Users/rbarr/Desktop/Available files/" then or just by adjusting the conditions your mail rule is triggered.
Enjoy, Michael / Hamburg
Apple Mail defines both a class account and a constant account for the rule type property of a rule condition. The Applescript compiler always resolves this collision of the term "account" to the class, making it impossible to do anything with rule conditions that match accounts.
When there's a conflict such as this, how can I specify a constant instead of a class name? Is there a double-angle syntax for enumerated constants? Is there a solution that works for collisions of any type of terms (not just classes & enumerations)?
Examples
Making an "Account" Rule Condition
The goal of the following non-working example is to create a rule condition that will match an account.
tell application "Mail"
set rool to make new rule at end of rules with properties {name:"test", enabled:false}
(* the following ends up creating an 'any recipient' condition, as 'account'
is «class mact»
*)
make new rule condition at end of rule conditions of rool with properties {rule type:account, expression:"Some Account"}
log rule type of last rule condition of rool
-- result: any recipient
(* inspecting the event for the following, «class tacc» produces the proper
record, ({'rtype':'tacc'}), but the rule condition is still an 'any recipient'
*)
make new rule condition at end of rule conditions of rool with properties {rule:«class tacc», expression:"Some Account"}
log rule type of last rule condition of rool
-- result: any recipient
end tell
Comparing
The goal of the following is to test whether a rule condition has rule type account. For it, first create a rule in Mail.app's preferences named "Account" with a single condition that matches some account.
tell application "Mail"
set acctType to rule type of first rule condition of rule "Account"
log acctType is account
-- result: false
log acctType is «class tacc»
-- result: false
end tell
My earlier question "How can I access a property that has the same name as class but different event code?" is similar, but only covers conflicts between class and property names. Moreover, the solution for it (using «class ...») works for properties, but not for other types of collisions.
One work-around is to define an account type variable referring to the enumeration constant outside of the tell application "Mail" block:
set theAccountType to «constant eruttacc»
tell application "Mail"
set rool to make new rule at end of rules with properties {name:"test", enabled:false}
set theCond to make new rule condition at end of rule conditions of rool with properties {rule type:theAccountType, expression:"Some Account"}
properties of theCond
end tell
See also the Raw code reference, which shows the «class ...», «constant ...» and «event ...» raw code forms.
Having experimented with your code for a while, I don't see any solution – this name ambiguity appears to be an oversight on Apple's part.
There is, however, a simple workaround to this: copy the rule type you need from an existing rule ("dummy").
-- "dummy" was created manually to copy rule types from
set rtype to (rule type of last rule condition of rule "dummy")
set rool to make new rule at end of rules with properties {name:"test", enabled:false}
set rcond to make new rule condition at end of rule conditions of rool with properties {rule type:rtype, expression:"~/Library/Mail/IMAP-john.doe#example.com"}
-- test it:
properties of rcond