Skip to content

Instantly share code, notes, and snippets.

@zeekay
Last active August 29, 2015 14:01
Show Gist options
  • Save zeekay/4189df753ae1c3f66721 to your computer and use it in GitHub Desktop.
Save zeekay/4189df753ae1c3f66721 to your computer and use it in GitHub Desktop.
Edit Finder selection in terminal Vim.
-- Meant to be used as part of Automator service. Create a new service in
-- Automator and send files or folders to this AppleScript. Supports multiple files.
on run {input, parameters}
tell application "Finder"
activate
try
set this_folder to (the target of the front window) as alias
on error
set this_folder to home folder
end try
-- get POSIX paths to current folder and selected file(s)
set file_paths to my get_file_paths(input)
set folder_path to quoted form of POSIX path of this_folder
-- edit selection in terminal vim
my edit_in_vim(folder_path, file_paths)
end tell
end run
-- get POSIX path(s) to input
on get_file_paths(input)
set input_files to {}
-- concatenate quoted paths to input_files
repeat with input_file in input
set end of input_files to quoted form of POSIX path of input_file & " "
end repeat
-- coerce to text
return input_files as text
end get_file_paths
-- cd to folder_path and edit file_paths in terminal vim
on edit_in_vim(folder_path, file_paths)
tell application "iTerm"
activate
-- make new terminal
set term to (make new terminal)
tell term
-- make new session
launch session "Default Session"
-- cd to folder_path and open selected files in vim
tell the last session
write text "cd " & folder_path & "; vim " & file_paths & return
end tell
end tell
end tell
end edit_in_vim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment