• 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: class reference "date" problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: class reference "date" problems


  • Subject: Re: class reference "date" problems
  • From: has <email@hidden>
  • Date: Thu, 23 Apr 2015 22:58:23 +0100

Jean-Christophe Helary wrote:

> What are the recommended books (I'm actually more a book person) ?
> I'd like something that gives a thorough reference for the language now that I can sort of find my way.

Chris and Stan have already mentioned three titles (I co-wrote the Apress one); there's also the Peachpit book by Sal Soghoian (current Automation/AppleScript manager). One of those four would be your best bet: none of them are perfect - each has its own combination of strengths and faults - and none cover AppleScript or scriptable application changes after 10.6 [1].

However, the core language hasn't changed much since then (it got a built-in library loader in 10.9 (not particularly good, but more than what it had before), and AppleScript-ObjC integration was more or less sorted out in 10.10) so they should keep you fairly straight on that. And for learning AppleScript-ObjC, go get Shane Stanley's books as already advised.



> >> Here it compiles to:
> >> date "Thursday, May 4, 1961 at 11:30:00"
> >>
> >> (notice the lack of GMT reference, even though I seem to have it in System preference...)
> >
> > I believe it pulls only from the Full-Date and the Medium-Time, but you'd have to check to be sure.
>
> That's what it looks like.

AppleScript dates do not support timezone information; they always assume the local timezone. (Yes, they are rubbish. Ancient classic MacOS date/time routines, unfortunately.) If you care about timezone info, you'll need to use Cocoa's NSDate class via the AS-ObjC bridge. Ditto if you want more flexible 'natural' text parsing than AS's `date <string>` specifiers support: see NSDateFormatter for that. Best person to ask for advice here is Shane; in an ideal world someone would've already wrapped all the technical Cocoa stuff in a nice, native, easy-to-use AppleScript library, but the AS world is far from ideal so I suspect you'll need to do it yourself.

One other thing: if you want to construct AppleScript date objects reliably in a non-locale-dependent way, use the following handler:

on makeDate(y, m, d, hr, min, sec)
    set newDate to current date
tell newDate to set {its year, its month, its day, its time} to {y, m, d, hr * hours + min * minutes + sec}
    return newDate
end makeDate


makeDate(2015, 6, 12, 18, 40, 1)
--> date "Friday, 12 June 2015 18:40:01"



> Two more open questions:
>
> 1) is Javascript less of a hassle ?

For what exactly? The JavaScript for Automation (JXA) component introduced in 10.10 is, broadly speaking, a sack of knackers, and (unsurprisingly) largely DOA. Its Apple event (application scripting) and OSA (script attachability) support is ugly, buggy, obfuscated, defective by design, hopelessly documented, and just a joke compared to AppleScript's own. And you've limited chance of getting help and assistance on it, as there's little community support - and what there is knows nearly nothing about how application scripting actually works - and almost zero support from the JXA team themselves. JXA's library loader is also rubbish: weak, underpowered, doesn't work with existing JS package systems, and conflicts with AppleScript's library system too. JXA's Cocoa bridge I'm not sure about, but you'd probably as well looking to third-party JS-Cocoa bridges as those are more mature, more polished, community led, and developed by folks who aren't afraid to eat their own dogfood and talk to their users too.

In general terms, the JavaScript language itself isn't great (though the same can be said of mainstream languages in general), but it has massive industry, tool, and community support that goes a long way to compensating for its faults, and has been making steady progress into server and desktop over the last few years (e.g. see node.js, npm). It's all a bit chaotic, and no doubt still awful at times, but at least it has a real live pulse.


> 2) is there an Applescript wiki ?

Nope. No library repository either. Used to be a general script archive that you could rummage in over at MacScripter, but alas that went titsup years ago. The vast majority AS knowledge and expertise is passed hand to mouth via this list and the MacScripter boards; the AS world has never had a culture of formal code/knowledge reuse and distribution, and it's a bit late in its lifetime for it to be developing one now. Google is your friend, in other words[2].

IMO, you'd probably be better looking towards a more popular, better supported scripting language like Python or Ruby for general scripting, and just turn to AppleScript when you need to do application automation stuff. If there's one thing absolutely in the AS's favor, it's that when you do get stuck on automating a particular application, there's always someone in the AS community who already knows that app like the back of their hand and is more than happy to assist. But when it comes to finding ready-to-use libraries for tasks like parsing human-readable date strings, every other language in the world absolutely mops the floor with it.[3]


HTH

has


[1] Unfortunately the market for AS books has rather gone dead since 2010. (I asked Apress a year or two ago about possibility of doing a 4th edition, but answer was effectively "How's your iOS?")

[2] (If "friend" is the right word for an all-seeing, all-knowing, all-surveilling, globe-spanning entity that knows more about what you ate for breakfast than you do.)

[3] e.g. A quick search of the ol' PyPI turns up this: <https://pypi.python.org/pypi/dateparser>. Haven't tried it myself, but its github repository is clearly active (always a good sign) and looks like it'd be worth investigating further.
_______________________________________________
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


  • Prev by Date: Re: choose file default location Documents folder ?
  • Next by Date: Re: choose file default location Documents folder ?
  • Previous by thread: Re: class reference "date" problems
  • Next by thread: "Expected class name but found application constant or consideration"...
  • Index(es):
    • Date
    • Thread