Tuesday, April 25, 2017

#1 2017-03-13 09:14:02 am

Adam239
Member
Registered: 2016-10-13
Posts: 47

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!

Applescript:

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


running Sierra 10.12.4

Offline

 

#2 2017-03-13 10:11:42 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2955

Re: Getting Short Date string from Date Received of Mail Message

Hello Adam.

Here on my french system this instruction

Applescript:

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

fails but this one behaves flawlessly

Applescript:

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

Offline

 

#3 2017-03-13 10:14:59 am

Adam239
Member
Registered: 2016-10-13
Posts: 47

Re: Getting Short Date string from Date Received of Mail Message

That works perfectly.
Thanks Yvan!!


running Sierra 10.12.4

Offline

 

#4 2017-03-13 10:22:01 am

haolesurferdude
Member
Registered: 2010-02-19
Posts: 82

Re: Getting Short Date string from Date Received of Mail Message

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

Applescript:


       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!

Offline

 

#5 2017-03-13 11:39:01 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2955

Re: Getting Short Date string from Date Received of Mail Message

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

Offline

 

#6 2017-03-13 11:53:24 am

Adam239
Member
Registered: 2016-10-13
Posts: 47

Re: Getting Short Date string from Date Received of Mail Message

Hey Yvan,

The list of supported OS' definitely needs updating, you're right 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


running Sierra 10.12.4

Offline

 

#7 2017-03-13 12:01:51 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2955

Re: Getting Short Date string from Date Received of Mail Message

OK.

Something like that :

Applescript:

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

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.116 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)