Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS "Open With kitty.app" #4460

Closed
cole-wilson opened this issue Jan 6, 2022 · 27 comments
Closed

macOS "Open With kitty.app" #4460

cole-wilson opened this issue Jan 6, 2022 · 27 comments

Comments

@cole-wilson
Copy link

cole-wilson commented Jan 6, 2022

Is your feature request related to a problem? Please describe.
This feature request is not related to a problem.

Describe the solution you'd like
I would like to be able to set kitty as the default opener for certain file types (like *.txt files). Once kitty has "opened" the document, it would pass the filename(s) off to a program of the user's choice (like their $EDITOR).

For example:
1. A user opens Finder on MacOS
2. The user "double clicks" a text file
3. kitty would then launch vim %s in a new window or tab, substituting the path

This feature likely be useful for Linux as well, although I am only a macOS user.

Describe alternatives you've considered
I have tried bundling my own custom *.apps that will launch kitty themselves, but they are very cumbersome to use and require XCode. In addition, they meet Gatekeeper issues with launching external executables. I believe that this feature would be a good addition to kitty, rather than using eternal tools.

Example Screenshot:
image

@cole-wilson cole-wilson changed the title macOS "Open With Kitty" macOS "Open With kitty.app" Jan 6, 2022
@cole-wilson
Copy link
Author

Note (if easy enough to develop): It would also be great if custom file icons could be applied (such as the Chrome icon on the .html file) by users.

@kovidgoyal
Copy link
Owner

This is very much macOS specific, on Linux you can just create a wrapper
script or .desktop file and use it to launch files with any program and
any command line you like.

It's only Apple that makes this difficult. As such I am not very
motivated to implement this. Having kitty interpret command line
arguments as files to open instead of programs to run has a cost, it
makes command line processing more difficult to reason about.

That said, one probably can detect files passed to kitty via launch services on
macOS and special case them.

@page-down
Copy link
Contributor

page-down commented Jan 7, 2022

(Reply before the latest changes.)

It looks like the code is already there.

kitty/child-monitor.c

process_cocoa_pending_actions(void) {
    // ...
    if (cocoa_pending_actions_data.open_files_count) {
        // ...

While this feature looks great, I also think the potentially inconsistent behavior will cause confusion.

For example, for a .sh file, should kitty execute it directly, or open it in a text editor? For different types of files, should kitty open them as configured in open-actions?

For now open the .sh file and kitty will execute it in a new window.


@kovidgoyal

I found an issue where text.txt, which does not have execute permission, will be executed when opened with kitty.app. Do you think this is an issue that needs to be addressed?

(Tested before the latest commit.)

Also, to get kitty.app to appear in the "Open With" list, it also requires registration of various file types in macOS for kitty.

@page-down
Copy link
Contributor

page-down commented Jan 7, 2022

I tested the latest commit, which resulted in not being able to run the .sh file with kitty.

<key>CFBundleDisplayName</key>
<string>kitty</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
	<key>CFBundleTypeExtensions</key>
	<array>
		<string>command</string>
		<string>sh</string>
		<string>zsh</string>
		<string>bash</string>
		<string>fish</string>
		<string>tool</string>
	</array>

@kovidgoyal
Copy link
Owner

see the next commit

@page-down
Copy link
Contributor

I tested the latest commit and .sh is executed with zsh.

#!/usr/bin/env bash
echo "${BASH_VERSION}"

Some open source projects have .command files that cannot be executed.

#!/usr/bin/env python
import os
# ...

Also for file type registration, I don't know how to register all file types yet. Since the file types are not registered, it is not possible to drag a text file to the kitty Dock icon to open it. And of course it doesn't appear in the "open with" list yet.

Also do you think it's worth checking the executable permissions of the file? Then execute it or open it with a text editor.

@kovidgoyal
Copy link
Owner

Executable permissions are already checked. And shell scripts are launched with the default shell unless they have a defined file extension such as .zsh, .bash or .fish.

@page-down
Copy link
Contributor

Strange, currently I am trying to open a .sh file with no execute permissions and it will be executed directly.

@kovidgoyal
Copy link
Owner

Yes, like I said, script files will be opened with the default shell,
unless they are executable, in which case they are executed directly.

@page-down
Copy link
Contributor

page-down commented Jan 7, 2022

Oh, it's pretty clearly that I missed the beginning of the open_file(). Thanks for the reminder.

Do you have any good ideas for files like Makefile, LICENSE, setup.cfg, shell.nix, etc.? Currently these files cannot be opened.

The file: setup.cfg is of unknown type, cannot open it.

For example, is it possible to prefer user-configured open-actions.

open-actions.conf

protocol file
ext nix 
action launch --type os-window vi ${FILE_PATH}

EDIT: Not really, since this is actually dedicated to hyperlinks, where the GUI program will be configured to open the file.

@kovidgoyal
Copy link
Owner

kovidgoyal commented Jan 7, 2022 via email

@page-down
Copy link
Contributor

... You can add mime types in the mime.types file in the kitty config directory.

I forgot about this feature.

text/plain nix Makefile

I tried it and the nix file opens fine, but the Makefile still doesn't work. Is there a good way to handle this?

@page-down
Copy link
Contributor

Also are there any security issues?

  • Name a file cat.jpg with the actual contents as an executable binary or script. Offer it to the victim for download.
  • Guide the victim to save to a special file system location, to make this file with executable permissions. (For example, some macOS shared volumes are mounted with file permissions of 777 and cannot be modified.)
  • The victim opens it with kitty, at this point the payload is successfully executed.

I did not test the above.

To be honest, I'm really not sure I like this approach. : /

@kovidgoyal
Copy link
Owner

kovidgoyal commented Jan 7, 2022 via email

@page-down
Copy link
Contributor

page-down commented Jan 7, 2022

Since before, kitty would unconditionally try to execute anything passed to it.

Previously kitty was not registered as an image viewer and was not so easily used. If the user explicitly opens with kitty at the command line, then this is his own problem.

I can think of one more scenario.

  • Clone the repository using git clone with permissions, where the file will not carry the macOS quarantine flag.
  • The cat.jpg file has execute permissions in git.
  • When opened with kitty, it will be executed. However, the user thinks that kitty will act as an image viewer and just display the image.

At this point kitty has two identities at the same time, a picture viewer that does not execute code, or a terminal with full privileges.

Hopefully, the user will understand what he is doing.

Maybe I am overthinking it, if you are not worried then I have no problem with it.

@kovidgoyal
Copy link
Owner

well, I dont actually mind not having the open_file() function execute
things. It means you can drag exectuables to kitty or open then directly
with kitty from finder, but there is not much point to doing that
anyway.

@cole-wilson
Copy link
Author

Thank you very much for the quick fix! I really appreciate kitty, and this makes it a lot better for me.

@page-down
Copy link
Contributor

Thank you. I tested the latest changes and all the issues I mentioned have been resolved.

Perhaps the last one:
I tested after uninstalling imagemagick and the error message flashed (window closed), the user may not know what went wrong and how to fix it.

Not a big deal, but it would be nice if --hold (and perhaps all places where hold is used) could be configured to hold only when exiting with a non-zero. This allows to use launch --hold even when running icat, and does not conflict with icat's hold.

@kovidgoyal
Copy link
Owner

kovidgoyal commented Jan 8, 2022 via email

@page-down
Copy link
Contributor

I tested after uninstalling imagemagick ...

I dont follow, can you explain.

# from `def default_launch_actions()`:
action launch --type=os-window kitty +kitten icat --hold $FILE_PATH

When imagemagick is not installed, kitten icat exits after outputting an error message and the OS window is closed immediately. Here is what I see with launch --hold.

ImageMagick is required to process images

Press Enter to exit

If you want a hold dependent on exit code that would need to be a separate option.

Yes, it makes perfect sense.

@ahmedelgabri
Copy link

@kovidgoyal thanks for this feature.

I have a question & a problem.

Question:

Do I need to add all extensions/files I want to be opened to mime.types file? I'm on macOS & some of the files I want to open already have text/plain when I check with file -I <file>


Problem:

I have this inside my open-actions.conf

protocol file
mime text/*
action launch --type=overlay $EDITOR $FILE_PATH

And my $EDITOR is set to nvim, and I have some plugins & configs that depends on some programs being in my $PATH. But when I double click a file, I get errors that neovim can't find the binary in my $PATH. Although, when I run r! which <binary> it returns the correct path. While inside normal kitty panes/tabs everything works fine. So I'm not sure what's the difference here?

@ahmedelgabri
Copy link

I also have

protocol file
mime image/*
action launch --type=overlay kitty +kitten icat --hold ${FILE_PATH}

And have ImageMagic installed, yet it errors out saying

ImageMagick is required to process images

Press Enter to exit

@page-down
Copy link
Contributor

Do I need to add all extensions/files I want to be opened to mime.types file? I'm on macOS & some of the files I want to open already have text/plain ...

If it is already recognized in the system, then no special configuration is needed.

... open-actions.conf

Note that this feature is not related to open-actions.conf. It's launch-actions.conf. And there are some default configurations already in place.

https://github.com/kovidgoyal/kitty/blob/v0.24.2/kitty/open_actions.py#L222-L245

... my $EDITOR is set to nvim ...
... have ImageMagic installed, yet it errors out saying ...

Where did you configure it? What you need to configure is kitty's environment variables, and configuring them in shell rc is of course invalid. Because at this point neovim will be executed directly by kitty and has nothing to do with the shell.

https://sw.kovidgoyal.net/kitty/faq/#things-behave-differently-when-running-kitty-from-system-launcher-vs-from-another-terminal

debug kitty's env vars:

map f1 show_kitty_env_vars

It is not easy to configure environment variables for GUI software under macOS. You can add the paths of your other plugins to $PATH by using exe_search_path.

https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.exe_search_path

This should solve all your program execution problems.

@ahmedelgabri
Copy link

Note that this feature is not related to open-actions.conf. It's launch-actions.conf.

Not sure that this is correct, all examples in the docs are mentioning open-actions.conf https://sw.kovidgoyal.net/kitty/kittens/hyperlinked_grep/?highlight=open%20actions%20conf#hyperlinked-grep unless I'm missing something.

Where did you configure it? What you need to configure is kitty's environment variables, and configuring them in shell rc is of course invalid. Because at this point neovim will be executed directly by kitty and has nothing to do with the shell.

https://sw.kovidgoyal.net/kitty/faq/#things-behave-differently-when-running-kitty-from-system-launcher-vs-from-another-terminal

debug kitty's env vars:

map f1 show_kitty_env_vars

It is not easy to configure environment variables for GUI software under macOS. You can add the paths of your other plugins to $PATH by using exe_search_path.

https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.exe_search_path

This should solve all your program execution problems.

Thanks, it's a bit unfortunate though because this means extra work to keep everything in sync (shell rc & kitty config).

@page-down
Copy link
Contributor

... unless I'm missing something ...

The one you gave is for configuring the behavior of clicking on links within kitty.

However, this post is about a new feature in 0.24.2 to open files with kitty under macOS via open with. This feature is configured using launch-actions.conf .

@ahmedelgabri
Copy link

Ok, I have been playing with different options for the last hour with no results.

  • Seems like kitty is still picking up $EDITOR from my shell env somehow even if I set env EDITOR=nvim --noplugin inside kitty.conf, I tried also env EDITOR="nvim --noplugin", doesn't even show in the output of show_kitty_env_vars
  • To set my expectations correctly, opening files from within kitty should or shouldn't pick up shell rc env variables? because it seems that it's not picking them up too.

Example using GNU ls, when I run ls --hyperlink=auto & then click on a file, I expect that this will pick up my shell environment properly. But it's not, I get the same errors when I open files using "Open with".

@page-down
Copy link
Contributor

  • Click on hyperlink within kitty -> open-actions.conf
  • Right-click on the file in macOS, select Open With and choose kitty -> launch-actions.conf
  • env in kitty.conf
    • The environment variables configured via env in the configuration file are for the programs running in kitty, not for kitty itself.
    • Parsing of {open,launch}-actions.conf (expanding env vars) is done in kitty itself.
  • Environment variables in *-actions.conf
    • All environment variables will come from kitty (debug with map f1 show_kitty_env_vars)
  • $EDITOR in {open,launch}-actions.conf

... kitty is still picking up $EDITOR from my shell env somehow even if I set env EDITOR= ...

This is working as expected.
I guess you don't have editor configured, please refer to the documentation.
If you don't have editor configured and kitty's environment variable doesn't have editor, kitty will execute shell rc and get EDITOR from it.

... I tried also env ... doesn't even show in the output of show_kitty_env_vars

Please see the documentation. These are not for kitty.

... opening files from within kitty should or shouldn't pick up shell rc env variables?

EDITOR and SHELL are two special environment variables in {open,launch}-actions.conf.
For the others, kitty does not read the environment variables in shell rc.

... click on a file, I expect that this will pick up my shell environment properly.

In macOS, kitty is executed by launchd, and the shell rc environment variable is not known unless kitty invokes the shell every time, but does not do so. In the actions profile, all programs will be run directly by kitty and have nothing to do with shell rc.

My guess is that you need the following configuration.

editor nvim
exe_search_path /your/path/to/executables/dir

or

editor /abs/path/to/nvim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants