在ixx上将iCal事件导出到.ics文件

时间:2012-07-20 08:07:43

标签: macos cocoa icalendar calendar-store

我需要创建将所有iCal事件导出到isc文件的应用程序。你能告诉我怎么做吗?使用Cocoa应用程序,命令行工具或AppleScript应用程序?....我试图用AppleScript应用程序,使用CalendarStore Framework。我能够获取所有事件,但如何将它们保存到ics文件??

这是我的代码:

    - (IBAction)btnSelector:(id)sender {
CalCalendarStore *calendarStore = [CalCalendarStore defaultCalendarStore];
NSDate *startDate=[[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
startDate=[formatter dateFromString:@"2010-01-01 00:00:00"];                             
NSDate *endDate=[[NSDate alloc] init];    
 endDate=[formatter dateFromString:@"2050-01-01 00:00:00"];

 NSArray *calendars = [calendarStore calendars];
    NSPredicate *allEventsPredicate = [CalCalendarStore eventPredicateWithStartDate:startDate endDate:endDate calendars:[calendarStore calendars]];

NSArray *events = [calendarStore eventsWithPredicate:allEventsPredicate]; 

for(CalEvent *event in events)
{
    //here i must save event to file...
    NSLog(@"%@",event);
    }

for (CalCalendar *calendar in calendars) {

    self.lblNotification.title=[NSString stringWithFormat:@"%@  %@",self.lblNotification.title,calendar.title];

}

 }

2 个答案:

答案 0 :(得分:2)

我采用了不同的方法 - 我使用 GUI 脚本从 Apple 日历应用程序导出指定数量的日历。它可以导出到桌面或文档文件夹,我可能可以编辑脚本以强制它导出到桌面文件夹。

https://github.com/chinwayland/applescripts/blob/master/wintec-scripts/Export%20Calendars%20to%20ICS.applescript

-- This script exports multiple calendars to ICS one by one using GUI Scripting

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set numberOfCalendars to display dialog "How many calendars?" default answer ""

tell application "Calendar"
    activate
end tell

tell application "System Events"
    tell process "Calendar"
        repeat with i from 2 to text returned of numberOfCalendars as number
            
            # Select each calendar
            tell row i of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1
                set selected to true
                delay 1
            end tell
            
            # Click Export from Menu
            tell menu item 1 of menu of menu item "Export" of menu "File" of menu bar 1
                click
                delay 3
            end tell
            
            # Click Export Button from pop up window
            tell button "Export" of sheet 1 of window 1
                click
                delay 1
            end tell
            
            
            if button "Replace" of sheet 1 of sheet 1 of window 1 exists then
                tell button "Replace" of sheet 1 of sheet 1 of window 1
                    click
                end tell
                delay 1
            end if
        end repeat
    end tell
end tell

答案 1 :(得分:1)

我解决了问题...现在我不使用CalendareStore框架,我只是阅读了日历文件夹的内容:

NSFileManager *fileManager = [[NSFileManager alloc] init];
//   /Users/username/Library/Calendars
NSError *err;

NSArray *usersFolder = [fileManager contentsOfDirectoryAtPath:@"/Users" error:&err];

for (NSString *dirElement in usersFolder) {
   // NSLog(@"users folder : %@",dirElement);

    NSString *userCalendarPath = [NSString stringWithFormat:@"/Users/%@/Library/Calendars",dirElement];
    NSArray *contentCalendarDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:userCalendarPath error:nil];
    NSArray *calendarFolders = [contentCalendarDirectory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.calendar'"]];

    NSMutableArray *calendarNames= [[NSMutableArray alloc]  init];

    for(NSString *calendarFolder in calendarFolders)
    {
       // NSLog(@"calendar folder %@/%@",userCalendarPath,calendarFolder);

        NSString * fullCalendarInfoPath = [NSString stringWithFormat:@"%@/%@/Info.plist",userCalendarPath,calendarFolder];
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: fullCalendarInfoPath];


        NSString * eventsDirectory = [NSString stringWithFormat:@"%@/%@/Events",userCalendarPath,calendarFolder];
        NSArray *contentEventsDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:eventsDirectory error:nil];
        NSArray *events = [contentEventsDirectory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.ics'"]];


        for(NSString* eventName in events)
        {
            NSString * eventPath = [[NSString alloc] initWithFormat:@"%@/%@/Events/%@",userCalendarPath,calendarFolder,eventName];
             //this array contain all ics paths...
//to read file content:
//NSString *eventContent = [[NSString alloc] initWithContentsOfFile:eventPath encoding:NSUTF8StringEncoding error:nil];
            [eventsPath addObject:eventPath];

          }
         NSString* calendarTitle = [dict objectForKey: @"Title"];

        [calendarNames addObject:[NSString stringWithFormat:@"%@  (%lu events)",calendarTitle,[events count]]];

    }
    if(calendarNames.count>0)
    { 
       NSDictionary *userCalendars = [NSDictionary dictionaryWithObjectsAndKeys:dirElement,@"parent",calendarNames,@"children", nil];
       [dataCalendars addObject:userCalendars];
       }    
         }