On 20 Aug 2015, at 9:27 am, Stockly, Ed <email@hidden> wrote:
The other aspect that bothered me (and I don't know if this has changed or not) was that initially there was no standard text to date format that would always work, regardless of what your mac's settings were.
That's actually what's causing the issue here. It will convert a string to date/time, only if it matches your mac's settings, but there is (or at least was) no one setting common to all macs that you can rely on, no matter where your script was installed.
If there were such a setting (or settings), there would be no problem converting that string to a date.
That's a valid issue, but it's not quite the issue here. Let's assume there was such a format -- say yyyy-MM-dd hh:mm:ss. That's not going to deal with Michael's problem, because he wants to use "10:11", and he's expecting some rule to understand that he means the hh:mm part (and not mm:ss), and that he wants the rest of the values filled in with the values of now.
Addressing your issue becomes a lot easier in 10.11 -- you can define your own format:
use framework "Foundation"
set theFormatter to current application's NSDateFormatter's alloc()'s init() theFormatter's setDateFormat:"yyyy-MM-dd hh:mm:ss" set theDate to (theFormatter's dateFromString:"2015-08-20 09:59:00") as date
But that code won't work in earlier versions because you can't use a simple coercion to make an AppleScript date from a Cocoa date. However, this will work in Mavericks and beyond with my BridgePlus library:
use framework "Foundation" use script "BridgePlus"
set theFormatter to current application's NSDateFormatter's alloc()'s init() theFormatter's setDateFormat:"yyyy-MM-dd hh:mm:ss" set theDate to ASify from (theFormatter's dateFromString:"2015-08-20 09:59:00")
To solve Michael's problem, in 10.11 you can use:
use scripting additions use framework "Foundation"
set theFormatter to current application's NSDateFormatter's alloc()'s init() theFormatter's setDefaultDate:(current date) theFormatter's setDateFormat:"hh:mm" set theDate to (theFormatter's dateFromString:"10:11") as date
Or earlier versions using BridgePlus:
use framework "Foundation" use script "BridgePlus"
set theFormatter to current application's NSDateFormatter's alloc()'s init() theFormatter's setDefaultDate:(current application's NSDate's |date|()) theFormatter's setDateFormat:"hh:mm" set theDate to ASify from (theFormatter's dateFromString:"10:11") And if you want to do it now but without BridgePlus, you can include your own handler to convert Cocoa dates to AS dates, like this:
use scripting additions use framework "Foundation"
on makeASDateFrom:theNSDate set theCalendar to current application's NSCalendar's currentCalendar() set comps to theCalendar's componentsInTimeZone:(missing value) fromDate:theNSDate tell (current date) to set {theASDate, year, day, its month, day, time} to ¬ {it, comps's |year|(), 1, comps's |month|(), comps's |day|(), (comps's hour()) * hours + (comps's minute()) * minutes + (comps's |second|())} return theASDate end makeASDateFrom:
|