Using AppleScript, is it possible to check for unread emails? - applescript

I just found out about AppleScript, and I'm in the process of learning. I'd like to create something that'll check my email to see if I have a new email. If yes, I want it to return a value (i.e. "Mail"). If not, then I want it to return a different value (i.e. "No Mail"). My idea is to use the Mail application to achieve this.
Looking at the Dictionary for the Mail application (http://www.mugginsoft.com/html/kosmictask/ASDictionaryDocs/Apple/Mail/OS-X-10.7/Mail-5.2/html/), this doesn't look possible since it doesn't seem like you can check for unread mails in particular.

Yes, this is possible. I used to use it with Geektool to show a list of read messages that I hadn’t dealt with yet.
In your case, it’s even easier, since you don’t want to keep a list of unread messages, but simply exit as soon as any are discovered.
The flag you’re looking for is “read status”.
tell application "Mail"
if exists (messages of inbox whose read status is false) then
"Mail"
else
"No Mail"
end if
end tell
Note that you do have to be looking in a particular mailbox; in this case, I’m looking at inbox. If you are presorting messages to other mailboxes using rules, I’m not aware of any way of doing this using AppleScript without checking each mailbox separately. (Though it might be possible using sqlite directly on Mail’s database file.)
This might also work if you need to look for unread mail in other mailboxes:
tell application "Mail"
if exists (messages of inbox whose read status is false) then
return "Mail"
else
repeat with box in mailboxes
tell box
if exists (messages whose read status is false) then
return "Mail"
end if
end tell
end repeat
end if
return "No Mail"
end tell
However, my anecdotal experience is that if you have a complicated setup, running scripts like this (and including this one) will tend to get Mail’s smart (and dumb) mailboxes out of sync with the read status of the messages within them.

Related

Initial survey won't trigger a series of automated surveys. Can someone advice?

I have a survey called 'Set up and kill switch' (image 1) that will trigger a series of weekly surveys to be sent out, but it's not working. I'm not sure if I didn't set it up correctly or if the logic is not right. I attempted to set it up as sending surveys out via email. In step 3 (refer to image 2), I also specified the conditions: when the set up and kill switch survey has been completed and if the predetermined date in the survey matches the date 'today', the system should activate automated surveys for the next 8 weeks so participants should be receiving them on a weekly basis. However, when I tested this out, I did not receive a survey after completing the kill switch survey. Does anyone have experience in making something like this on redcap? I suspect that the logic is fine, but it could be an issue between linking participant IDs so they aren't receiving anything?
Logic code:
datediff([weekly_questionnai_arm_3i][weekly_date],"today","d","mdy",true)>=0 and ([weekly_questionnai_arm_3i][weekly_killswitch(0)])<>"1"
Kill switch screenshot
Automated survey logic
You need to check the box next to 'Ensure logic is still true before sending invitation?'.
As soon the send logic is true the automated emails will be queued/scheduled for sending - changing the form fields so that the send logic is no longer true will not stop the scheduled emails from being sent unless the 'Ensure logic is still true ...' box is checked.
I also recommend clicking and reading the link underneath "How to use 'stop logic' to disable an automated invite".

Send a text message upon email receipt using Mail.app rules and AppleScript

We are looking to use Mail.app's rules to define certain criteria and, when such criteria is met, to use AppleScript to send a single text message separately to multiple individuals (not as a group text). How can this be accomplished?
This 2006 post on Mac OS X Hints looked to do something similar, but the code referenced is inaccessible due to link rot. This question shows how to send an SMS, but it only sends a single message.
You'll first need to set up iMessage in Messages so you can send texts from your Mac.
Then, open Script Editor, create a new script, and paste in the following code:
property recipients : {"+1 (999) 999-9999", "email#icloud.com"}
repeat with recipient in recipients
tell application "Messages"
send "your message here" to participant recipient
end tell
end repeat
In recipients, add as many phone numbers or emails that can receive texts as needed between the curly brackets, each in quotations and separated from the previous one by a comma, and edit your message here to the message you want to send.
Save the script in ~/Library/Application Scripts/com.apple.mail, as explained here.
Create your mail rule and, under Perform the following actions:, include a Run AppleScript action and select the saved script. Once the rule is saved, you're all done.
Upon receiving an email that meets the criteria of the rule you set up, the script will loop through each phone number or email in recipients and send them the specified message. The script has no problem sending a message to a contact you've never messaged before.

Applescript perform action AXShowMenu

I've looked everywhere for an answer but can't find anything on this. When I run this code the action AXShowMenu executes correctly. But the script continues to run for a few seconds and throws the error:
"action "AXShowMenu" of menu button of text field of scroll area 3 of splitter group 1 of window "Messages" of process "Messages" doesn’t understand the “perform” message."
Am I missing something painfully obvious here? How can I get Applescript to move on after it executes the AXShowMenu action?
tell application "System Events"
tell process "Messages"
perform action "AXShowMenu" of menu button of text field of scroll area 3 of splitter group 1 of window "Messages"
end tell
end tell
AXShowMenu
This is the link to the menu i'm trying to access. The element can be created if you create a new message and input some text in the text field.
Having established your end goal is to determine whether the number entered into the To: field in the Messages app operates under the iMessage service or the SMS service, you could try using properties of the buddies and services objects to determine whether they can be used to discriminate effectively between the types of numbers.
As I don't have an iPhone, I lack SMS capability from my Messages app. However, what I've observed when I enter some random number is that it will be categorised by the Messages app into the type of service that, I assume, will be the most appropriate for the nature of the number:
use application "Messages"
service type of services of buddy "12343" = iMessage
For me, this returns true, simply because iMessage is the only active service type that operates in my Messages app. However, this response is inline with what appears in the popup menu within the app.
So, assuming the logic follows, I would hope that you could either make use of those two lines as they are, swapping out the number as needed, and working off a value that will either be true or false; or perhaps first working out if your Messages app has a "default" service into which it will categorise numbers of uncertain nature. My guess is that it will favour "SMS" rather than iMessage given both available services to choose from, in which case the following script might be more suitable:
use application "Messages"
name of service of buddy "12343" = "SMS"
NB: It's possible that different locales use different service names, and what AppleScript on a British system refers to as service "SMS" might be service "Text Message" on a US system, or wherever you're located.
You can easily perform a quick initial check on the names of each Messages service by running tell application "Messages" to get the name of every service. The list will contain 4 or 5 potential services, but it'll be clear which applies to text messaging, and slightly ambiguous but eventually apparent which applies to iMessaging.

AppleScript: How to filter messages based on find or Smart Mailbox?

I am trying to leverage Apple Mail's search capability to quickly get to a list of emails based on content. If I step through with AppleScript and extract each emails content, it takes about 100 times longer:
repeat with aMessage in (messages of inbox whose date received ≥ dDate)
set tContent to content of aMessage as rich text
if "findtext" is in tContent then
do stuff
end if
end repeat
How can do one of the following:
1. Use the UI commands to type in a search term and access the selected messages in a script.
2. Use the UI commands to switch to a custom or favorite MailBox and then access the selected messages in a script.
Thanks in advance.
There does not seem to be a way to actually do this in Outlook. I switched to Apple Mail and was able to get it to work a little faster using similar code.

Retrieve number of messages in smart mailbox in Mail.app

I'd like to retrieve the number of messages in a given smart mailbox in Mail.app.
Basically, I'd like to modify the Übersicht Unread mail count widget (available here) to show the number of messages (both read and unread) in a smart mailbox I created.
The current code used in the widget is:
osascript -e 'tell application "Mail" to set unreadCount to unread count of inbox
The biggest problem seems to be referencing the smart mailbox.

Resources