Getting Short Date string from Date Received of Mail Message

Hi there,

I’m trying to set a variable to the short date string for date an email was received but I’m having trouble converting it.

Currently, if I use the function ‘date received’ the date is returned in this format:
Monday, 6 March, 2017 at 19:29:13

I’d like to store this date in a variable but in the following format:
MM/DD/YYYY

I have tried using the short date string function but for some reason Applescript cant get the short date string of that format.

The following script is what I have so far. Basically, you select an email in your inbox and then run this and it returns the date received. I really need help converting the date format into a more database-friendly format!

tell application "Mail"
	set selectedMessages to selection
	if (count of selectedMessages) is equal to 0 then
		display alert "No Messages Selected" message "Select the messages you want to collect before running this script."
		error number -128
	end if
	set dateList to {}
	set reportText to ""
	repeat with theMessage in selectedMessages
		set reportText to reportText & (content of theMessage) as string
	end repeat
	repeat with tMsg in (get selection)
		set end of dateList to the (date received of tMsg)
		return dateList
	end repeat
end tell

Hello Adam.

Here on my french system this instruction

set end of dateList to the short date string of (date received of tMsg)

fails but this one behaves flawlessly

set end of dateList to the short date string of (get date received of tMsg)

I guess that it’s a de-referencing problem.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) lundi 13 mars 2017 16:11:38

That works perfectly.
Thanks Yvan!!

The conversion can be done by replacing your code within the last repeat-end repeat with


		set theDate to (date received of tMsg)
		set dateString to short date string of theDate
		copy dateString to the end of dateList

Your looping strategy needs some work. As it now stands, you will only get the date for the first selection.

Hope this helps!

Thanks for the feedback Adam.

Two questions

1 - is the instruction return dateList a temporary one or is it just misplaced ?
To return the list it must be moved after the end repeat.

2- May you edit the contents of the small local menu which allow us to select the version of the Operating System we are running ? For months it’s higher version is 10.10.
Would be fine to add 10.11, 10.12 and as we will quickly see it, 10.13
It’s not for me as the used system appears in my signature but I think to other helpers or askers.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) lundi 13 mars 2017 17:38:56

Hey Yvan,

The list of supported OS’ definitely needs updating, you’re right :slight_smile:
For people viewing this in the future, the OS I am currently using is 10.12.3

I’ll add this information to my signature for now, until the list is updated.

As for the Datelist - now you’ve helped me solve the short date problem, I’ll definitely re-write the script because, as haolesurferdude pointed out, the looping does need some work.

Thanks again for the support guys!

Adam

OK.

Something like that :

tell application "Mail"
	set selectedMessages to selection
	if (count of selectedMessages) is equal to 0 then
		display alert "No Messages Selected" message "Select the messages you want to collect before running this script."
		error number -128
	end if
	set dateList to {}
	set reportText to ""
	repeat with theMessage in selectedMessages
		set reportText to reportText & (content of theMessage) as string
		set end of dateList to short date string of (get date received of theMessage)
	end repeat
end tell
{reportText, dateList}

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) lundi 13 mars 2017 18:01:46