Here is an AppleScript script to make it possible double-click files in Finder so that they will be opened in Vim, by Normen Hansen: vim-macos-scripts/open-file-in-vim.applescript at master · normen/vim-macos-scripts · GitHub (it must be used from Automator).
I try to simplify it and also to make it possible to use it without Automator. To use my own version, you only need to save it as application instead, like this:
osacompile -o open-with-vim.app open-with-vim.applescript
and then copy it to /Applications
and set your .txt
files to be opened using this app.
Here it is, it alsmost works:
-- https://github.com/normen/vim-macos-scripts/blob/master/open-file-in-vim.applescript
-- opens Vim with file or file list
-- sets current folder to parent folder of first file
on open theFiles
set command to {}
if input is null or input is {} or ((item 1 in input) as string) is "" then
-- no files, open vim without parameters
set end of command to "vim;exit"
else
set firstFile to (item 1 of theFiles) as string
tell application "Finder" to set pathFolderParent to quoted form of (POSIX path of ((folder of item firstFile) as string))
set end of command to "cd" & space & (pathFolderParent as string)
set end of command to ";hx" -- e.g. vi or any other command-line text editor
set fileList to {}
repeat with i from 1 to (theFiles count)
set end of fileList to space & quoted form of (POSIX path of (item i of theFiles as string))
end repeat
set end of command to (fileList as string)
set end of command to ";exit" -- if Terminal > Settings > Profiles > Shell > When the shell exits != Don't close the window
end if
set command to command as string
set myTab to null
tell application "Terminal"
if it is not running then
set myTab to (do script command in window 1)
else
set myTab to (do script command)
end if
activate
end tell
return
end open
The only problem is the if
block in the very beginning:
if input is null or input is {} or ((item 1 in input) as string) is "" then
What is wrong here? How to make it work? (Assuming it already works fine if AppleScript is used using Automator.)