Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Linux, dialog-node.fileselect browses for a file. On Windows, it browses for a directory. #12

Open
apboon opened this issue Nov 1, 2019 · 4 comments

Comments

@apboon
Copy link

apboon commented Nov 1, 2019

On Linux, dialog-node.fileselect browses for a file. On Windows, it browses for a directory. Which is differs from what it claims it would do. This is because on Windows, shell.BrowseForFolder is called, which indeed shows a directory selection dialog instead of a file selection dialog (common file dialog).

@apboon
Copy link
Author

apboon commented Nov 1, 2019

I saw the following on https://gist.github.com/mlhaufe/1034241

Function BrowseForFile()
    Dim shell : Set shell = CreateObject("Shell.Application")
    Dim file : Set file = shell.BrowseForFolder(0, "Choose a file:", &H4000, "C:\")
    BrowseForFile = file.self.Path
End Function

Looking at the above code it seems that calling shell.BrowseForFolder is the way to go, but the difference is in the third argument. dialog-node uses 0 in shell.BrowseForFolder(0, dialogTitle, 0), and the example code I found uses &H4000..

Needs more investigation..

@apboon
Copy link
Author

apboon commented Nov 1, 2019

Nope.. shell.BrowseForFolder isn't the right call for the job of selecting a file. At most, shell.BrowseForFolder (in combination with &H4000) can select directories and files, but never just files (source: https://docs.microsoft.com/en-us/windows/win32/shell/shell-browseforfolder).

This example will do the trick (source: https://stackoverflow.com/questions/38643487/vbscript-browseforfile-function-how-to-specify-file-types)

Function GetFileDlgEx(sIniDir,sFilter,sTitle) 
Set oDlg = CreateObject("WScript.Shell").Exec("mshta.exe ""about:<object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><script>moveTo(0,-9999);eval(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0).Read("&Len(sIniDir)+Len(sFilter)+Len(sTitle)+41&"));function window.onload(){var p=/[^\0]*/;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(p.exec(d.object.openfiledlg(iniDir,null,filter,title)));close();}</script><hta:application showintaskbar=no />""") 
oDlg.StdIn.Write "var iniDir='" & sIniDir & "';var filter='" & sFilter & "';var title='" & sTitle & "';" 
GetFileDlgEx = oDlg.StdOut.ReadAll 
End Function
set fso = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = fso.GetAbsolutePathName(".")
sIniDir = CurrentDirectory &"\Myfile.csv" 
sFilter = "csv files (*.csv)|*.csv|txt files (*.txt)|*.txt|log files (*.log)|*.log|" 
sTitle = "Put a title of your program here :) (-_°)" 
MyFile = GetFileDlgEx(Replace(sIniDir,"\","\\"),sFilter,sTitle) 
wscript.echo MyFile

(painfully not indented.. -__-)

@leefsmp
Copy link

leefsmp commented Jan 9, 2024

this is a VB sample ... :(

@apboon
Copy link
Author

apboon commented Jan 9, 2024

You are correct.

I found no other way to call native Win32 shell APIs from Node.js.

Maybe there's another bright mind out there that knows how to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants