在Mac OSX上创建mailto处理程序应用 [英] Creating an mailto handler app on Mac OSX

查看:135
本文介绍了在Mac OSX上创建mailto处理程序应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Emacs中使用mu4e作为我的邮件客户端,但是我不知道如何创建一个可以将mailto url传递到以下shell脚本的脚本:

I am using mu4e in Emacs as my mail client, but I cannot figure out how to create a script that can pass on the mailto url to the following shell script:

#!/bin/sh
# emacs-mailto-handler

mailto=$1
mailto="mailto:${mailto#mailto:}"
mailto=$(printf '%s\n' "$mailto" | sed -e 's/[\"]/\\&/g')
elisp_expr="(mu4e~compose-browse-url-mail \"$mailto\")"

emacsclient -a \"\" --create-frame -n --eval "$elisp_expr" \
        '(set-window-dedicated-p (selected-window) t)'

当我在命令行上调用此脚本时,它将在Emacs中打开一个具有正确地址和主题的新框架:

When I call this script on my command line it opens up a new frame in Emacs with the correct address and subject:

$ emacs-mailto-handler "mailto:webmonkey@wired.com?subject=I-love-cats-too%21"

问题是我需要创建一个可以配置为Mac OSX中默认邮件客户端的应用程序. 我试图使用Automator和Platypus调用我的shell脚本,但是我无法让它们将它们接收的参数传递给shell脚本. (我看到了这个问题:

The problem is that I need to create an app that can be configured to be the default mail client in Mac OSX. I have tried to use Automator and Platypus to call my shell script, but I cannot get them to pass the argument they are receiving to the shell script. (I have see this question: OS X: how to make command-line script appear as helper application to handle mailto?, but that does not do the trick for me.)

最终,我只需要能够使该应用程序执行以下脚本调用即可: emacs-mailto-handler"mailto:webmonkey@wired.com?subject = I-love-cats-too%21" mailto链接来自浏览器.

At the end of the day I just have to be able to get the app to do this script call: emacs-mailto-handler "mailto:webmonkey@wired.com?subject=I-love-cats-too%21" where the mailto link is what is coming from the browser.

任何有关此方面的线索都将受到欢迎!

Any clues on this would be most welcome!

预先感谢, 托本

推荐答案

这将需要某种捆绑的应用.

This will require a bundled app of some sort.

当应用处理URL时,它不会在其命令行参数(main()argv参数数组)中接收到它们.实际上,该应用程序可以在其运行期间的任何时间接收到打开URL的请求,而不仅仅是在启动时.因此,肯定需要除命令行参数以外的其他机制来接收它们.这样一来,脚本就无法在其参数中接收URL.

When an app handles URLs, it does not receive them among its command-line arguments (the argv argument array of main()). In fact, the app can receive requests to open URLs at any time during its run, not just at launch. So, it definitely requires a mechanism other than command-line arguments to receive them. That precludes a script from receiving the URL in its arguments.

相反,它作为类kInternetEventClass和ID kAEGetURL的Apple Event接收打开或获取URL的请求.该应用程序为该Apple Event设置了一个处理程序,该处理程序由框架调用.为了使框架能够接收和调度Apple Event,应用程序必须a)使用这些框架,并且b)为框架提供一个机会,以监视框架在内部使用的进程间通信机制来传递事件.再说一次,这不是shell脚本可以做的.

Instead, it receives the request to open or get a URL as an Apple Event of class kInternetEventClass and ID kAEGetURL. The app sets up a handler for that Apple Event and the handler is invoked by the frameworks. In order for the frameworks to receive and dispatch the Apple Event, the app has to a) be using those frameworks, and b) provide an opportunity for the frameworks to monitor the inter-process communication mechanism they use internally to pass events around. Again, this is not something a shell script can do.

在Cocoa应用程序中,这需要在应用程序的早期启动代码中放置以下代码,例如应用程序委托的-applicationWillFinishLaunching:方法:

In a Cocoa app, this would entail putting code like the following in the early startup code of the app, such as the -applicationWillFinishLaunching: method of the app delegate:

    NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
    [appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

然后将添加一个名称与上面传递的选择器匹配的方法;在这种情况下-handleGetURLEvent:withReplyEvent::

Then one would add a method whose name matches the selector passed above; in this case -handleGetURLEvent:withReplyEvent::

- (BOOL)handleGetURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
    NSAppleEventDescriptor* directObjectDescriptor = [event paramDescriptorForKeyword:keyDirectObject];
    NSString* urlString = [directObjectDescriptor stringValue];
    NSURL* url = [NSURL URLWithString:urlString];
    // ... do something with url ...
}

除了该代码外,该应用还必须在CFBundleURLTypes键下声明其在Info.plist文件中处理特定方案的URL的能力.如下所示的条目将声明处理mailto: URL的能力:

In addition to that code, the app would have to declare its ability to handle URLs of particular schemes in its Info.plist file, under the CFBundleURLTypes key. An entry such as the following would declare the ability to handle mailto: URLs:

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>Email Address URL</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>mailto</string>
            </array>
        </dict>
    </array>

可以想象,由Platypus或Automator生成的应用程序中可能包含上述URL支持代码.那就是通用的.声明必须支持特定的URL方案.通常无法宣布对任何/所有方案的支持.

It is conceivable that the applications generated by either Platypus or Automator could have the above URL-support code in them. That much is general-purpose. Declaring support for specific URL schemes is something that they'd have to let you configure. There's no way to generally declare support for any/all schemes.

我入侵了由Automator生成的应用程序的Info.plist文件,以查看是否可以处理URL.没用但是,我正在使用由Automator从OS X 10.6生成的应用进行测试.较新版本的Automator可能会添加支持.那将解释您引用的另一个问题中报告的成功.

I hacked on the Info.plist file of an app generated by Automator to see if I could make it handle URLs. It didn't work. However, I'm testing with an app generated by Automator from OS X 10.6. It's possible that newer versions of Automator added support. That would explain the success reported in that other question you cited.

我还没有和鸭嘴兽进行过确认.

I haven't checked with Platypus.

这篇关于在Mac OSX上创建mailto处理程序应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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