Virgilius Haufniensis Posted August 21, 2017 Share Posted August 21, 2017 (edited) Hey everybody! I am working on this workflow where I have a file filter with the most important text files (for me, at least) to open with vim in iTerm. Currently I have it configured so that I can choose to just run vim and get a new document or I can choose to run "v $SOMEFILE" in Alfred to make it open that way. It's super cool, I've used https://github.com/stuartcryan/custom-iterm-applescripts-for-alfred/ and Macvimtoolbox as inspiration for this. There is one bug, however. When the active iTerm window is not just showing a prompt but engaged, then the script doesn't open a new windows but just pastes the "nvim /path/to/file" into the window. I'd like to configure the Apple-script to just open a new window in that case. How do I do that? The code I'm using is here, btw: on alfred_script(q) my runIniTerm( "nvim " & q & "" ) end alfred_script -- This is v0.5 of the custom script for AlfredApp for iTerm 2.9+ -- Please see https://github.com/stuartcryan/custom-iterm-applescripts-for-alfred/ -- for the latest changes. on is_running(app_name) tell application "System Events" to (name of processes) contains app_name end is_running -- Please note, if you store the iTerm binary in any other location than the Applications Folder -- please ensure you update the two locations below (in the format of : rather than / for folder dividers) -- this gets around issues with AppleScript not handling things well if you have two iTerm binaries on your system... which can happen on runIniTerm(q) if is_running("iTerm2") or is_running("iTerm") then run script " on run {q} tell application \":Applications:iTerm.app\" activate try select first window set onlywindow to true on error create window with default profile select first window set onlywindow to true end try tell the first window if onlywindow is false then create tab with default profile end if tell current session to write text q end tell end tell end run " with parameters {q} else run script " on run {q} tell application \":Applications:iTerm.app\" activate try select first window on error create window with default profile select first window end try tell the first window tell current session to write text q end tell end tell end run " with parameters {q} end if end runIniTerm The workflow can be found here: https://transfer.sh/Ul5L3/Open-files-with-Vim.alfredworkflow Edited August 22, 2017 by Virgilius Haufniensis Link to comment
deanishe Posted August 21, 2017 Share Posted August 21, 2017 Please post the actual workflow, not pictures and extracts of it. Upload it somewhere and post a link. It's unreasonable to expect us to first rebuild your workflow in order to help you. Link to comment
Virgilius Haufniensis Posted August 22, 2017 Author Share Posted August 22, 2017 Of course, sorry about that. The workflow can be found here: https://transfer.sh/Ul5L3/Open-files-with-Vim.alfredworkflow Link to comment
deanishe Posted August 22, 2017 Share Posted August 22, 2017 You've broken the script or grabbed a broken version. This very important line is different in your version, and that's why it isn't working for you. Link to comment
Virgilius Haufniensis Posted August 22, 2017 Author Share Posted August 22, 2017 Thank you so much, deanishe! It now works perfectly. Link to comment
capsella Posted May 2, 2018 Share Posted May 2, 2018 did you post the working version someplace, it sounds useful :) Link to comment
bruhmyguy630 Posted October 29, 2019 Share Posted October 29, 2019 Here's a version I put together since that link is dead: https://github.com/spiderman19989/myalfredworkflows/blob/master/vim.alfredworkflow Link to comment
Martin Chadwick Posted July 27, 2020 Share Posted July 27, 2020 Hi .. I thought it might be interesting to extend the Vim+iTerm idea as follows: Refer to the automator scripts described here:https://blog.schembri.me/post/neovim-everywhere-on-macos/ #1 'Edit in Vim' This automator takes text in and spits text out. In other words, you need to select some text to edit in vim, run the action, and the text will be replaced with whatever you saved in vim. (for reference - AS code attached below from the above URL). #2 'Write in NeoVim' This automator launches vim launch vim with the current context without requiring text to be selected. (for reference - see above URL). This solution neatly resolves the problem of integrating vim-style editing into Mail.app for long emails (as well as posts to forums like this!) I assume this needs a script-filter > osascript-AS workflow (separate from the 'custom-alfred-iterm-scripts' described earlier). However I'm not familiar enough with Alfred workflow AS coding to implement the input/output methods required. (I've had a look through Packal and this Forum for equivalent solutions.) Any assistance appreciated from others interested in these workflow ideas. Martin on run {input, parameters} set tempfile to do shell script "mktemp -t edit-in-vim" tell application "iTerm" create window with default profile tell the current window tell the current session write text "cat <<EOF > \"" & tempfile & "\"\n" & input & "\nEOF" write text "vim \"" & tempfile & "\"" repeat while name contains "Shell" or name contains "vim" delay 0.5 end repeat set content to do shell script "cat \"" & tempfile & "\"" end tell close end tell end tell return content end run Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now