通过我的应用程序以编程方式启动Mac Calendar.app(并选择特定日期)? [英] Programmatically launch Mac Calendar.app (and choose specific date) from my app?

查看:225
本文介绍了通过我的应用程序以编程方式启动Mac Calendar.app(并选择特定日期)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Mac应用程式中加入一个按钮,按下时会启动使用者的预设日历应用程式。

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.

这是OS X Mountain Lion的用法。

This is for OS X Mountain Lion.

有一个通用的方法吗?

编辑
FWIW,这是我在做现在:

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];
    }
}

这听起来像所有可能的方法是解决方法,直到开发出更好的API。我的解决方案类似于Jay的建议。我使用bundle标识符来获取路径,因为我认为它有点不太脆弱。即使他们(或用户)决定重命名应用程序,苹果也不太可能在将来更改bundle ID。不幸的是,这种方法不会让我到一个特定的日期。

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.

更新2 :$

推荐答案

如果你的应用程序没有沙箱,NSGod有一个非常好的答案。所以现在你可能不得不诉诸硬连线方法,例如

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

或者使用OS X上Calendar / iCal支持的URL方案(NSGod指出)评论),类似于 URL Scheme for在日期或活动时打开iCal应用程序?

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

这篇关于通过我的应用程序以编程方式启动Mac Calendar.app(并选择特定日期)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆