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

好久不见. 提交于 2019-11-26 06:30:41

问题


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

  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

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

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

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.

Thanks!


回答1:


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

The important part of the linked procedure is adding an appropriate CFBundleURLTypes to your application's Info.plist, describing the scheme. The example given there looks like this:

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



回答2:


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.

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.

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



回答3:


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

brew install duti

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

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:

mdls -name kMDItemContentTypeTree /Applications/MacVim.app

Now make a text file somewhere in your system where you associate bundle ids with 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.

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:

duti ~/.default-apps.duti



回答4:


The important part of the linked page in Jay's answer is the entry in the 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.

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




回答5:


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




回答6:


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

Platypus is 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.

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

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.

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.

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

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

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


来源:https://stackoverflow.com/questions/471581/how-to-map-a-custom-protocol-to-an-application-on-the-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!