Mail.app's rules are sending the wrong messages to AppleScript - applescript

I have the following AppleScript triggered by a Mail.app rule:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with msg in theMessages
set theText to subject of msg & return & content of msg & date sent of msg
display dialog (theText)
end repeat
end perform mail action with messages
end using terms from
If I select a message, right click and choose 'Apply Rules' it works properly. However, if the script is triggered by an incoming message, it seems to have a random message in theMessages.
Here is the rule:
How do I get the right message?
I'm using High Sierra with Mail 11.2.

As your script will iterate over your mail, I expect that your messages are not sorted by date ... So when you run you script it will take the first element (and not the most recent)
Could you run Mail, then sort your email by date (with the most recent at top position) then quit and re-run Mail (to double-check that configuration was saved)
Then verify if your script works.
If you don't want to set the filter manually, according to this, you may add at the following script to the beginning :
tell application "System Events" to click menu item "Date" of menu "Sort By" of menu item "Sort By" of menu "View" of menu bar item "View" of menu bar 1 of process "Mail"
to sort email by date before running your script in order to get the right message.
You may also take a look here, here and here to verify and double check that the rules are properly setup.

Apparently, handling incoming messages with rules is an asynchronous process. When on perform mail action is called, the message is not yet completely updated. Only partial data is available immediately.
A possible workaround would be to add a delay 1 into the script. This give Mail a second to finish updating the message. Here is what the script then looks like:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with msg in theMessages
-- delay a bit of time for msg to load
delay 1
set theText to subject of msg & return & content of msg & date sent of msg
— do other processing
end repeat
end perform mail action with messages
end using terms from

Related

How disable outlook meeting notifications

There I have an issue hope so I will get the right answer here. I have an real estate business. I am using the outlook calendar to book the meetings. but when I am assigning the time to the user or doing any edit-related operation that time another user is getting the mail alerts.i want to disable those I only want to send those the appointment mail noting else. is there any way to disable those emails?
The Application.ItemSend event is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. So, if you need to prevent anything from sending out, you can set the Cancel parameter to false, for example, a VBA handler is shown below:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub

Problem on applying script when i send the email

Does anyone know how to trigger VBA when i sent out an email.
I tried to look for the option from Rule Wizard on "Apply rule on message i send" but there is script option.
Cheers
Of course, you may assign a VBA sub to run when your rule is triggered. The sub should take a single argument which represents the item being sent:
Public Sub Test(item as Outlook.MailItem)
' run your code here
End Sub
You may add an event handler to the ItemSend event of the Application class. It is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
You write an event procedure (also known as an event handler) to respond to events that occur in Microsoft Outlook. For example, you can write an event procedure that automatically maximizes the explorer window when Outlook starts.
Events are associated with particular objects. The Application object is the topmost object, and is always available (that is, it does not have to be created). You can add an Application event procedure in the ThisOutlookSession module window simply by selecting Application in the left list and then selecting the event in the right list.

AutoHotKey - Using Presence of Data In a Text Box to Set Correct Radio Button

One of my data entry scripts has multiple GUIs that prompt the user to answer a series of questions, and based on those answers, generates a template in Notepad to be copied and pasted into the Remarks section of a program to send appointment orders to a contractor.
Some of the GUIs I created have just a Yes and No radio button, but others have a Yes and No radio button along with a text box to enter additional data. For example, one GUI asks if there are upcoming appointments at our clinic. The default radio button is No so the user can quickly skip to the next GUI in the series, since the Next button at the bottom of the GUI is set as default.
If the user leaves the No button as-is, at the end of "20 questions" as the script prints out the the template based on the answers, the script writes out "Upcoming appointments: None"
If the user selects Yes and enters data in the respective text boxes, the template writes the "Upcoming Appointments" banner line, two {Enter} commands, the list of appointments, followed by two more {Enter} commands to leave space between Appointments and the following section.
Example:
If ApptsRadioButtonYes = 1
{
SendInput >{space 2}>{space 2}>{space}Future Appointments - Do NOT Schedule On This Date{(}s{)}{space}<{space 2}<{space 2}< ; Top-of-section banner line
Send {Enter 2}
Send %Appts_Info% ; list of appointments entered in text box
Send {Enter 2}
SendInput >{space 2}>{space 2}>{space 2}>{space}<{space 2}<{space 2}<{space 2}< ; End-of-section banner line
Send {Enter 3}
}
Else If ApptsRadioButtonNo = 1
{
Send Appointments pending: None
Send {Enter 3}
}
The flaw is, with the default set to No, if the user enters info in the text box but forgets to change the radio button to Yes, the script ignores any next-of-kin information or appointments info and prints out a line of text at the end of the script as ""Upcoming Appointments: None" or "NOK: None."
Conversely, if I set the default to Yes but the user did not enter anything in the text box, the script would print "NOK: " with nothing following. The same sort of thing happens with the upcoming appointments GUI, but a little more confusing: the script prints the "Upcoming Appointments" banner line, a blank line, another blank line (where there should have been an appointment), and two more {Enter}.
Is there a command I could use that would change the radio button to Yes if there is data in the text box, something along the lines of (in psuedo code):
If UserData is not blank
{
Set RadioButtonYes to 1
Set RadioButtonNo to 0
}
This way, if there is data and the user forgets to set the appropriate radio button to Yes, the presence of data in the text box takes care of the correct setting, and the appropriate heading plus the data is written.
Alternately, I could just change GUIs like that to just Yes/No, and if the answer is Yes, another GUI would pop up to get the data; if the answer is No, it would continue on to the next Yes/No GUI. I would rather keep the number of GUIs down to a manageable size and confined to one topic each. I could even just combine everything into one GUI, but that might get a little "busy."
Thanks!
You can launch a subroutine automatically whenever the user types something into the edit field:
A g-label such as gMySubroutine may be listed in the control's
options. This would cause the MySubroutine label to be launched
automatically whenever the user or the script changes the contents of
the control.
(documentation)
In my following code example, this is represented by the gcharTyped keyword, meaning "Jump to label called chartyped".
gui, 1:add, radio, vApptsRadioButtonYes group, Yes
gui, 1:add, radio, checked vApptsRadioButtonNo, No
gui, 1:add, edit, vUserData gcharTyped w150 h150
gui, 1:add, button, gsubmit, submit
gui, 1:show
return
submit:
gui, 1:submit
if ApptsRadioButtonYes = 1
msgbox, Yes!
else if ApptsRadioButtonNo = 1
msgbox, No!
exitapp
return
charTyped:
; change the state of the first radio button to CHECKED:
; also unchecks all remaining radio buttons from the same group
GuiControl, 1:, ApptsRadioButtonYes, 1
return
1guiClose:
exitapp
return

Applescript populate a dialog list with Microsoft outlook contacts

All,
I'm attempting to create an applescript that allows me to create a word document (a business proposal). One part is being able to use applescript to select the client from micorsoft outlook.
I know how to do this in VBA but in Applescript I can't seem to figure it out. Basically I need a dialog box that either has a list of all my Outlook contacts from which I can select one.
Much appreciated,
-J
Quick and dirty, but this works (Office 2008)
tell application "Microsoft Entourage"
set contactList to {}
set lastContact to (count contacts)
repeat with thisContact from 1 to lastContact
set theContact to item thisContact of contacts
set end of contactList to (first name of theContact & " " & last name of theContact)
end repeat
set contactSelected to (choose from list contactList with prompt "Please select a contact." without multiple selections allowed) as text
if (contactSelected is not "False") then
display dialog contactSelected
end if
end tell
There are essentially two parts to the script: getting the contact names and presenting the information. Getting the contacts is easy because contacts is a property of the application itself. Running this in 40+ contacts only takes a second.
Presenting the data and getting the selection isn't so obvious. The data to be presented has to be a string. Honestly, I forget why I have as text dangling off the end, but I seem to remember that doing this was easier if everything was handled as a string of some kind. Once the selection has been verified—having "False" returned means the user clicked the cancel button—you are then able to carry on with the string where I placed the display dialog. Unfortunately, you don't get the row number or anything convenient like that. It just doesn't work that way, so you will have to do a bit of fudging to get back to the corresponding contact object itself.
Add salt to taste...

Prevent Email From Being Sent via Applescript

I've got the following AppleScript (below). I'm attempting to confirm the sending of an email. This AppleScript is already successfully hooked up to an "outbox rule" (using Mail Act-On) in Mail.app, and I've verified that it runs when it is supposed to (at the time of sending).
The ultimate goal is to pop a dialog to the user, asking if they "really" want to send the email. If not, stop the email from being sent.
The current script attempts to delete the message, but that doesn't work. Any ideas?
using terms from application "Mail"
on perform mail action with messages messageList for rule theRule
repeat with thisMessage in messageList
set theResult to display dialog "Send?" buttons {"OK", "Cancel"} default button 2
if button returned of theResult is not equal to "OK" then
delete thisMessage
end if
end repeat
end perform mail action with messages
end using terms from
I think that a "Cancel" button in a display dialog will immediately end execution of your script which means that the delete thisMessage line never is run.
You might try changing that to something like:
set theResult to display dialog "Send?" buttons {"OK", "No, Delete Message"} default button 2
if button returned of theResult is not equal to "OK" then
...

Resources