Last active
December 9, 2024 21:50
-
-
Save n8henrie/f3abd106f6da2928b0b6a767a12f4b78 to your computer and use it in GitHub Desktop.
JXA to open finder selection in MacVim, or default to an empty buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/osascript -l JavaScript | |
// For use in Hammerspoon via hs.osascript.javascriptFromFile | |
// | |
// Debugging: | |
// 1. Add a `delay` to the beginning | |
// 2. Add some `console.log`s | |
// 3. Run from console as `osascript Open\ in\ MacVim.js` | |
// 4. Switch to a Finder window before the sleep is done | |
'use strict'; | |
function quotedPosixPath(file) { | |
let prefixed = decodeURI(file.url()) | |
let posixPath = prefixed.replace(/^file:\/\//, "") | |
// https://stackoverflow.com/a/22827128 | |
// Escape internal single quotes | |
let escapedPath = posixPath.replace(/'/g, `'\\''`) | |
return `'${escapedPath}'` | |
} | |
(function () { | |
// delay(2) | |
const finder = Application("Finder") | |
const currentApp = Application.currentApplication() | |
currentApp.includeStandardAdditions = true | |
let selection = finder.selection() | |
let files = [] | |
for (var i=0; i<selection.length; i++) { | |
let file = selection[i] | |
files.push(quotedPosixPath(file)) | |
// helix doesn't seem to support `file://`, but nvim does, | |
// so can go back to the below if I go back to nvim | |
// files.push(JSON.stringify(selection[i].url())) | |
} | |
let cmd = [ | |
"PATH=/etc/profiles/per-user/n8henrie/bin:$PATH", | |
"/usr/bin/open", | |
"-n", | |
"-a", | |
"alacritty", | |
"--args", | |
'--class="__text_scratchpad"', | |
"--command", | |
"hx", | |
].concat(files) | |
let cmdStr = cmd.join(" ") | |
// console.log(cmdStr) | |
currentApp.doShellScript(cmdStr) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment