Windows エクスプローラーで Ruby スクリプトにファイルをドロップできるようにするにはどうすればよいでしょうか

Windows エクスプローラーで Ruby スクリプトにファイルをドロップできるようにするにはどうすればよいでしょうか

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).

答え1

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.

答え2

Based on @cHau's answer、ここにRubyスクリプトのドロップサポートを設定するregファイルがあります(完全にテストされていないため、おそらくコンピュータが破壊されます)(要旨):

Windows レジストリ エディター バージョン 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}"

答え3

私はRubyプログラムを次のようにコンパイルしましたOCRA の宝石Windows 7 エクスプローラーで「filename.txt」をコンパイルされた実行可能ファイル (*.exe) に直接ドラッグすると動作します。以下は私の Ruby の冒頭です。

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

不明なファイル名のドラッグアンドドロップをサポートしたい場合は、以下をお試しください。

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

Windows レジストリの変更やデフォルトの起動プログラムの設定は必要ありません。

答え4

ファイルの拡張子を使用して、このスクリプトのデフォルト アプリケーションとして Ruby を設定してみてください。

見るここどうやってするの。

関連情報