2021-07-17 13:58:42 +03:00
|
|
|
Scripting the mouse click
|
2020-09-18 19:03:30 +03:00
|
|
|
======================================================
|
|
|
|
|
2022-04-27 10:58:20 +03:00
|
|
|
|kitty| has support for :term:`terminal hyperlinks <hyperlinks>`. These are
|
|
|
|
generated by many terminal programs, such as ``ls``, ``gcc``, ``systemd``,
|
|
|
|
:ref:`tool_mdcat`, etc. You can customize exactly what happens when clicking on
|
|
|
|
these hyperlinks in |kitty|.
|
2020-09-18 19:03:30 +03:00
|
|
|
|
2022-04-27 10:58:20 +03:00
|
|
|
You can tell kitty to take arbitrarily many, complex actions when a link is
|
|
|
|
clicked. Let us illustrate with some examples, first. Create the file
|
|
|
|
:file:`~/.config/kitty/open-actions.conf` with the following:
|
2020-09-18 19:03:30 +03:00
|
|
|
|
|
|
|
.. code:: conf
|
|
|
|
|
|
|
|
# Open any image in the full kitty window by clicking on it
|
|
|
|
protocol file
|
|
|
|
mime image/*
|
|
|
|
action launch --type=overlay kitty +kitten icat --hold ${FILE_PATH}
|
|
|
|
|
|
|
|
Now, run ``ls --hyperlink=auto`` in kitty and click on the filename of an
|
|
|
|
image, holding down :kbd:`ctrl+shift`. It will be opened over the current
|
|
|
|
window. Press any key to close it.
|
|
|
|
|
2022-04-27 10:58:20 +03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The :program:`ls` comes with macOS does not support hyperlink, you need to
|
|
|
|
install `GNU Coreutils <https://www.gnu.org/software/coreutils/>`__. If you
|
|
|
|
install it via `Homebrew <https://formulae.brew.sh/formula/coreutils>`__, it
|
|
|
|
will be :program:`gls`.
|
|
|
|
|
2020-09-18 19:03:30 +03:00
|
|
|
Each entry in :file:`open-actions.conf` consists of one or more
|
|
|
|
:ref:`matching_criteria`, such as ``protocol`` and ``mime`` and one or more
|
|
|
|
``action`` entries. In the example above kitty uses the :doc:`launch <launch>`
|
|
|
|
action which can be used to run external programs. Entries are separated by
|
|
|
|
blank lines.
|
|
|
|
|
|
|
|
Actions are very powerful, anything that you can map to a key combination in
|
2022-04-27 10:58:20 +03:00
|
|
|
:file:`kitty.conf` can be used as an action. You can specify more than one
|
|
|
|
action per entry if you like, for example:
|
2020-09-18 19:03:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
.. code:: conf
|
|
|
|
|
|
|
|
# Tail a log file (*.log) in a new OS Window and reduce its font size
|
|
|
|
protocol file
|
|
|
|
ext log
|
|
|
|
action launch --title ${FILE} --type=os-window tail -f ${FILE_PATH}
|
|
|
|
action change_font_size current -2
|
|
|
|
|
|
|
|
|
|
|
|
In the action specification you can expand environment variables, as shown in
|
|
|
|
the examples above. In addition to regular environment variables, there are
|
|
|
|
some special variables, documented below:
|
|
|
|
|
|
|
|
``URL``
|
|
|
|
The full URL being opened
|
|
|
|
|
|
|
|
``FILE_PATH``
|
|
|
|
The path portion of the URL (unquoted)
|
|
|
|
|
|
|
|
``FILE``
|
|
|
|
The file portion of the path of the URL (unquoted)
|
|
|
|
|
|
|
|
``FRAGMENT``
|
|
|
|
The fragment (unquoted), if any of the URL or the empty string.
|
|
|
|
|
2021-11-22 17:22:43 +03:00
|
|
|
|
|
|
|
.. note::
|
2022-04-27 10:58:20 +03:00
|
|
|
You can use the :opt:`action_alias` option just as in :file:`kitty.conf` to
|
2021-11-22 17:22:43 +03:00
|
|
|
define aliases for frequently used actions.
|
|
|
|
|
|
|
|
|
2020-09-18 19:03:30 +03:00
|
|
|
.. _matching_criteria:
|
|
|
|
|
|
|
|
Matching criteria
|
|
|
|
------------------
|
|
|
|
|
|
|
|
An entry in :file:`open-actions.conf` must have one or more matching criteria.
|
2021-04-28 17:16:56 +03:00
|
|
|
URLs that match all criteria for an entry will trigger that entry's actions.
|
2020-09-18 19:03:30 +03:00
|
|
|
Processing stops at the first matching entry, so put more specific matching
|
|
|
|
criteria at the start of the list. Entries in the file are separated by blank
|
|
|
|
lines. The various available criteria are:
|
|
|
|
|
|
|
|
``protocol``
|
|
|
|
A comma separated list of protocols, for example: ``http, https``. If
|
2022-04-27 10:58:20 +03:00
|
|
|
absent, there is no constraint on protocol.
|
2020-09-18 19:03:30 +03:00
|
|
|
|
|
|
|
``url``
|
|
|
|
A regular expression that must match against the entire (unquoted) URL
|
|
|
|
|
2020-09-19 05:01:17 +03:00
|
|
|
``fragment_matches``
|
|
|
|
A regular expression that must match against the fragment (part after #) in
|
|
|
|
the URL
|
2020-09-18 19:03:30 +03:00
|
|
|
|
|
|
|
``mime``
|
|
|
|
A comma separated list of MIME types, for example: ``text/*, image/*,
|
2022-04-27 10:58:20 +03:00
|
|
|
application/pdf``. You can add MIME types to kitty by creating a file named
|
|
|
|
:file:`mime.types` in the :ref:`kitty configuration directory <confloc>`.
|
|
|
|
Useful if your system MIME database does not have definitions you need. This
|
|
|
|
file is in the standard format of one definition per line, like:
|
|
|
|
``text/plain rst md``. Note that the MIME type for directories is
|
|
|
|
``inode/directory``.
|
2020-09-18 19:03:30 +03:00
|
|
|
|
|
|
|
``ext``
|
|
|
|
A comma separated list of file extensions, for example: ``jpeg, tar.gz``
|
|
|
|
|
|
|
|
``file``
|
|
|
|
A shell glob pattern that must match the filename, for example:
|
|
|
|
``image-??.png``
|
2022-01-07 16:17:37 +03:00
|
|
|
|
|
|
|
|
2022-02-05 10:27:15 +03:00
|
|
|
.. _launch_actions:
|
|
|
|
|
2022-01-07 16:17:37 +03:00
|
|
|
Scripting the opening of files with kitty on macOS
|
|
|
|
-------------------------------------------------------
|
|
|
|
|
2022-02-05 10:27:15 +03:00
|
|
|
On macOS you can use :guilabel:`Open With` in Finder or drag and drop files and
|
|
|
|
URLs onto the kitty dock icon to open them with kitty. The default actions are:
|
|
|
|
|
|
|
|
* Open text files in your editor and images using the icat kitten.
|
|
|
|
* Run shell scripts in a shell
|
|
|
|
* Open SSH urls using the ssh command
|
|
|
|
|
2022-02-05 15:09:48 +03:00
|
|
|
These actions can also be executed from the command line by running::
|
|
|
|
|
2022-04-27 10:58:20 +03:00
|
|
|
kitty +open file_or_url another_url ...
|
|
|
|
|
|
|
|
# macOS only
|
|
|
|
open -a kitty.app file_or_url another_url ...
|
2022-02-05 15:09:48 +03:00
|
|
|
|
2022-02-06 18:19:40 +03:00
|
|
|
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:
|
2022-02-06 14:51:39 +03:00
|
|
|
|
|
|
|
.. code-block:: sh
|
|
|
|
|
2022-02-06 18:19:40 +03:00
|
|
|
# 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
|
2022-02-06 14:51:39 +03:00
|
|
|
|
2022-02-05 10:27:15 +03:00
|
|
|
You can customize these actions by creating a :file:`launch-actions.conf` file
|
2022-04-27 10:58:20 +03:00
|
|
|
in the :ref:`kitty config directory <confloc>`, just like the
|
|
|
|
:file:`open-actions.conf` file above. For example:
|
2022-01-07 16:17:37 +03:00
|
|
|
|
2022-06-02 06:55:30 +03:00
|
|
|
.. literalinclude:: ../kitty/open_actions.py
|
|
|
|
:language: conf
|
|
|
|
:start-at: # Open script files
|
|
|
|
:end-before: '''.splitlines()))
|