From eedc849c45063ea63d456d5b537c81770bc15a18 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 6 Feb 2022 20:49:40 +0530 Subject: [PATCH] Cleanup previous PR --- docs/open_actions.rst | 14 ++++++++------ kitty/cocoa_window.m | 9 ++++----- kitty/fast_data_types.pyi | 4 +++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/docs/open_actions.rst b/docs/open_actions.rst index 67b10b57f..58665a1ce 100644 --- a/docs/open_actions.rst +++ b/docs/open_actions.rst @@ -120,15 +120,17 @@ 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.) +Since macOS lacks an official interface to set default URL scheme handlers, +kitty has a command you can use for it. The first argument for is the URL +scheme, and the second optional argument is the bundle id of the app, which +defaults to kitty, if not specified. For example: .. code-block:: sh - kitty +runpy 'from kitty.fast_data_types import cocoa_set_url_handler; print(cocoa_set_url_handler("ssh", "net.kovidgoyal.kitty"));' + # Set kitty as the handler for ssh:// URLs + kitty +runpy 'from kitty.fast_data_types import cocoa_set_url_handler; import sys; cocoa_set_url_handler(*sys.argv[1:]); print("OK")' ssh + # Set someapp as the handler for xyz:// URLs + kitty +runpy 'from kitty.fast_data_types import cocoa_set_url_handler; import sys; cocoa_set_url_handler(*sys.argv[1:]); print("OK")' xyz someapp.bundle.identifier You can customize these actions by creating a :file:`launch-actions.conf` file in the kitty config directory, just like diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index c9169be2f..ddb6d12e9 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -846,11 +846,10 @@ cocoa_set_url_handler(PyObject UNUSED *self, PyObject *args) { 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; - + OSStatus err = LSSetDefaultHandlerForURLScheme((CFStringRef)scheme, (CFStringRef)identifier); + if (err == noErr) Py_RETURN_NONE; + PyErr_Format(PyExc_OSError, "Failed to set default handler with error code: %d", err); + return NULL; } // autoreleasepool } diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index dc2f3e924..ca72397f4 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -737,9 +737,11 @@ 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: + +def cocoa_set_url_handler(url_scheme: str, bundle_id: Optional[str]) -> None: pass + def locale_is_valid(name: str) -> bool: pass