• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: date syntax not working on 10.10.5?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: date syntax not working on 10.10.5?


  • Subject: Re: date syntax not working on 10.10.5?
  • From: Shane Stanley <email@hidden>
  • Date: Thu, 20 Aug 2015 10:15:16 +1000

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:

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: date syntax not working on 10.10.5?
      • From: "Stockly, Ed" <email@hidden>
References: 
 >date syntax not working on 10.10.5? (From: Michael Grant <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Christopher Stone <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Michael Grant <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Shane Stanley <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Michael Grant <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: "Stockly, Ed" <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Yvan KOENIG <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Michael Grant <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Shane Stanley <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Michael Grant <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Shane Stanley <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Michael Grant <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: "Stockly, Ed" <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Shane Stanley <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: "Stockly, Ed" <email@hidden>)

  • Prev by Date: Re: date syntax not working on 10.10.5?
  • Next by Date: Re: date syntax not working on 10.10.5?
  • Previous by thread: Re: date syntax not working on 10.10.5?
  • Next by thread: Re: date syntax not working on 10.10.5?
  • Index(es):
    • Date
    • Thread