Jump to content

Recommended Posts

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. 

 

OSTlcQf.png

 

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.

 

w0MRmf1.png

 

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 :D

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 by Virgilius Haufniensis
Link to comment
  • 8 months later...
  • 1 year later...
  • 8 months later...

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...