macos 如何将自定义协议映射到 Mac 上的应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/471581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:01:09  来源:igfitidea点击:

How to map a custom protocol to an application on the Mac?

macosprotocolsprotocol-handlercustom-url-protocol

提问by cliff.meyers

I'm trying to register a custom protocol to an application on the Mac, i.e:

我正在尝试将自定义协议注册到 Mac 上的应用程序,即:

  1. User clicks on link with "abcdef://some/url/here"
  2. An installed application is launched with the above string passed as the first param
  1. 用户点击带有“abcdef://some/url/here”的链接
  2. 已安装的应用程序以上述字符串作为第一个参数启动

I've done this successfully on Windows using the information from this question:

我已经使用这个问题中的信息在 Windows 上成功完成了这项工作:

how do I create my own URL protocol? (e.g. so://...)

如何创建自己的 URL 协议?(例如所以://...)

I would prefer to find something that is browser-independent, in other words at the OS level. I would also like to automate this registration through a shell script, so hopefully there is a way to do this that doesn't involve the GUI.

我更愿意找到与浏览器无关的东西,换句话说,是在操作系统级别。我还想通过 shell 脚本自动进行此注册,因此希望有一种不涉及 GUI 的方法。

Thanks!

谢谢!

采纳答案by Jay

I've not had occasion to use it, but some time ago I bookmarked OS X URL handler to open links to local fileswhich is exactly what you're looking for.

我没有机会使用它,但前段时间我为OS X URL 处理程序添加了书签以打开指向本地文件的链接,这正是您要查找的内容。

The important part of the linked procedure is adding an appropriate CFBundleURLTypesto your application's Info.plist that describes the scheme. The example given there looks like this:

链接过程的重要部分是向CFBundleURLTypes您的应用程序的 Info.plist添加一个适当的描述方案的信息。那里给出的示例如下所示:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Local File</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>local</string>
        </array>
    </dict>
</array>

回答by Turadg

On Macs this is easy to do with AppleScript. The most detailed description is in this article, Launch Scripts from Webpage Links on Mac. I'd read that page since it includes a full walk-through and a full working example to download.

在 Mac 上,使用 AppleScript 很容易做到这一点。最详细的描述在这篇文章中,从 Mac 上的网页链接启动脚本。我会阅读该页面,因为它包含完整的演练和完整的可下载示例。

Basically, you make an event handler in a script:

基本上,您在脚本中创建一个事件处理程序:

on open location this_URL
    display dialog "I'm doing something with this URL: " & return & this_URL
end open location

Then save that as an Application. Then in the Finder use Show Package Contents to edit the Info.plist. You add some properties to the app to register it as a handler for your protocol.

然后将其另存为应用程序。然后在 Finder 中使用 Show Package Contents 来编辑 Info.plist。您向应用程序添加一些属性以将其注册为协议的处理程序。

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Cliff's handler</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>abcdef</string>
        </array>
    </dict>
</array>

回答by SystematicFrank

As of today, the best way I found to solve this problem on a Mac with the least overhead is using the command line dutiwhich allows me to define in a very simple text file all my associations:

截至今天,我发现在 Mac 上以最少的开销解决此问题的最佳方法是使用命令行duti,它允许我在一个非常简单的文本文件中定义所有关联:

brew install duti

酿造安装duti

You will need two things. First bundle ids of the Apps you want to associate:

您将需要两件事。您要关联的应用程序的第一个包 ID:

mdls -name kMDItemCFBundleIdentifier /Applications/MacVim.app

mdls -name kMDItemCFBundleIdentifier /Applications/MacVim.app

Second the UTI of the file type, Apple provides a list, but you can also explore the supported UTI by your app like this:

其次是文件类型的 UTI,Apple 提供了一个 list,但您也可以通过您的应用程序探索支持的 UTI,如下所示:

mdls -name kMDItemContentTypeTree /Applications/MacVim.app

mdls -name kMDItemContentTypeTree /Applications/MacVim.app

Now make a text file somewhere in your system where you associate bundle ids with UTI:

现在在系统中的某处创建一个文本文件,将捆绑包 ID 与 UTI 关联起来:

# ~/.default-apps.duti
#
# bundle id       UTI                  role
com.apple.Safari  public.html          all
org.vim.MacVim    txmt
org.vim.MacVim    public.ruby-script

Notice that I can associate a URL handler like txmt and also file types like Ruby scripts.

请注意,我可以关联像 txmt 这样的 URL 处理程序和像 Ruby 脚本这样的文件类型。

In that file you I keep track of all my app preferences and reproduce them immediately after a complete fresh install or when getting an account on other Mac just running:

在该文件中,我会跟踪我的所有应用首选项,并在完全全新安装后或在其他 Mac 上获取帐户时立即重现它们正在运行:

duti ~/.default-apps.duti

回答by Mark Thomson

This question is a decade old(!) but a Google search brought me here so I wanted to mention something I just discovered.

这个问题已经有十年历史了(!)但是谷歌搜索把我带到了这里,所以我想提一下我刚刚发现的东西。

Platypusis an open source tool that allows you to create standalone "Applications" from a shell script or other scripting language. Although it's really just a script wrapper, it does enable some cool things like dialog boxes and menu bar items.

Platypus是一个开源工具,允许您从 shell 脚本或其他脚本语言创建独立的“应用程序”。尽管它实际上只是一个脚本包装器,但它确实启用了一些很酷的东西,例如对话框和菜单栏项。

Critically, it even enables you to register your "app" as a handler for your own custom URL scheme. From the docs:

至关重要的是,它甚至可以让您将“应用程序”注册为您自己的自定义 URL 方案的处理程序。从文档:

Register as URI scheme handler makes the app register as a handler for URI schemes. These can be either standard URI schemes such as http or a custom URI schemes of your choice (e.g. myscheme://). If your app is the default handler for a URI scheme, it will launch open every time a URL matching the scheme is opened. The URL is then passed to the script as an argument.

注册为 URI 方案处理程序使应用程序注册为 URI 方案的处理程序。这些可以是标准的 URI 方案,例如 http,也可以是您选择的自定义 URI 方案(例如 myscheme://)。如果您的应用程序是 URI 方案的默认处理程序,则每次打开与该方案匹配的 URL 时,它都会启动 open。然后将该 URL 作为参数传递给脚本。

Setup is dead simple. Just provide your script, enter your desired scheme name on the advanced settings page and then click to build the app (it's all automated). Everything after the scheme and forward slashes will be passed as the argument to your script.

设置非常简单。只需提供您的脚本,在高级设置页面上输入您想要的方案名称,然后单击以构建应用程序(这都是自动化的)。方案和正斜杠之后的所有内容都将作为参数传递给您的脚本。

For example, you could use the following bash script as a handler for the "speak://" protocol.

例如,您可以使用以下 bash 脚本作为“speak://”协议的处理程序。

#!/usr/bin/env bash
# The 'say' command on macOS will speak the provided text through the speaker
say 

You could invoke it by entering speak://say-something-funnyinto your browser or by using the opencommand on the command line:

您可以通过进入speak://say-something-funny浏览器或使用open命令行上的命令来调用它:

$ open "speak://hello-from-the-command-line"

回答by Matthew Schinckel

The important part of the linked page in Jay's answer is the entry in the Info.plist.

Jay 回答中链接页面的重要部分是 Info.plist 中的条目。

I think with Launch Services it will automatically open this app if it is the only one that can handle a particular URL scheme, else you'll need to use the trick that Charlie Martin describes.

我认为使用 Launch Services,如果它是唯一可以处理特定 URL 方案的应用程序,它将自动打开此应用程序,否则您将需要使用 Charlie Martin 描述的技巧。

I'm not sure what the defaults command that needs to be executed is, or if it is a launchctl command.

我不确定需要执行的默认命令是什么,或者它是否是一个 launchctl 命令。

回答by Charlie Martin

Digging up the details is difficult, but there is a preference pane called RCDefaultAppthat will handle it for you. I'd still love to know how it works, will continue digging.

挖掘细节很困难,但有一个名为RCDefaultApp的首选项窗格可以为您处理。我仍然很想知道它是如何工作的,将继续挖掘。