Skip to content

Instantly share code, notes, and snippets.

@CoderPiF
Last active February 20, 2023 09:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CoderPiF/86238712faa40f163ff3f739887530b1 to your computer and use it in GitHub Desktop.
Save CoderPiF/86238712faa40f163ff3f739887530b1 to your computer and use it in GitHub Desktop.
open file with vim in iTerm2 by double click
on run {input, parameters}
set the_path to POSIX path of input
set cmd to "vim '" & the_path & "'"
set isRunning to (application "iTerm" is running)
tell application "iTerm"
activate
if isRunning then
if (count of windows) > 0 then
set sess to (current session of (create tab with default profile of current window))
else
set sess to (current session of (create window with default profile))
end if
else
set sess to (current session of current window)
end if
tell sess
write text cmd
end tell
end tell
end run
@nick-kadutskyi
Copy link

Thank's for a very helpful script!

I would add a delay until a current window exists in cases when iTerm is not running so that it has time to open a new window:

repeat until (exists current window)
    delay 0.1
end repeat

Result:

on run {input, parameters}
	set the_path to POSIX path of input
	set cmd to "vim '" & the_path & "'"
	set isRunning to (application "iTerm" is running)
	tell application "iTerm"
		activate
		if isRunning then
			if (count of windows) > 0 then
				set sess to (current session of (create tab with default profile of current window))
			else
				set sess to (current session of (create window with default profile))
			end if
		else
			repeat until (exists current window)
                            delay 0.1
                        end repeat
			set sess to (current session of current window)
		end if
		tell sess
			write text cmd
		end tell
	end tell
end run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment