• 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: Can't Coerce String to Date?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't Coerce String to Date?


  • Subject: Re: Can't Coerce String to Date?
  • From: Shane Stanley <email@hidden>
  • Date: Sat, 14 Mar 2015 09:52:07 +1100

On 14 Mar 2015, at 2:37 am, Alex Hall <email@hidden> wrote:

The reason is kind of long, but suffice it to say that when I need a date in a script, it has to be in a dictionary format so I can pull out specific pieces.

I'll take your word for it, but I'm entirely unconvinced :-)

To do this, I have a handler that takes a date object and hands me my dictionary. Using NSDates is causing problems, though. NSDateFormatter isn't really an option, again because of the way this script works and what it does, and it seems a waste to set up NSDateComponents (not to mention I can't figure out how to OR together the masks to get the information I want).

You can use NSDateComponents via NSCalendar. The way you or the masks is to coerce them to integers and add them up. For example:

set theFlag to ((current application's NSEraCalendarUnit) as integer) + ¬
((current application's NSYearCalendarUnit) as integer) + ¬
((current application's NSMonthCalendarUnit) as integer) + ¬
((current application's NSDayCalendarUnit) as integer) + ¬
((current application's NSHourCalendarUnit) as integer) + ¬
((current application's NSMinuteCalendarUnit) as integer) + ¬
((current application's NSSecondCalendarUnit) as integer)

Now if you run that, you'll get a result of 254, so in this case there's a good argument for not bothering. Then your script becomes:

set theCalendar to current application's NSCalendar's currentCalendar()
set theComponents to theCalendar's components:254 fromDate:theNSDate
set theEra to theComponents's era()
set theYear to theComponents's |year|()
if theEra = 0 then set theYear to -theYear
set theMonth to theComponents's |month|()
set theDay to theComponents's |day|()
set theHour to theComponents's hour()
set theMinute to theComponents's minute()
set theSeconds to theComponents's |second|()

You may not need all those components.

I thought it would be easy:

set startDateString to (e's startDate()'s |description|()) as text
set startDate to (word 2 of startDateString & "/" & word 3 of startDateString & "/" & word 1 of startDateString) as date

So many places for things to go wrong in just two lines of code :-(

If you want a handler to convert from NSDates to AS dates, I've posted one here before and it's in my book. But it essentially uses the code above. And really, that's a conversion you should only make when you need an actual AS date -- they're lame compared with NSDates.

And you should probably have another think about NSDateFormatter. Using built-in formatters gives you localization.

-- 
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

References: 
 >Can't Coerce String to Date? (From: Alex Hall <email@hidden>)

  • Prev by Date: Re: What Does Error Return ?
  • Next by Date: Re: Can't Coerce String to Date?
  • Previous by thread: Re: Can't Coerce String to Date?
  • Next by thread: Test
  • Index(es):
    • Date
    • Thread