sam
sam

Reputation: 3489

Programmatically launch Mac Calendar.app (and choose specific date) from my app?

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.

Upvotes: 4

Views: 2862

Answers (4)

NSGod
NSGod

Reputation: 22958

Note: I was still researching this while you updated with what you used, but I'll add this FWIW.

Using the bundle identifier of the application is generally a more robust way to refer to an app then using the name alone, as a user could move or rename the app in OS X, but they can't easily change the bundle identifier. Moreover, even while Apple renamed iCal.app to Calendar.app, the 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 app is sandboxed. You could potentially try to create a custom NSAppleEventDescriptor that would specify the equivalent of something like the following AppleScript code, but it will likely be denied because of the sandbox:

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

If your app doesn't have to be sandboxed, it's much easier if you use the Scripting Bridge, and with that method it's possible to select a specific NSDate.

Sample project using ScriptingBridge: OpenCalendar.zip

In that project, I use the following code:

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

That will launch Calendar.app/iCal.app and change the calendar to the specified date.

Upvotes: 8

Jay
Jay

Reputation: 6638

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://"]];

Upvotes: 4

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

You might try using EventKit to create a EKCalendarItem instance of the desired date and time, open the event, then remove it immediately thereafter. If it's well-timed, it may not even visibly blink on/off the user's calendar.

It's another kludge, but until NSWorkspace has an -openDate: method, kludges are the only recourse.

Upvotes: 1

ssantos
ssantos

Reputation: 16536

As discussed on this thread, it seems there's no url scheme to launch iCal

URL Scheme for opening the iCal app at a date or event?

Upvotes: 0

Related Questions