neovide / neovide

No Nonsense Neovim Client in Rust

Home Page:https://neovide.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

On Mac - selecting a file in finder and trying to open it with Neovide opens an empty file

yanshay opened this issue · comments

Describe the bug
On Mac, selecting a file in finder and trying to open the file through right clicking the file and selecting Neovide as the application to open the file with, results with opening an empty file.
Also, Mac shows that it is uncertain that Neovide can open such a file.

To Reproduce
Steps to reproduce the behavior:

  1. Go to Finder
  2. Select a .txt file (or any other textual format)
  3. Right click
  4. Select 'Open With' from the menu
  5. Select "Other..."
  6. Change "Enable:" to "All Applications"
  7. Select Neovide as the application to open the file with
  8. Press Open button
  9. Neovide is being open with an empty buffer

Expected behavior
Neovide should open with the selected file

Desktop (please complete the following information):

  • OS: macOS 11.6.5
  • Neovide Version 0.8.0
  • Neovim Version 0.6.1
  • Mac - Intel

Additional context

  • NVim is installed with Brew and I used the ' sudo launchctl config user path' to get it working

I don't have mac device.
does this issue relevant to you by any chance! #1240

It was relevant. That was about launching Neovide from finder (Mac file explorer). That one I worked around as you can see in my comment there.

Now I'm trying to get Neovide to open a specific file, not just launch. It opens but doesn't load the file.

Same issue here on MacOS 12.3.1 MacBook Pro (13-inch, M1, 2020) with Neovide 0.8.0. I can open Neovide.app as GUI and in shell, but I can't open a file in the finder, I get an empty buffer.

i have macbook pro m1 and when i select js file to open with neovide i get empty buffer
another problem is when i open neovide from launcher everytime i need to open nvim-tree and from root path go to users and to my project
Telescope not working because it's not searching in my project directory it's searching in my os
sorry for bad english😅

@younes-ghorbany For the last issue: See the option autochdir through :h 'autochdir'. That'll autonavigate NeoVim inside of Neovide to the folder of the currently opened file.

But that has nothing to do with this issue. Neovide can't know where you want to be when starting from the launcher.

commented

It would be really helpful for macOS to be able to open a file in new buffer by click.
Maybe solution would be create an Automator app with AppleScript which run Neovide.app with specific arguments? And save this app - assign to any file by default, so by opening test.js for example it will open newly created Automator app, which will trigger Neovide with arguments (arguments - file path which you want to open in new buffer)

Does anyone know how to write this kind of AppleScript ?)

@xbladesub This should work

on run {input, parameters} 
    set filePath to quoted form of POSIX path of input 
    set neovide to "/usr/local/bin/neovide " -- path to neovide 
    do shell script neovide & filePath 
    return input 
end run

Just create a new application in automator and add this to the "run AppleScript" action.

EDIT: the script only allows you to open one file at a time. I hacked together this other script that might solve it

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on activate_open_instance(win_title, is_first_time)
	tell application "System Events"
		set neovideProcList to a reference to (every process whose name is "neovide")
		repeat with proc in neovideProcList
			set PID to proc's unix id
			set myFiles to paragraphs of (do shell script "lsof -F -p " & PID & " | grep ^n/ | cut -c2-")
			set fName to my replace_chars(win_title, "'", "") 
			if myFiles contains fName then
				tell proc
					set frontmost to true
				end tell
				return true
			end if
		end repeat
	end tell
	
	return false
end activate_open_instance

on run {input, parameters}
	set filePath to quoted form of POSIX path of input
	if not my activate_open_instance(filePath, false) then
		set neovide to "/usr/local/bin/neovide "
		do shell script neovide & filePath & " > /dev/null 2>&1 &"
		delay 0.3 
		my activate_open_instance(filePath, true)
	end if
	return input
end run

credits to https://gist.github.com/agzam/76d761804330cc8c4600fccda952ed1c

commented

@XLramones's solution didn't work for me with sh (default shell in Automator). I also made it work using $PATH so no need to manually set the full path of neovide.

Here is the fixed version:

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on activate_open_instance(win_title, is_first_time)
	tell application "System Events"
		set neovideProcList to a reference to (every process whose name is "neovide")
		repeat with proc in neovideProcList
			set PID to proc's unix id
			set myFiles to paragraphs of (do shell script "lsof -F -p" & space & PID & space & "| grep ^n/ | cut -c2-")
			set fName to my replace_chars(win_title, "'", "")
			if myFiles contains fName then
				tell proc
					set frontmost to true
				end tell
				return true
			end if
		end repeat
	end tell
	
	return false
end activate_open_instance

on run {input, parameters}
	set filePath to quoted form of POSIX path of input
	if true or not my activate_open_instance(filePath, false) then
		set enablePathVar to "eval \"$(/usr/libexec/path_helper -s)\"; PATH=$PATH:/opt/homebrew/bin neovide"
		set cmd to "$(/usr/bin/env neovide)"
		set fileCmdPath to quoted form of (filePath) & space & "> /dev/null 2>&1 &"

		do shell script enablePathVar & space & cmd & space & fileCmdPath
		delay 0.3
		my activate_open_instance(filePath, true)
	end if
	return input
end run
commented

@XLramones's solution didn't work for me with sh (default shell in Automator). I also made it work using $PATH so no need to manually set the full path of neovide.

Here is the fixed version:

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on activate_open_instance(win_title, is_first_time)
	tell application "System Events"
		set neovideProcList to a reference to (every process whose name is "neovide")
		repeat with proc in neovideProcList
			set PID to proc's unix id
			set myFiles to paragraphs of (do shell script "lsof -F -p" & space & PID & space & "| grep ^n/ | cut -c2-")
			set fName to my replace_chars(win_title, "'", "")
			if myFiles contains fName then
				tell proc
					set frontmost to true
				end tell
				return true
			end if
		end repeat
	end tell
	
	return false
end activate_open_instance

on run {input, parameters}
	set filePath to quoted form of POSIX path of input
	if true or not my activate_open_instance(filePath, false) then
		set enablePathVar to "eval \"$(/usr/libexec/path_helper -s)\"; PATH=$PATH:/opt/homebrew/bin neovide"
		set cmd to "$(/usr/bin/env neovide)"
		set fileCmdPath to quoted form of (filePath) & space & "> /dev/null 2>&1 &"

		do shell script enablePathVar & space & cmd & space & fileCmdPath
		delay 0.3
		my activate_open_instance(filePath, true)
	end if
	return input
end run

Your solution didn't work for me. I created the Applescript application, then used Open As -> and selected the application on a file. It opens up a blank Neovide window with the filename at the top and the correct file path, but the text area is always empty.

Any ideas how to debug?

commented

@ThSGM I just tested again and I can confirm that it works. I streamlined the code a bit here though:

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on activate_open_instance(win_title, is_first_time)
	tell application "System Events"
		set neovideProcList to a reference to (every process whose name is "neovide")
		repeat with proc in neovideProcList
			set PID to proc's unix id
			set myFiles to paragraphs of (do shell script "lsof -F -p" & space & PID & space & "| grep ^n/ | cut -c2-")
			set fName to my replace_chars(win_title, "'", "")
			if myFiles contains fName then
				tell proc
					set frontmost to true
				end tell
				return true
			end if
		end repeat
	end tell
	
	return false
end activate_open_instance

on run {input, parameters}
	set filePath to quoted form of POSIX path of input
	if not my activate_open_instance(filePath, false) then
		set enablePathVar to "eval \"$(/usr/libexec/path_helper -s)\"; PATH=$PATH:/opt/homebrew/bin;"
		set cmd to "/usr/bin/env neovide"
		set fileCmdPath to quoted form of filePath
		
		do shell script enablePathVar & space & cmd & space & fileCmdPath
		delay 0.3
		my activate_open_instance(filePath, true)
	end if
	return input
end run

Can you confirm that it works?

commented

@ThSGM I just tested again and I can confirm that it works. I streamlined the code a bit here though:

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on activate_open_instance(win_title, is_first_time)
	tell application "System Events"
		set neovideProcList to a reference to (every process whose name is "neovide")
		repeat with proc in neovideProcList
			set PID to proc's unix id
			set myFiles to paragraphs of (do shell script "lsof -F -p" & space & PID & space & "| grep ^n/ | cut -c2-")
			set fName to my replace_chars(win_title, "'", "")
			if myFiles contains fName then
				tell proc
					set frontmost to true
				end tell
				return true
			end if
		end repeat
	end tell
	
	return false
end activate_open_instance

on run {input, parameters}
	set filePath to quoted form of POSIX path of input
	if not my activate_open_instance(filePath, false) then
		set enablePathVar to "eval \"$(/usr/libexec/path_helper -s)\"; PATH=$PATH:/opt/homebrew/bin;"
		set cmd to "/usr/bin/env neovide"
		set fileCmdPath to quoted form of filePath
		
		do shell script enablePathVar & space & cmd & space & fileCmdPath
		delay 0.3
		my activate_open_instance(filePath, true)
	end if
	return input
end run

Can you confirm that it works?

Unfortunately it does not work. It launches neovide with a blank window.

image

image

Is there a way to debug?

commented

@ThSGM What is the file path and name? Can you right click on the file, click on other and open it using the integrated finder to select the OpenInNeovide launcher.

commented

@ThSGM What is the file path and name? Can you right click on the file, click on other and open it using the integrated finder to select the OpenInNeovide launcher.

The file path is in ~/Downloads/ and the file name is listed in the top of the second image above.

I create an Applescript Application file, then right click on the file, Open-As, and select the application file.

commented

For me it works when I slight change the code for the fileCmdPath variable:

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on activate_open_instance(win_title, is_first_time)
	tell application "System Events"
		set neovideProcList to a reference to (every process whose name is "neovide")
		repeat with proc in neovideProcList
			set PID to proc's unix id
			set myFiles to paragraphs of (do shell script "lsof -F -p" & space & PID & space & "| grep ^n/ | cut -c2-")
			set fName to my replace_chars(win_title, "'", "")
			if myFiles contains fName then
				tell proc
					set frontmost to true
				end tell
				return true
			end if
		end repeat
	end tell
	
	return false
end activate_open_instance

on run {input, parameters}
	set filePath to quoted form of POSIX path of (input as text)
	if true or not my activate_open_instance(filePath, false) then
		set enablePathVar to "eval \"$(/usr/libexec/path_helper -s)\"; PATH=$PATH:/opt/homebrew/bin neovide"
		set cmd to "$(/usr/bin/env neovide)"
		set fileCmdPath to quoted form of (POSIX path of (input as text))
		
		do shell script enablePathVar & space & cmd & space & fileCmdPath
		delay 0.3
		my activate_open_instance(filePath, true)
	end if
	return input
end run

This is a duplicate of #1682

commented

I use Lunarvim + script to run it in Neovide.
What should I change so it will use Lunarvim's config when I click on files?

Here the script which I use to run Neovide with Lunarvim's config:

#!/bin/bash
SERVICE="Neovide"
if pgrep -f "$SERVICE" >/dev/null
then
	open /Users/nshv/Repos/neovide/target/release/bundle/osx/Neovide.app
else
	export LUNARVIM_RUNTIME_DIR="${LUNARVIM_RUNTIME_DIR:-"$HOME/.local/share/lunarvim"}"
	export LUNARVIM_CONFIG_DIR="${LUNARVIM_CONFIG_DIR:-"$HOME/.config/lvim"}"
	export LUNARVIM_CACHE_DIR="${LUNARVIM_CACHE_DIR:-"$HOME/.cache/lvim"}"

	cd $HOME

	exec /Users/nshv/Repos/neovide/target/release/bundle/osx/Neovide.app/Contents/MacOS/neovide --frame=none -- -u "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "$@"
	# exec /Users/nshv/Repos/neovide/target/release/bundle/osx/Neovide.app/Contents/MacOS/neovide --frame=none --multigrid -- -u "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "$@"
fi

I use Lunarvim + script to run it in Neovide. What should I change so it will use Lunarvim's config when I click on files?

You can symlink the lunarvim directories to the normal spot, or import them in you init.lua.

Also, I think this belongs in discussions, but because you already asked here, dont remove it.

So symlinking would be:

ln -s ~/.local/share/lunarvim/lvim ~/.config/nvim

This will allow neovide to open the config straight from the default directory.

So symlinking would be:

ln -s ~/.local/share/lunarvim/lvim ~/.config/nvim

This will allow neovide to open the config straight from the default directory.

Awesome, that worked. Thanks

Firstly, thanks to the previous contributors for your efforts. I found the script would fail if Neovide.app was opened without specifying any file or was specified with multiple files. I made a little bit changes to it to fix these scenarios:

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on activate_open_instance(win_title, is_first_time)
	tell application "System Events"
		set neovideProcList to a reference to (every process whose name is "neovide")
		repeat with proc in neovideProcList
			set PID to proc's unix id
			set myFiles to paragraphs of (do shell script "lsof -F -p" & space & PID & space & "| grep ^n/ | cut -c2-")
			set fName to my replace_chars(win_title, "'", "")
			if myFiles contains fName then
				tell proc
					set frontmost to true
				end tell
				return true
			end if
		end repeat
	end tell
	
	return false
end activate_open_instance

on run {input, parameters}
	if input is not {} then
		set posixPaths to {}
		
		repeat with fileItem in input
			set posixPath to POSIX path of (fileItem as text)
			set end of posixPaths to quoted form of posixPath
		end repeat
		
		set AppleScript's text item delimiters to space
		set filePathsString to (posixPaths as string)
		set AppleScript's text item delimiters to ""
		
		set firstPosixPath to item 1 of posixPaths
		
		if true or not my activate_open_instance(firstPosixPath, false) then
			set enablePathVar to "eval \"$(/usr/libexec/path_helper -s)\"; PATH=$PATH:/opt/homebrew/bin neovide"
			set cmd to "$(/usr/bin/env neovide)"
			
			do shell script enablePathVar & space & cmd & space & filePathsString
			delay 0.3
			my activate_open_instance(firstPosixPath, true)
		end if
	else
		set enablePathVar to "eval \"$(/usr/libexec/path_helper -s)\"; PATH=$PATH:/opt/homebrew/bin neovide"
		set cmd to "$(/usr/bin/env neovide)"
		
		do shell script enablePathVar & space & cmd
		delay 0.3
		my activate_open_instance("", true)
	end if
	return input
end run

Hello there. Since #1562 is closed for this issue and this issue is circling around an Apple script (thanx for that!), is there anything new for a clean solution?

I think packaging as an appropriate MacOS app would be the best.

I would love if someone could find out how we should do this properly (maybe package the scripts) and make them part of the install?

There are also other scripts scattered around in different macOS related issues. Some of them even needed to make it work at all. So if someone could look if we should package some of that as well it would be good. For example

I'm therefore adding the hacktoberfest tag.

Getting this when trying to call/open Neovide directly with the file selected in my file manager:

image

@9mm worked on this and drag and drop here #2191, but unfortunately it was not as simple as it first seemed. Any additional help and comments are welcome in that PR.

commented

If you want to open a file from terminal, you can try

/Applications/neovide.app/Contents/MacOS/neovide file

This open an empty file before, but somehow this suddenly works as well

open -a neovide file

Finder file: open with neovide

Automator

  • Utilities > Run shell script

    /Applications/neovide.app/Contents/MacOS/neovide $1

  • Select Pass input as argument

  • File > Export app to /Applications/

Finder file open with... select the exported app

A little detail for @leafOfTree 's idea, start the creation flow with

  • from Automator, File>New and selecting Application as the type.

Firstly, thanks to the previous contributors for your efforts. I found the script would fail if Neovide.app was opened without specifying any file or was specified with multiple files. I made a little bit changes to it to fix these scenarios:

code
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

on activate_open_instance(win_title, is_first_time)
tell application "System Events"
  set neovideProcList to a reference to (every process whose name is "neovide")
  repeat with proc in neovideProcList
    set PID to proc's unix id
    set myFiles to paragraphs of (do shell script "lsof -F -p" & space & PID & space & "| grep ^n/ | cut -c2-")
    set fName to my replace_chars(win_title, "'", "")
    if myFiles contains fName then
      tell proc
        set frontmost to true
      end tell
      return true
    end if
  end repeat
end tell

return false
end activate_open_instance

on run {input, parameters}
if input is not {} then
  set posixPaths to {}

  repeat with fileItem in input
    set posixPath to POSIX path of (fileItem as text)
    set end of posixPaths to quoted form of posixPath
  end repeat

  set AppleScript's text item delimiters to space
  set filePathsString to (posixPaths as string)
  set AppleScript's text item delimiters to ""

  set firstPosixPath to item 1 of posixPaths

  if true or not my activate_open_instance(firstPosixPath, false) then
    set enablePathVar to "eval \"$(/usr/libexec/path_helper -s)\"; PATH=$PATH:/opt/homebrew/bin neovide"
    set cmd to "$(/usr/bin/env neovide)"

    do shell script enablePathVar & space & cmd & space & filePathsString
    delay 0.3
    my activate_open_instance(firstPosixPath, true)
  end if
else
  set enablePathVar to "eval \"$(/usr/libexec/path_helper -s)\"; PATH=$PATH:/opt/homebrew/bin neovide"
  set cmd to "$(/usr/bin/env neovide)"

  do shell script enablePathVar & space & cmd
  delay 0.3
  my activate_open_instance("", true)
end if
return input
end run

One thing not covered by the Application/AppleScript solution, is opening/adding another file in an already running Neovide.

If someone knows if this would be possible somehow, it would be a nice addition.
Thank you!

With 0.13, this issue does not occur anymore, probably due to #2395

Yes, we forgot to close the issues when merging that.