1

Is it possible to open a document by using the Windows 10's "open with" feature with a program that comes with Cygwin, e.g. Zathura?

Open-with

This one with a Cygwin program.

First attempt: try to simply use the Cygwin program under Windows directly, i.e. the executable in C:\cygwin64\bin (in case of the standard install path).

Regardless of the "open with" problem, directly opening a Cygwin program often doesn't work well except for the most simple programs (no login shell is run first!).

So, starting the Cygwin X server, setting the Windows system environment variable DISPLAY (this gets passed through) to :0.0, using "open with" on C:\book.pdf and choosing C:\cygwin64\bin\zathura.exe makes Zathura open the GUI window. But we get the error:

error: Unknown file type: 'cannot open `/cygdrive/c/Windows/system32/C:\book.pdf'    
(No such file or directory)'

Obviously, something goes wrong with the path here.

Second attempt: work around the problem by using a Windows shortcut with the target:

C:\cygwin64\bin\bash.exe -l -c 'zathura "`cygpath "$0"`"'

At least that works for drag and drop in the Windows explorer. But again not with "open with".

As a minimal example create a Windows shortcut emitpath.lnk with the target:

C:\cygwin64\bin\bash.exe -l -c 'echo "`cygpath "$0"`" > out.path'

It works as expected again with dragging & dropping the document onto the shortcut: a file in the Cygwin home out.path is created and the "cygwin-ified" path of the document is written into it. We got very close.

But, alas, with "open with" it does not work; nothing happens.

So how to use "open with" with a Cygwin program on Windows 10?

There must be an easier way than to create an executable wrapper.

  • First use $() instead of back-ticks. They are easier. Then you will have to show us what close looks like. – ctrl-alt-delor May 1 at 20:54
  • @ctrl-alt-delor I don't understand the last sentence. – wolf-revo-cats May 2 at 1:26
  • You said "We got very close.". What does this look like. I can not say head north, or head south, as I don't know in which direction you are close in. Tell us what happened. Describe what happened. – ctrl-alt-delor May 2 at 14:23
  • Put that it the question, so people don't have to read all the comments. – ctrl-alt-delor May 3 at 9:15
1

Make a batch file like:

@echo off
D:
chdir D:\cygwin64\bin
set A=%1
bash --login  -c "/usr/bin/echo $(cygpath  $A)"
pause

In the root directory of Cygwin should be present a Cygwin.bat to be used as base.
Put the link to the batch file in the SendTo Folder

Now when I send a picture from my Pictures folder, I have in a new CMD window:

/cygdrive/c/Users/Marco/Pictures/img250.jpg
Drücken Sie eine beliebige Taste . . .

The last is Press any key in German.
From here you can modify the batch to run any cygwin program you want.

If you plan to use the X server, it is probably better to have the Xwin server already running due to possible timing problem in the execution.

| improve this answer | |
1

The problem with matzeri's solution is that it only works for the most sane filenames. A white space in the filename already breaks it.

Remember, if C:\book 1.pdf is the document to be opened, we want to end up with a string where on the bash side the escaping is correct - like this:

"/usr/bin/zathura \"$(cygpath 'C:\book 1.pdf')\""

But Windows passes the document path as an argument to a batch-script in two different ways:

  • "open with": always double-quoted.
  • "drag & drop": only sometimes double-quoted (when white spaces are present).

Because of the very abstruse escape rules of the batch language, it's probably better to use a lot of intermediate variables to build the string; otherwise you'll get insane.

We can use:

  • ^ escapes the ". But only in unquoted strings, see here!

  • set x=%x:"=% is a substitution of " by nothing in the string in variable x.

  • The single quote ' is a normal character and does not need to be escaped.

So the following batch file does the job:

@echo off
C:
cd C:\cygwin64\bin
set pdfpath=%1
set pdfpath=%pdfpath:"=%
set cygpart=$(cygpath '%pdfpath%')
set fullcmd=/usr/bin/zathura \^"%cygpart%\^"
bash -l -c "%fullcmd%"
pause

There may be some strange filename which still breaks it. But at least it works for all somewhat normal filenames, including ones containing white spaces, parentheses, brackets.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.