• 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: Sat, 22 Aug 2015 10:19:35 +1000

On 22 Aug 2015, at 7:54 am, Stockly, Ed <email@hidden> wrote:

I have a confession to make.

After testing AppleScriptObjC stuff, I never looked at it again. It seemed
pretty cool, but beyond the scope of what I had time and need for.

I'm sure there are lots of people in the same boat. It is a fairly big initial leap. (But then, wasn't AppleScript itself for most of us all those years ago?) 

However, if I could get data detectors back, even if it was just for
dates, that would be worth it.

This dead horse you've flogged has convinced me to take the leap.

Where do I start?

Axel has listed some resources, but I honestly don't know that they are a great place to *start*. The initial release notes he points to are old, as he says, and they were intended for people writing ASObjC projects in Xcode. That's probably not what you're trying to do, and although the language is obviously the same, you can ignore a lot of what little there is. The AppleScript release notes are useful, as they are for all scripters really. But the Objective-C stuff is probably a bit much to begin with unless you have some kind of programming background.

To my mind, there are two approaches. One is just copying-and-pasting code that looks interesting, trying it, and asking questions. I think that's how a lot of us started in AppleScript -- I know I did. It's a little more difficult because ASObjC code doesn't involve sending Apple events (which helps make it fast), and no Apple events means nothing appears in a typical script editor's log. When stuff does appear, Cocoa objects show up as things like «class ocid» id «data optr00000000F0A93556CF7F0000», which isn't particularly enlightening. Sources for such code are here, obviously, and also macscripter.net. Both are also good places to ask questions.

The second thing I'd recommend will sound like a sales pitch, but it's my book 'Everyday AppleScriptObjC'. Yes, unfortunately it costs money, although not a lot ($US15). But I can safely say it's the best book on the subject -- because it's the only one :-). It tries to take a methodical approach, and it's written very much from a scripter's perspective. A third edition will be coming out with the release of El Capitan, and anyone who buys the second version now will get a free update.

Finally, I realised that data-detectors code could be tweaked to be a bit shorter and faster, especially if you're only looking for one date in a string:

use framework "Foundation"
use script "BridgePlus" -- used here to convert between Cocoa dates and AS dates pre-10.11
on getDatesIn:aString
-- convert string to Cocoa string
set anNSString to current application's NSString's stringWithString:aString
-- create data detector
set theDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeDate) |error|:(missing value)
-- find first match in string; returns an NSTextCheckingResult object
set theMatch to theDetector's firstMatchInString:anNSString options:0 range:{0, anNSString's |length|()}
-- get the date property of the NSTextCheckingResult
set theDate to theMatch's |date|()
return theDate
end getDatesIn:

set theDates to ASify from (my getDatesIn:"2015-08-20 15:24:11") -- 'ASify from' converts from Cocoa object to AS object

And if you're serious and either have a few dollars to spare or you're doing it for your business, let me plug my editor ASObjC Explorer. It sounds odd to use a another editor, but it actually logs what's happening, and no other editor does. It also offers code-completion for Cocoa terminology, and as you have probably noticed that can be somewhat verbose, so anything that avoids typos can save a lot of time.

The above code shows this in the log when run from Script Editor:

Result:

date "Thursday, 20 August 2015 3:24:11 pm"

Run with logging in ASObjC Explorer, you get this:

0000.000 -- Start --
0000.001 [5] set anNSString to current application's NSString's stringWithString:aString
  --> (NSString) "2015-08-20 15:24:11"
0000.002 [7] set theDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeDate) |error|:(missing value)
  --> (NSDataDetector) <NSDataDetector: 0x608000c7a140> (null) 0x0 0x8
0000.005 [9] set theMatch to theDetector's firstMatchInString:anNSString options:0 range:{0, anNSString's |length|()}
  --> (NSDateCheckingResult) <NSDateCheckingResult: 0x610000096670>{0, 19}{2015-08-20 05:24:11 +0000}
0000.005 [11] set theDate to theMatch's |date|()
  --> (NSDate) 2015-08-20 05:24:11 +0000
0000.006 [15] set theDates to ASify from (my getDatesIn:"2015-08-20 15:24:11") -- 'ASify from' converts from Cocoa object to AS object
  --> date "Thursday, 20 August 2015 3:24:11 pm"

Result of Main Script:
  --> date "Thursday, 20 August 2015 3:24:11 pm"

(And you can try it free for 30 days. End of blatant plug.)

-- 
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: Shane Stanley <email@hidden>
    • 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>)
 >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>)
 >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