I tried doing it the DL route but apparently there is some problem with Ruby properly creating the struct to be passed to the 'GetOpenFileNameA' function (at least, that's what I've encountered). But I have found a different way...
Fortunately for me, I have delved into AutoIT scripting which allows me to create an Open Dialog Box. So I then decided to write a simple script to pass all the necessary params to this application from Ruby (i.e. Dialog message and filter) and the AutoIT application returns the filename that is selected or nil if canceled.
AutoIT code:
If $CmdLine[0] < 1 Then
ConsoleWriteError("No arguments given")
Exit
EndIf
$op = $CmdLine[1]
Switch($op)
Case "open"
If $CmdLine[0] < 3 Then
ConsoleWriteError("open command requires message and filter arguments.")
Exit
EndIf
$msg = $CmdLine[2]
$filter = $CmdLine[3]
$selected = FileOpenDialog($msg, "", $filter, 1)
If @error Then
ConsoleWriteError("Open Dialog problem")
Exit
EndIf
ConsoleWrite($selected)
EndSwitch
Ruby code:
f = IO.popen("controls.exe open \"Select a file\" \"Text File(*.txt)\"")
puts f.gets
This seems to work nicely. A bit of a round-trip but allows you to get what you need.
For those who don't know, AutoIT is a Windows-based automation scripting system (console and GUI wise, see some of my AutoIT posts).
Cheers!
3 comments:
I like AutoIT too. However, this alse possible:
require "win32ole"
cd = WIN32OLE.new("MSComDlg.CommonDialog")
cd.filter = "All Files(*.*)|*.*" +"|Ruby Files(*.rb)|*.rb"
cd.filterIndex = 2
cd.maxFileSize = 128
cd.showOpen()
file = cd.fileName # Retrieve file, path
if not file or file==""
puts "No filename entered."
else
puts "The user selected: #{file}\n"
end
From:
http://www.java2s.com/Code/Ruby/Windows-Platform/Openfiledialog.htm
Ah, the COM route, of course.
Just a pity your post is a bit late.
Thanks anyway!
Late for you, but not for other, future readers. :)
Post a Comment