Coercing a Date

I’m searching through my calendar, looking for events that have a particular entry. I want to list these along with their start date, and I would like to show the latter in the (UK) form d/m/y where these are integers. I’ve managed to find the records I want OK along with the dates but I’m having a problem coercing the dates into that format.

I thought that if I produced a list, say dateList, of {d, m, y} for each entry I could change the default text delimiter to "/, " and then coercing dateList into a string would give me the date in the format that I wanted. However producing dateList is not as straightforward as I thought.

If eventDate is the start date for a particular event I was trying to use the following to produce dateList:

			set dateList to {day, month, year} of eventDate as integer as string

but this fails with the message "Can’t make (day of date ., month of date ., year of date .) into type integer. However if I deal with each separately, as below, this works (I realise that I don’t need the “as integer” for day and year but I wanted to see if using the same coercion for all three separately would work):

			set dayInteger to day of eventDate as integer as string
			set monthInteger to month of eventDate as integer as string
			set yearInteger to year of eventDate as integer as string
/applescript]
Can someone explain why the former doesn't work?

Model: iMac
AppleScript: 2.2.1
Browser: Safari 5.1.7
Operating System: Mac OS X (10.7)

Hi,

an AppleScript date object cannot be coerced (except as text to get its full text representation)
If you need single date properties as text use something like this


tell eventDate to set dateList to {day as text, its month as integer as text, year as text}

Ah, many thanks Stephan, that grand. My script is now working and producing the results I wanted.

I’m very new to AppleScript - I see I’ve got a lot to learn!

Eric

There are two problems with it. Firstly, it’s an AppleScript multiple reference, which can’t be directly coerced ” not correctly, anyway. You have to resolve the reference and then coerce the result. For example:

set dateList to (get {day, month, year} of (current date)) as string

But in your case, even if you resolve the reference first, you can’t coerce a multi-item list to integer. (It doesn’t make sense.) You have to coerce the items individually, either as you have done or as Stefan has suggested.

Thanks Nigel for that explanation. I’ll bear that in mind.

Eric