Skip to content

Instantly share code, notes, and snippets.

@willeccles
Last active October 12, 2024 09:03
Show Gist options
  • Save willeccles/02228fc54c42942531a072318dcaf82b to your computer and use it in GitHub Desktop.
Save willeccles/02228fc54c42942531a072318dcaf82b to your computer and use it in GitHub Desktop.
AppleScript for using Kitty in Alfred. This was made for bash, but can easily be made to work with any shell.
(* 2019-06-07: Added nohup and output redirection to fix a bug with "Open Terminal here" feature.
Thanks to @fools-mate for bringing the issue to my attention. *)
on alfred_script(q)
do shell script "cd ~; nohup /Applications/kitty.app/Contents/MacOS/kitty /bin/bash -c \"source ~/.bashrc && " & q & ";/bin/bash\" > /dev/null 2>&1 &"
end alfred_script
@meseck
Copy link

meseck commented Jun 7, 2019

That was a quick and extensive response, thanks!

It tried the command in the terminal and it works fine but when I try to exchange the line in Alfred, it don't work anymore.
What do I wrong? I have no experience in AppleScript.

Btw. nice little trick to use which zsh.

@willeccles
Copy link
Author

@fools-mate: If you can post your AppleScript here, I can probably help you a bit better. The one I just posted is not for use in Alfred, it's for the Automator Folder Action thing that I made, which would cause it not to work, as it's specialized for that purpose. However, if you follow the general steps I listed, you should be able to get to a working solution. If not, post your script here and I'll take a look when I can. Apologies for the misleading explanation, I'll update it so that any future readers aren't confused.

@meseck
Copy link

meseck commented Jun 7, 2019

Ah ok, I thought this solution would be also a fix for the problem in Alfred.

I just tried to exchange the one line in Alfred, this was really naiv. 😅

on alfred_script(q)
	do shell script "nohup /Applications/kitty.app/Contents/MacOS/kitty `which zsh` -c "source ~/.zshrc && cd \"$1\"; `which zsh`" > /dev/null 2>&1 &"
end alfred_script

@willeccles
Copy link
Author

willeccles commented Jun 7, 2019

@fools-mate: This is an adapted version of the original (for bash). Try this and let me know if it works for you! I haven't tested it since I'm at work, but this should do it for you.

on alfred_script(q)
	do shell script "cd ~; nohup /Applications/kitty.app/Contents/MacOS/kitty /bin/bash -c \"source ~/.bashrc && " & q & ";/bin/bash\" > /dev/null 2>&1 &"
end alfred_script

@meseck
Copy link

meseck commented Jun 7, 2019

I tried it with zsh and it works great! 🎉
Thank you.

@willeccles
Copy link
Author

@fools-mate: No worries! Glad it works for you. I'll update the original script in a moment to reflect this change.

@aahung
Copy link

aahung commented Jun 26, 2019

Current script will open another instance of kitty.app (extra icon in Dock)

@willeccles
Copy link
Author

@aahung I am pretty sure this is a kitty preference, and I haven't noticed this issue before. Not sure what to tell you.

@pyrho
Copy link

pyrho commented Mar 23, 2020

If you want to open a new tab within an existing Kitty instance I came up with this: https://gist.github.com/pyrho/d2f4fe152eb8113b1956edd6d7456862, it's a bit more troublesome to set up, but nothing too hard ^^

@eliliam
Copy link

eliliam commented Aug 5, 2024

I just came across this gist as I was trying to set up this functionality in Alfred, however when I tried using it, it kept splitting my active terminal instead of opening a new tab. I did some digging and it turns out that the new-window command is deprecated, replaced by the launch command. Here's the snippet that I was able to come up with that opens a new tab instead of splitting the current one.

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

In addition to this, you will need to ensure that allow_remote_control yes is present in your ~/.config/kitty/kitty.conf file, and then also create a new file at ~/.config/kitty/macos-launch-services-cmdline which contains the following:

--listen-on unix:/tmp/mykitty.sock

This macos-launch-services-cmdline file is specific to the Mac version of kitty and specifies launch arguments to use, which is the only way I was able to get it to create the necessary socket file for remote control.

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