Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Windows Explorer (Win7) I can drop a file onto a batch (.bat) file, and the batch file is executed with the path to the dropped file as the first command line argument. How can I do the same thing with a Ruby script?

I don't mean using the argument within the script. The Ruby script doesn't get highlighted as a drop target when the file is over the icon, and dropping it just reorders the icons. I want it to behave the same as batch files (or any other executable).

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

The types of files that can have stuff dropped on them have keys in the registry to tell Windows how to run them. If you have your script's extension set up so that Ruby runs the script by default, then you're halfway there.

(Obligatory warning: this is a hack. Messing with your registry can royally screw up your computer. If you care at all about your data and don't trust me (as well you shouldn't), have a backup before you continue.)

  1. Start up the registry editor. It's called "regedit". (If you're running Vista or Windows 7, you may need to right-click it and "run as administrator".)
  2. Open up HKEY_CLASSES_ROOT\batfile\ShellEx\DropHandler. There will be a GUID as the default value. It happens to be the value used by batch files, EXE files, and a few others. As i'm not aware of any COM objects that Ruby uses, we're gonna abuse this one. It's labeled in the registry as ".exe drop target". What it seems to do is "start" the script, passing the files' names as args.
  3. Double-click the "(Default)" to open the value. Copy the guid, and then cancel out of the edit box.
  4. Now find "RubyFile" and "RubyWFile" inside HKEY_CLASSES_ROOT. The first one's for console scripts, and the latter seems to be for GUI scripts. If you use a different Ruby interpreter than i do, or if you set up the default handler yourself, you may find the keys "rbfile" and/or "rbwfile" instead.
  5. Right-click one of the keys, and create a new key in there called "ShellEx" if it doesn't already exist. Then create another key inside that new one, called "DropHandler".
  6. Edit the default value in that DropHandler key, and paste in the GUID you copied in step 3. Click OK.
  7. Repeat steps 5 and 6 with the other key.

Once that's done, you should be able to drag files onto your script.

share|improve this answer
    
On XP and 7 HKEY_CLASSES_ROOT\batfile\ShellEx\DropHandler is always {86C86720-42A0-1069-A2E8-08002B30309D} so that could save a step. Also on XP it doesn't have 'RubyFile', but using 'rbfile' works. On 7 it has both keys but only 'RubyFile' works. Cheers! –  Ben Scott Oct 9 '10 at 23:26
    
Oh. I just more carefully read step 4 re RubyFile vs rbfile. Very good. –  Ben Scott Oct 9 '10 at 23:41
    
I wasn't sure whether it'd always be the same (different Windows versions, 32 vs 64 bit, etc). But i also figured that either way, copy-n-paste would be easier to get right. :) –  cHao Oct 10 '10 at 1:24
add comment

Based on @cHau's answer, here is a reg file that will set up drop support for Ruby scripts (not fully tested, will probably destroy your computer) (gist):

Windows Registry Editor Version 5.00

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

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

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

[HKEY_CLASSES_ROOT\RubyWFile\ShellEx\DropHandler]
@="{86C86720-42A0-1069-A2E8-08002B30309D}"
share|improve this answer
add comment

Try setting Ruby as the default application for this scripts, by using the file's extension.

See here how to do it.

share|improve this answer
    
It already is the default application. If I double click the script it executes, but it doesn't if I try to drag another file onto the script. –  Ben Scott Oct 9 '10 at 6:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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