Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Last active February 21, 2024 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8henrie/f3abd106f6da2928b0b6a767a12f4b78 to your computer and use it in GitHub Desktop.
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
#!/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()))
}
// Use double quotes since we're single-quoting the file paths
let bashCommand = `"hx ${files.join(" ")}"`
let cmd = [
"PATH=/etc/profiles/per-user/n8henrie/bin:$PATH",
"alacritty",
'--class="__text_scratchpad"',
"--command",
"bash",
"-c",
bashCommand
]
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