macOS: Add a way to set kitty as the default handler for the URL schemes

This commit is contained in:
pagedown 2022-02-06 19:51:39 +08:00
parent f047678711
commit e31ca68875
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB
3 changed files with 38 additions and 0 deletions

View File

@ -120,6 +120,16 @@ These actions can also be executed from the command line by running::
or
kitty +open file_or_url ...
Since macOS lacks an official interface to set default URL scheme handler, you
can set it with the following command. The first argument for
``cocoa_set_url_handler`` is the URL scheme, and the second optional argument is
the bundle id of the app, which defaults to kitty's. (Restores when the second
argument is an empty string.)
.. code-block:: sh
kitty +runpy 'from kitty.fast_data_types import cocoa_set_url_handler; print(cocoa_set_url_handler("ssh", "net.kovidgoyal.kitty"));'
You can customize these actions by creating a :file:`launch-actions.conf` file
in the kitty config directory, just like
the :file:`open-actions.conf` file above. For example:

View File

@ -829,6 +829,31 @@ - (BOOL)openFileURLs:(NSPasteboard*)pasteboard
} // autoreleasepool
}
static PyObject*
cocoa_set_url_handler(PyObject UNUSED *self, PyObject *args) {
@autoreleasepool {
const char *url_scheme = NULL, *bundle_id = NULL;
if (!PyArg_ParseTuple(args, "s|z", &url_scheme, &bundle_id)) return NULL;
if (!url_scheme || url_scheme[0] == '\0') Py_RETURN_FALSE;
NSString *scheme = [NSString stringWithUTF8String:url_scheme];
NSString *identifier = @"";
if (!bundle_id) {
identifier = [[NSBundle mainBundle] bundleIdentifier];
if (!identifier || identifier.length == 0) identifier = @"net.kovidgoyal.kitty";
} else if (bundle_id[0] != '\0') {
identifier = [NSString stringWithUTF8String:bundle_id];
}
// This API has been marked as deprecated. It will need to be replaced when a new approach is available.
if (LSSetDefaultHandlerForURLScheme((CFStringRef)scheme, (CFStringRef)identifier) == noErr) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
} // autoreleasepool
}
static NSSound *beep_sound = nil;
static void
@ -878,6 +903,7 @@ - (BOOL)openFileURLs:(NSPasteboard*)pasteboard
{"cocoa_set_global_shortcut", (PyCFunction)cocoa_set_global_shortcut, METH_VARARGS, ""},
{"cocoa_send_notification", (PyCFunction)cocoa_send_notification, METH_VARARGS, ""},
{"cocoa_set_notification_activated_callback", (PyCFunction)set_notification_activated_callback, METH_O, ""},
{"cocoa_set_url_handler", (PyCFunction)cocoa_set_url_handler, METH_VARARGS, ""},
{NULL, NULL, 0, NULL} /* Sentinel */
};

View File

@ -737,6 +737,8 @@ def cocoa_set_global_shortcut(name: str, mods: int, key: int) -> bool:
def cocoa_get_lang() -> Optional[str]:
pass
def cocoa_set_url_handler(url_scheme: str, bundle_id: Optional[str]) -> bool:
pass
def locale_is_valid(name: str) -> bool:
pass