将 AppleScript 日期转换为 ASOBJC 中的 NSdate 对象?
Converting AppleScript Date into an NSdate object in ASOBJC?
所以我有一些 AppleScriptObjC 代码可以用日期做一些事情。它做的第一件事就是将今天的日期转换为 NSDate 对象。
set nowDate to current application's NSDate's |date|()
我希望能够将 nowDate
设置为我选择的任何日期。从我读到的所有内容来看,直到 10.11,这是一个非常复杂的过程。但在 10.11 中,它应该变得更容易了。我实际上找不到任何关于它如何更容易的例子。
我真正想要的是能够说
set aDate to current date
set day of aDate to "15"
set month of aDate to "5"
set year of aDate to "2020"
set hours of aDate to "13"
set minutes of aDate to "00"
set seconds of aDate to "00"
set nowDate to NSDate's aDate --or something simple like that
我还发现这个函数看起来可以将日期设置为您想要的任何日期,但我不知道如何将其转换为有用的 ASOBJC 代码(我摸索了一下,但得到了无处):
- (NSDate *)dateWithEra:(NSInteger)eraValue
year:(NSInteger)yearValue
month:(NSInteger)monthValue
day:(NSInteger)dayValue
hour:(NSInteger)hourValue
minute:(NSInteger)minuteValue
second:(NSInteger)secondValue
nanosecond:(NSInteger)nanosecondValue;
奖金问题...我们所处时代的整数值是多少?
尽管 AppleScriptObjC 可以轻松地将 NSDate 强制转换为 AppleScript 日期,但要采用其他方式就没有那么简单了。 Cocoa 有大量的日期选项,因此您需要使用一些语句来定义您想要的内容。
您的 Objective-C 方法的转换类似于(不要忘记围绕 AppleScript 术语的管道):
use framework "Foundation"
use scripting additions
tell (current date) to set {theDay, theMonth, theYear, theHours, theMinutes, theSeconds} to {its day, its month as integer, its year, its hours, its minutes, its seconds}
set theCalendar to current application's NSCalendar's currentCalendar
theCalendar's dateWithEra:1 |year|:theYear |month|:theMonth |day|:theDay hour:theHours minute:theMinutes |second|:theSeconds nanosecond:0
您还可以使用 NSDateFormatter 从 ISO 日期字符串转换:
tell ((current date) as «class isot» as string) to set dateString to it
set formatter to current application's NSDateFormatter's alloc's init
formatter's setDateFormat:"yyyy-MM-dd'T'HH:mm:ss"
# formatter's setLocale:(current application's NSLocale's alloc's initWithLocaleIdentifier:"en_US_POSIX")
formatter's dateFromString:dateString
对于您的奖金问题,这取决于所使用的日历,但公历有两个纪元,BCE/BC 和 CE/AD。从 Apple 的 Date and Time Guide,如果你为 BCE 使用负年份,则纪元为 0,否则为 1。
Although AppleScriptObjC will easily convert an NSDate to an AppleScript date, there isn't anything quite that simple for going the other way.
是一些东西非常简单:AppleScript日期在用作参数时隐式桥接到NSDate
所以你可以用
轻松转换
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
set currentDate to current date
set cocoaDate to current application's NSDate's dateWithTimeInterval:0 sinceDate:currentDate
log cocoaDate
所以我有一些 AppleScriptObjC 代码可以用日期做一些事情。它做的第一件事就是将今天的日期转换为 NSDate 对象。
set nowDate to current application's NSDate's |date|()
我希望能够将 nowDate
设置为我选择的任何日期。从我读到的所有内容来看,直到 10.11,这是一个非常复杂的过程。但在 10.11 中,它应该变得更容易了。我实际上找不到任何关于它如何更容易的例子。
我真正想要的是能够说
set aDate to current date
set day of aDate to "15"
set month of aDate to "5"
set year of aDate to "2020"
set hours of aDate to "13"
set minutes of aDate to "00"
set seconds of aDate to "00"
set nowDate to NSDate's aDate --or something simple like that
我还发现这个函数看起来可以将日期设置为您想要的任何日期,但我不知道如何将其转换为有用的 ASOBJC 代码(我摸索了一下,但得到了无处):
- (NSDate *)dateWithEra:(NSInteger)eraValue
year:(NSInteger)yearValue
month:(NSInteger)monthValue
day:(NSInteger)dayValue
hour:(NSInteger)hourValue
minute:(NSInteger)minuteValue
second:(NSInteger)secondValue
nanosecond:(NSInteger)nanosecondValue;
奖金问题...我们所处时代的整数值是多少?
尽管 AppleScriptObjC 可以轻松地将 NSDate 强制转换为 AppleScript 日期,但要采用其他方式就没有那么简单了。 Cocoa 有大量的日期选项,因此您需要使用一些语句来定义您想要的内容。
您的 Objective-C 方法的转换类似于(不要忘记围绕 AppleScript 术语的管道):
use framework "Foundation"
use scripting additions
tell (current date) to set {theDay, theMonth, theYear, theHours, theMinutes, theSeconds} to {its day, its month as integer, its year, its hours, its minutes, its seconds}
set theCalendar to current application's NSCalendar's currentCalendar
theCalendar's dateWithEra:1 |year|:theYear |month|:theMonth |day|:theDay hour:theHours minute:theMinutes |second|:theSeconds nanosecond:0
您还可以使用 NSDateFormatter 从 ISO 日期字符串转换:
tell ((current date) as «class isot» as string) to set dateString to it
set formatter to current application's NSDateFormatter's alloc's init
formatter's setDateFormat:"yyyy-MM-dd'T'HH:mm:ss"
# formatter's setLocale:(current application's NSLocale's alloc's initWithLocaleIdentifier:"en_US_POSIX")
formatter's dateFromString:dateString
对于您的奖金问题,这取决于所使用的日历,但公历有两个纪元,BCE/BC 和 CE/AD。从 Apple 的 Date and Time Guide,如果你为 BCE 使用负年份,则纪元为 0,否则为 1。
Although AppleScriptObjC will easily convert an NSDate to an AppleScript date, there isn't anything quite that simple for going the other way.
是一些东西非常简单:AppleScript日期在用作参数时隐式桥接到NSDate
所以你可以用
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
set currentDate to current date
set cocoaDate to current application's NSDate's dateWithTimeInterval:0 sinceDate:currentDate
log cocoaDate