Using Kitty as a custom terminal for Alfred

Posted on 22 Mar 2020 - filed under: terminal, productivity, macos

2023-05-13 edit: another fix to the apple script to wait for kitty to create the socket before trying to send a message to it

2020-10-26 edit: small fix to the apple script to launch Kitty first

10x macOS productivity (not really)

Kitty is an excellent GPU powered terminal application that’s awesome in many ways. iTerm2 is also a good option on macOS but, compared to Kitty, it just feels sluggish and bloated.

Another macOS tool that I use daily is Alfred, it’s a featureful spotlight replacement that can basically make coffee (as we say in French, not sure that’s a thing in English).

Alfred allows you to send a command directly to the terminal, it works with the default Terminal.app out of the box, but using anything else requires a bit of work.

It uses AppleScript to interact with applications, but Kitty does not offer an AppleScript API…. BUT Kitty does offer its own API, which allows you to control any aspects of kitty from outside kitty (yes even over ssh if you wish).

Setting it up

By default kitty disables this feature for security reasons, to enable it you need to have allow_remote_control yes in your kitty.conf or run kitty with -o allow_remote_control=yes.

That only allows to control kitty from within the kitty instance. We want to do that from another process, in order to do that you need to spawn kitty with --listen-on unix:A_PATH_THAT_YOU_CHOOSE, that will open a UNIX socket that will be used to interact with this kitty instance.

Given that on macOS you’re launching an app, it’s tough to send it a command line argument, but do not worry, Kitty’s got you covered, by creating a special file named macos-launch-services-cmdline in your ~./config/kitty/ folder, the content of this file will automatically be used as command line argument to Kitty.

So in ~./config/kitty/macos-launch-services-cmdline, put: --listen-on unix:/tmp/mykitty.

In Alfred, in the terminal settings, choose “Custom”, and input the following appleScript:

on alfred_script(q)
    tell application "kitty" to activate
    tell application "System Events"
        repeat until (exists file "/tmp/kittynyx.sock")
            delay 6
        end repeat
    end tell
    do shell script "/Applications/kitty.app/Contents/MacOS/kitty @ --to unix:/tmp/kittynyx.sock new-window --new-tab"
    tell application "System Events" to keystroke q
    tell application "System Events"
        key code 36 -- enter key
    end tell
end alfred_script

This script will ask kitty to open a new tab (using the socket we defined), and then use AppleScript to activate the window and send key events.

Hope it helps !


Comments

Comment on github