AppleScript, Microsoft Outlook and kMDItemDueDate

This is a bit of an arcane question, but hoping someone has some experience to help.

I have an AppleScript routine I’ve used for some time that I use to triage email. The script presents a variety of choices, including commands to set the follow-up dates for a message. The script uses UI commands to choose items from the contextual Microsoft Outlook “Follow-Up” menu that presents when command clicking on an Outlook message.

This is married with an Outlook Smart Folder that shows me messages flagged as due today or earlier. The data element the Smart Folder references for due date is kMDItemDueDate.

This all works fine.

Here’s the problem I’m facing. I’ve re-written the applescript to directly set the due date for the message rather than going down the UI scripting path. See script snippet below. The steps I’m referring to follow the set theDoLaterFolder... line

The script is working as expected in terms of due date (and other message attributes… eg destination folder) being set correctly. I can see the new due date in Outlook when I look at the message. However, it seems the internal kMDItemDueDate is not getting set.

kMDItemDueDate is set when choosing the “Follow-Up” flag from the Outlook UI. It does not appear to get set when manually updating due date via AppleScript. At least that is my supposition after extensive troubleshooting.

If kMDItemDueDate is not set, the smart folder rule will never fire.

Assuming that makes sense, any ideas on forcing a value to kMDItemDueDate?

AppleScript snippet for inbox triage
    tell application "Microsoft Outlook"
    	set theSelectedMessage to selected objects
    	repeat with theMessage in theSelectedMessage
    		
    		set theAction to text returned of (display dialog "Archive, Search or Days" default answer "")
    		
    		if theAction is "false" then
    			return
    		else
    			
    			if theAction = "S" then
    				my doSearch(theMessage)
    				return
    				
    			else if theAction = "A" then
    				set theArchiveFolder to folder "2019" of folder "Inbox" of account of theMessage
    				set is read of theMessage to true
    				set todo flag of theMessage to not flagged
    				move theMessage to theArchiveFolder
    				return
    				
    			else
    				set theDoLaterFolder to folder "Do Later" of folder "Inbox" of account of theMessage
    				set is read of theMessage to true
    				set todo flag of theMessage to not completed
    				set due date of theMessage to ((current date) + theAction * days)
    				move theMessage to theDoLaterFolder
    				
    			end if
    		end if
    	end repeat
    end tell

In case you are interested, the following is the raw query used for the Smart Rule (again, this rule works as expected when a message is flagged via the Follow-Up menu — either direct or through UI scripting). It does not work when due date is set via the script.

(com_microsoft_outlook_folderID=132)&&((com_microsoft_outlook_hasDueDate=0)||(kMDItemDueDate<=$time.today))

You ought to contact Microsoft and let them know this, as it’s something that would occur in the lines of code that make up the Outlook application. Clearly they’ve bound the menu item to a function that writes the metadata for the file. They must not have done the same for the AppleScript property.

But very well done for evolving your script and purging the UI scripting guff. To help you commit to the transition, I believe there are AppleScriptObjC methods that allow you to get and set metadata items. I’ll have to dig out what I have when I get home later, but I think the class NSMetadataItem is the one of interest.

@chri.sk thanks for the response. I would enjoy seeing whatever info you have on the AppleScriptObjC option. I have not done anything with that and would love to learn. Not to mention resolving my current problem.

Thanks in advance for any reference you can point me to. Cheers! — jay

well… in the spirit of “always learning”, my supposition above may be wrong. After some more googling I realized I could run the MDLS command with an Outlook message to inspect attributes. In doing that it looks like kMDItemDueDate is getting set, but the time is off.

Looks like I need to do some more script work.

Sharing this to avoid any other work on the part of others to provide other info.

edit:
that was the problem. That is, choosing Follow-Up date from the contextual menu defaults midnight to the due date. My direct method was setting the current time to the due date. As such, the logic of the Smart Folder was failing. I’ve modified the AppleScript to force midnight with the due date and it seems to be working now.

Sorry for the “noise” with this post.

1 Like

No, I think this is useful information for others to benefit from. Thanks for this.

Part 2 to this one. In general, the methods I describe above continue to work fine.

I am observing, though, so some very odd behavior that I can only attribute to our Outlook environment.

As a refresher relative to my current problem, when I flag an email I set the due date to midnight on the day of interest. Occasionally, but not always, I’m finding the due date field is getting reset to GMT midnight. This happens AFTER my script successfully updates due date.

I’m in central timezone US (GMT -6)

Example:
Script sets due date for tomorrow (Jan 23 @ midnight CT). I separately verify after the script is complete that the due date is set correct.
Then when I check again a few minutes later the internal due date shows as Jan 22 @ 18:00:00.

I’m at a loss as to what in the Outlook backend (black box for me) would overwrite my due date with a new time (apparently tied to GMT). This is further complicated by inconsistent behavior. Some message due dates get overwritten and some do not. At this point I cannot figure out a pattern.

I guess I could tweak my smart rules to account for entries that show up with a time of 18:00:00, though I’m not sure what logic I would use for that.

Posting here in the off chance others have ideas about the date synch issue and/or a different way I should be thinking about this. As always, thanks for any ideas — jay

This image illustrates the mess.

The due date of the message in question was set via AppleScript to 1/23/20 00:00:00

  1. Outlook in normal message view shows the flagged status and the correct “Due by” date

  2. When I run an AppleScript query against this message to display the due date it returns what I’m guessing to be the GMT offset. When I run that same query immediately after setting the due date it does show the correct date and time. This change is what is perplexing.

  3. Copy of the code that I use for my smart folder query in Outlook. The message is not displaying in this folder. It should display. (And… yes… the outlook_folderID is correct. Other messages are displaying).

  4. Inspecting internal attributes shows the kMDItemDueDate as correct value

I can’t understand why #2 and #4 don’t match and why the smart rule is not displaying the message.

You’re right, it appears to be directly tied to the GMT offset, which might mean you’ll need to have to cater for that when retrieving dates. There’s a scripting additions command called GMT offset that should give you your offset to save you having to hard code a fixed 6-hour difference. Or retrieve the due date

But it’s sounding like this is a bug, and Microsoft might either have some insight, or be able to fix the issue in their next minor update.

Out of interest, what does the AppleScript code look like that you use to set the due date and what does it look like for retrieving the due date ? If you also obtain a record of all properties for any given message, do all the dates have the same on retrieval ? If you set any of the dates, and then retrieve the properties en masse again, does the offset appear to affect any other dates ?

Thanks for the response. I do think it is a bug. After the fog of war settled, I realized I needed to do some simpler troubleshooting. That is, simply setting the flag date using the native Outlook flag menu choice.

Sure enough, when I added a flag through that path and then checked due date in AppleScript the same behavior is exhibited. due date is correct for 10 or 15 seconds and then gets reset to prior day with the GMT offset.

I’ve passed the problem to our Outlook engineers at my office.