Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 4 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
Worked for me, but only after restarting Windows Explorer. (Rebooting would probably work too.) – Ajedi32 Mar 31 '15 at 20:45

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




share|improve this answer

I compiled my Ruby program by OCRA gem, and it works if I drag "filename.txt" directly onto the compiled executable file (*.exe) in the Windows 7 explorer. Here is the beginning of my Ruby:

reports='filename.txt' #Opening the file
f ='output.txt', 'w')
File.readlines(reports).each do |line|
   #processing, such as f.puts

In case you want to support drag and drop unknown file names, you can try

filename = Dir.entries('.').detect {|z| z.match / whatever pattern or extension  /}, 'w')

No windows registry modifications or default opening program setting required.

share|improve this answer

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .