gpt4 book ai didi

macos - 在 Mac OSX 上创建一个 mailto 处理程序应用程序

转载 作者:行者123 更新时间:2023-12-01 10:48:54 24 4
gpt4 key购买 nike

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

#!/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 中打开一个新框架,其中包含正确的地址和主题:

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

问题是我需要创建一个可以配置为 Mac OSX 中的默认邮件客户端的应用程序。我曾尝试使用 Automator 和 Platypus 来调用我的 shell 脚本,但我无法让它们将接收到的参数传递给 shell 脚本。 (我看到了这个问题:OS X: how to make command-line script appear as helper application to handle mailto?,但这对我没有用。)

在一天结束时,我只需要能够让应用程序执行此脚本调用: emacs-mailto-handler "mailto:webmonkey@wired.com?subject=I-love-cats-too%21"其中 mailto 链接是来自浏览器的内容。

欢迎提供任何相关线索!

提前致谢,托本

最佳答案

这将需要某种捆绑应用。

当应用程序处理 URL 时,它不会在其命令行参数(main()argv 参数数组)中接收它们。事实上,应用程序可以在运行期间随时接收打开 URL 的请求,而不仅仅是在启动时。因此,它肯定需要一种不同于命令行参数的机制来接收它们。这会阻止脚本在其参数中接收 URL。

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

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

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

然后添加一个名称与上面传递的选择器相匹配的方法;在这种情况下 -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 ...
}

除了该代码之外,应用程序还必须在其 Info.plist 文件中声明其处理特定方案的 URL 的能力,该文件位于 CFBundleURLTypes 键下。如下所示的条目将声明处理 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 方案的支持是他们必须让您配置的东西。没有办法普遍声明支持任何/所有方案。

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

我还没有检查过 Platypus。

关于macos - 在 Mac OSX 上创建一个 mailto 处理程序应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22848223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com