Motivation
I came from Linux where to open a file in your favorite editor you just create a .desktop file. On macOS you usually have to write a script using AppleScript or JXA. It requires some knowledge of the system API. To save time to others & myself and make it more normalized I created this general purpose template that will make the process simple and straightforward.
At the moment tested with:
- kitty
- iTerm2
- Terminal.app
- Alacritty
- Neovim
- Vim
- Emacs
- Nano
By default it opens the editor in a new terminal window, and multiple files are handled by the editor itself.
These behaviors can be customized by editing the configuration file. The following things can be customized:
- the way it opens the editor:
- new terminal window
- new terminal tab
- the way it handles multiple files:
- editor tabs
- terminal tabs
- new terminal windows
If the terminal supports the launcher controls it via the terminal API, otherwise it uses the fallback method which is just using keyboard shortcuts as if the user typed them. They can be customized.
Note
The fallback method is not that reliable, but it works in most cases.
Note
There are plans to add a configuration script that will help with the configuration, but for now, you have to do it manually.
- Install
- jxa-builder (my other project that will help compiling and installing the launcher)
- (optional) duti make the output app the default editor for most of text files
- Clone the repository
- cd into the repository
- Open the src/index.js file. You'll see the following code snippet:
/////////////////////////////////////////////////////////////
const options = {
// "newTab" | "newWindow"
howToOpenEditor: "newWindow",
// "terminalTab" | "terminalWindow" | "editorTab"
howToHandleMultipleFiles: "editorTab",
// e.g. kitty | iTerm | Terminal | Hyper | Alacritty
terminal: "kitty",
// terminal | default
actionRecipe: "terminal",
// actionRecipe: "default",
defaultActionRecipeKeystrokes: {
// e.g. "control+shift+t" | "option+n" | null | undefined
openNewTerminalWindow: "command+n",
openNewTerminalTab: "command+t",
},
};
// e.g. "nvim" | "vim" | "vi" | "nano" | "emacs"
const editor = "nvim";
const homePath = ph.homeDirectory();
const configPath = homePath + "/.config";
const configFolderName = editor.toLowerCase() + "-launcher";
const optionsFileName = "options.json";
/////////////////////////////////////////////////////////////- Here you can modify launched editor, defaults, and where to store the configuration file.
Tip
Generally, the only things you need to change here are terminal and editor
But if you're interested the other options are explained below:
Properties:
- The
optionsobject contains the default configuration that you can customized via config file.- The
howToOpenEditoris the way the editor will be opened. - The
howToHandleMultipleFilesis the way the launcher will handle multiple files. - The
terminalis the terminal that will be used to open the editor. - The
actionRecipeis the way the launcher will control the terminal. The terminal API or the default fallback method. - The
defaultActionRecipeKeystrokesare the keystrokes that the launcher will use to control the terminal.
- The
- The constants below won't be available in the config file, so if you want to change them do it now.
- The
editoris the editor that will be used to open the file (it's the command that you would use in the terminal). - The
configPathis the path to the configuration folder. Other popular locations on macOS are~/Library/Preferencesand~/Library/Application Support. - The
configFolderNameis the name of the configuration folder. - The
optionsFileNameis the name of the configuration file.
- Save and close the file
- To add custom icon you will need it in
icnsformat. Place it in the root of the repository and name iticon.icnsIf you have a pn - Now before compilation open the package.json file and in the
jxaobject change theappNameto the name you want to give to the app.
"jxa":{
"appName": "Double Click to Edit",
"compMode": "app",
"depsInstallMode": "app"
}- Save and close the file
- Now you can compile and install
jxa-builder build && jxa-builder install- (optional) Make the app the default one for text files
./src/set_default_text_editor.sh <app_name>kitty
if you're using kitty you'll need to enable the remote control in the configuration file. Add the following line to the ~/.config/kitty/kitty.conf file:
allow_remote_control yesAnd in the same directory create a file named macos-launch-services-cmdline with the following content:
--listen-on=unix:/tmp/mykitty --single-instance