自动化Javascript(JXA)“ whose()”中的比较/丰富查询
作者:互联网
如果我想浏览OS X日历(以前称为“ iCal”)以查找摘要为“午餐”的事件,则可以执行以下操作:
var eventsContainer = Application('Calendar').calendars[0].events
for (var i = 0; i < eventsContainer.length; i++) {
var thisEvent = eventsContainer[i];
if (thisEvent.summary() == 'Lunch') { doSomething() }
}
即使考虑到只搜索第一个日历这一事实,它也非常非常非常慢,因为每个iCal事件都需要转换为Javascript对象才能运行.这是一个更快的公式:
var foundEvents = Application('Calendar').calendars.events.whose({summary: 'Lunch'});
这对于完全匹配的摘要==’午餐’非常有用.但是,诸如endDate:>之类的比较呢?新的Date()或摘要:/ lunch / i?是否可以将本机(ObjC)代码传递到who()选择器中?是否有任何文档对谁的who()有帮助?
解决方法:
相关文档位于OS X 10.10的应用程序间通信的发行说明中:https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html
其who()参数中对象的右侧可以采用另一个单元素对象,其中左侧为运算符,右侧为一个值.例如:
.calendars.events.whose({summary: {_beginsWith: 'Lunch'}});
或者以我为例,查找从今天开始的事件:
var startOfDay = new Date();
startOfDay.setHours(0);
startOfDay.setMinutes(0);
startOfDay.setSeconds(0);
startOfDay.setMilliseconds(0);
var endOfDay = new Date();
endOfDay.setHours(23);
endOfDay.setMinutes(59);
endOfDay.setSeconds(59);
endOfDay.setMilliseconds(999);
var events = Application('Calendar').calendars.events.whose({
_and: [
{ startDate: { _greaterThan: startOfDay }},
{ startDate: { _lessThan: endOfDay }}
]
});
var convertedEvents = events();
for (var cal of convertedEvents) {
for (var ev of cal) {
console.log(ev.summary());
}
}
标签:objective-c,icalendar,javascript-automation,javascript,macos 来源: https://codeday.me/bug/20191118/2028545.html