10

I would like to use a bash shell script from the SendTo folder. When I put a shortcut to a batch or exe into theSendTo folder it shows up in the shell Send To context submenu, but when the shortcut is pointing at a shell script it doesn't.

The OS I'm testing this on is Win7 Home Premium SP1. The extension is .sh which has been associated with MinGW's bash.exe.

My shell script has a .sh extension and I've tried disassociated the .sh extension (I think that MinGW set it up initially, but that didn't work) using this utility and tried to reassociate it to bash using:

ftype ShellScript=c:\MinGW\msys\1.0\bin\bash.exe -c "'%1' %2"
assoc .sh=ShellScript

in an admin cmd shell. Though this works at a command prompt and the Explorer shell (via double click), it won't show up in the Send To menu and it won't accept a parameter by dragging a file on top of the script directly.

Does anyone know how I would do this?

Adrian
  • 641

4 Answers4

5

This will enable Drag & Drop to any script. You can place one of them in SendTo folder and use it afterwards.

Registry Export:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\ShellFile]

[HKEY_CLASSES_ROOT\ShellFile\Shell]

[HKEY_CLASSES_ROOT\ShellFile\Shell\Open]

[HKEY_CLASSES_ROOT\ShellFile\Shell\Open\Command]
@=hex(2):43,00,3a,00,5c,00,70,00,61,00,74,00,68,00,5f,00,65,00,78,00,74,00,5c,\
  00,62,00,61,00,73,00,68,00,2e,00,65,00,78,00,65,00,20,00,2d,00,63,00,20,00,\
  22,00,73,00,6f,00,75,00,72,00,63,00,65,00,20,00,24,00,30,00,3b,00,72,00,65,\
  00,61,00,64,00,22,00,20,00,25,00,31,00,20,00,25,00,2a,00,00,00

[HKEY_CLASSES_ROOT\ShellFile\ShellEx]

[HKEY_CLASSES_ROOT\ShellFile\ShellEx\DropHandler]
@="{86C86720-42A0-1069-A2E8-08002B30309D}"

The hex part actually is "C:\cygwin\bin\bash.exe -c "source $0;read" %1 %*" which gets encoded in the export.

You probably will want to remove the read after testing, so you can write scripts that just perform a task without leaving an open window. If you need this for single scripts, you can always add it add their end.

Use assoc .ext=ShellFile after importing to link any file extension you want with this functionality. The DropHandler in this example works for Windows XP and Windows 7 (probably others too) and basically means "run the command, with all dropped filenames as arguments".

Use this as a script (echotest.ext) to test basic functionality:

echo $0 $*;
Squeezy
  • 9,850
  • 1
  • 18
  • 18
4

Here is how to pass an argument to a bash shell function via SendTo (or via drag & drop). As an example I used the builtin echo. Set the target for your link in the SendTo folder as follows:

C:\cygwin\bin\bash.exe -c "echo Argument: $0; read"

Here $0 stands for the first argument after the given command linea), i.e. the full filename of the file on which the sendto action was executed. read keeps the window open, so that you can read the message. (I tested this with cygwin's bash, but I think mingw's bash should work, too.)

In your case, the target should be

c:\MinGW\msys\1.0\bin\bash.exe -c "/path/to/your/script.sh $0; read"

Now your script can process the filename. But note, that the filename is passed to the script as the first argument, so inside the script the filename is referenced as $1.


Last, but not least here are two screenshots as a summary:

enter image description here


enter image description here


a) You quoted man bash:

-c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

To understand that, use e.g. the following target line:

C:\cygwin\bin\bash.exe -c "echo This is $0; read" Foo Bar Baz

This will print This is Foo, while

C:\cygwin\bin\bash.exe -c "echo This is $2; read" Foo Bar Baz

will print This is Baz. So the "string" is everything between the apostrophes, and Foo Bar Baz are the arguments.

mpy
  • 28,816
1

If Windows refuses to link to a .sh file, you could maybe try using a .bat file that invokes the .sh script.

If that doesn't work, you could also try to compile the .bat into .exe.
A quick google found :

Batch Compiler
Bat-To-Exe

harrymc
  • 498,455
0

Try this updated version of the REG file (note I use 64-bit Windows 7; use System32 instead of SysWOW64 if you have 32-bit Win7, Vista, or XP):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\ShellFile]
@="Shell Script"

[HKEY_CLASSES_ROOT\ShellFile\DefaultIcon]
@="C:Windows\\SysWOW64\\imageres.dll,-68"

[HKEY_CLASSES_ROOT\ShellFile\shell]

[HKEY_CLASSES_ROOT\ShellFile\shell\edit]

[HKEY_CLASSES_ROOT\ShellFile\shell\edit\command]
@="C:\\Windows\\SysWOW64\\NOTEPAD.EXE %1"

[HKEY_CLASSES_ROOT\ShellFile\shell\open]
"EditFlags"=hex:00,00,00,00

[HKEY_CLASSES_ROOT\ShellFile\shell\open\command]
@="C:\\MinGW\\msys\\1.0\\bin\\bash.exe -c \"source $0;\" \"%1 %*\""

[HKEY_CLASSES_ROOT\ShellFile\shell\print]

[HKEY_CLASSES_ROOT\ShellFile\shell\print\command]
@="C:\\Windows\\SysWOW64\\NOTEPAD.EXE /p %1"

[HKEY_CLASSES_ROOT\ShellFile\shell\runas]
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\ShellFile\shell\runas\command]
@="C:\\MinGW\\msys\\1.0\\bin\\bash.exe -c \"source $0;\" \"%1 %*\""

[HKEY_CLASSES_ROOT\ShellFile\shell\runasuser]
@="@shell32.dll,-50944"
"Extended"=""
"SuppressionPolicyEx"="{F211AA05-D4DF-4370-A2A0-9F19C09756A7}"

[HKEY_CLASSES_ROOT\ShellFile\shell\runasuser\command]
"DelegatExecute"="{ea72d00e-4960-42fa-ba92-7792a7944c1d}"

[HKEY_CLASSES_ROOT\ShellFile\ShellEx]

[HKEY_CLASSES_ROOT\ShellFile\ShellEx\DropHandler]
@="{86C86720-42A0-1069-A2E8-08002B30309D}"

This will allow your shell scripts to be also run as Administrator just as any .bat file does.  In other words, it makes all shell scripts UAC compatible when using Windows Vista and Windows 7 or 8.

kramlat
  • 21