I'd like to include a button in my Mac app which, when pressed, will launch the user's default calendar app. Preferably, I'd like to have the calendar open to a certain date.
This is for OS X Mountain Lion.
Is there a general way to do this?
Edit: FWIW, this is what I'm doing now:
- (IBAction)launchCalendarApp:(id)sender
{
[[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Calendar.app"];
}
I know hardcoding the path like this is a bad idea which is why I'm asking the question.
Update: This is what I ended up doing:
- (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 sounds like all of the possible ways of doing this are workarounds until a better API is developed. My solution is similar to Jay's suggestion. I'm using the bundle identifier to get the path because I think it is a little less brittle. Apple is unlikely to change the bundle ID in the future even if they (or the user) decides to rename the app. Unfortunately, this method doesn't get me to a specific date. I will investigate further some of the other suggestions (using ical:// etc.) when I have more time.
Update 2: NSGod has a terrific answer below that also opens the Calendar to a specific date provided your app is not sandboxed.
So it sounds like for now you'd either have to resort to a hard wired approach, e.g.
// Launches Calendar.app on 10.7+
[[NSWorkspace sharedWorkspace] launchApplication:@"Calendar"];
Or use the URL scheme using what's supported by Calendar/iCal on OS X (pointed out by NSGod in comments below) similar to URL Scheme for opening the iCal app at a date or event? :
// Launches iCal (works at least with 10.6+)
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"ical://"]];