Programmatically run Mac Calendar.app (and select a specific date) from my application?

I would like to include a button in my Mac app that, when clicked, launches the default user calendar app. It is advisable that the calendar be open before a certain date.

This is for OS X Mountain Lion.

Is there a general way to do this?

Edit : FWIW, here is what I am doing now:

- (IBAction)launchCalendarApp:(id)sender { [[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Calendar.app"]; } 

I know the hardcoding way, as this is a bad idea, so I ask a question.

Update : This is what I ended up with:

 - (IBAction)launchCalendarApp:(id)sender { NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace]; NSString *iCalPath = [sharedWorkspace absolutePathForAppBundleWithIdentifier:@"com.apple.iCal"]; BOOL didLaunch = [sharedWorkspace launchApplication:iCalPath]; if (didLaunch == NO) { NSString *message = NSLocalizedString(@"The Calendar application could not be found.", @"Alert box message when we fail to launch the Calendar application"); NSAlert *alert = [NSAlert alertWithMessageText:message defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@""]; [alert setAlertStyle:NSCriticalAlertStyle]; [alert runModal]; } } 

It seems like all the possible ways to do this are workarounds to developing a better API. My solution is similar to Jay's suggestion. I use the package identifier to get the path, because I think it is a little less fragile. Apple is unlikely to change the package identifier in the future, even if they (or the user) decide to rename the application. Unfortunately, this method does not lead me to a specific date. Further I will study some other sentences (using ical: // etc.), When I have more time.

Update 2 : NSGod has a terrific answer below, which also opens a calendar for a specific date if your application is not isolated.

+4
source share
4 answers

So, it seems that now you have to either resort to a hard wired approach, for example,

 // Launches Calendar.app on 10.7+ [[NSWorkspace sharedWorkspace] launchApplication:@"Calendar"]; 

Or use a URL scheme using what Calendar / iCal supports on OS X (indicated by NSGod in the comments below), similar to a URL scheme for opening an iCal application for a date or event? :

 // Launches iCal (works at least with 10.6+) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"ical://"]]; 
+4
source

Note. I was still learning this while you were updating with what you used, but I will add this FWIW.

Using the bundle identifier of an application is usually a more reliable way to access the application and then using only the name, since the user can move or rename the application in OS X, but they cannot easily change the package identifier. Moreover, even when Apple renamed iCal.app in Calendar.app, CFBundleIdentifier is still com.apple.iCal .

 if (![[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:@"com.apple.iCal" options:NSWorkspaceLaunchDefault additionalEventParamDescriptor:nil launchIdentifier:NULL]) { NSLog(@"launching Calendar.app failed!"); } 

The above code will work even if your application is isolated. You could try creating a custom NSAppleEventDescriptor that would indicate the equivalent of something like the following AppleScript code, but would most likely fail because of the sandbox:

 view calendar at date "Sunday, April 8, 2012 4:28:43 PM" 

If your application does not need to be isolated, it is much easier if you use Scripting Bridge, and with this method you can select a specific NSDate .

Sample project using ScriptingBridge: OpenCalendar.zip

In this project, I use the following code:

 SBCalendarApplication *calendarApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.iCal"]; [calendarApp viewCalendarAt:[self.datePicker dateValue]]; 

Calendar.app/iCal.app launches and changes the calendar to the specified date.

+8
source

You can try using EventKit to create an EKCalendarItem instance of the desired date and time, open the event, and then delete it right after that. If it is timely, it may not even noticeably enable / disable the user's calendar.

This is another kludge, but so far NSWorkspace does not have the -openDate method: kludges is the only resource.

+1
source

As discussed in this thread, there seems to be no url scheme to run iCal

URL scheme for opening iCal application in date or event?

0
source

All Articles