FileSelectFile() : Supports multi-line filter

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

FileSelectFile() : Supports multi-line filter

22 Sep 2020, 09:01

FileSelectFile(Options, RootDir\Filename, Title, Filter)
Please refer the docs: FileSelectFile for parameter usage... Also, refer docs: Gui +OwnDialogs
 
The functionality is similar to the built-in command except for the last parameter Filter, which will support a multiline filter.
A Filter string should be pipe delimited and a double pipe will preselect the item before it. (Just like we do for a ListBox)
If the first character of Filter string is a Pipe, then items will be shown without extension types,
i.e., AHK files (*.ahk) will be displayed as AHK files.. yet the filter would work.

There are a few additional options which has been documented in second post.
 
Here is an example call:

Code: Select all

#NoEnv
#SingleInstance, Force
Menu, Tray, Icon, user32.dll, 5
Gui +OwnDialogs

Filter := "Scripts (*.ahk)|Interpreter (AutoHotkey*.exe)||Help (AutoHotkey.chm)|GPL (License.txt)|All (*.*)"
MsgBox % FileSelectFile("m35", A_AhkPath, "AutoHotkey files", Filter)

; Paste FileSelectFile() below
..and here is the screenshot:
 
Image
If the first character of Filter string is pipe,
for eg. |Scripts (*.ahk)|Interpreter (AutoHotkey*.exe)||Help (AutoHotkey.chm)|GPL (License.txt)|All (*.*)
then the drop down would look like this:
 
Image
 
 
The function:
 

Code: Select all

FileSelectFile( Options:="", RootDir:="", Title:="", Filter:="" ) {   ;   v0.72 by SKAN on D39R/D39U
Local                                                                 ;     @ tiny.cc/fileselectfile

  Option := StrSplit(Options, "|", A_Space)                           ;            Options parameter
  AltWnd := Round(Option[4]),  xFlags := Round(Option[3]),  DefExt := Trim(Option[2], ". ")
  Main := Option[1],  o := SubStr(Main, 1, 1),  f := Round( SubStr(Main, (o="S" || o="M" ? 2 : 1)) )

  nFlags := ( (OFN_ENABLEXPLORER   :=0x80000)      | (f&1  ? (OFN_FILEMUSTEXIST     :=0x001000) : 0)
  | (f&2    ? (OFN_PATHMUSTEXIST   :=0x00800) : 0) | (f&8  ? (OFN_CREATEPROMPT      :=0x002000) : 0)
  | (f&16   ? (OFN_OVERWRITEPROMPT :=0x00002) : 0) | (f&32 ? (OFN_NODEREFERENCELINKS:=0x100000) : 0)
  | (o="M"  ? (OFN_ALLOWMULTISELECT:=0x00200) : 0) | xFlags )

  q := ( (nFlags & (OFN_OVERWRITEPROMPT := 0x00002)) && !(nFlags & (OFN_CREATEPROMPT := 0x002000)) )
  GetFileName := ( o="S" || q ) ?  "comdlg32.dll\GetSaveFileName"  :  "comdlg32.dll\GetOpenFileName"

  RootDir  .= InStr(FileExist(RootDir), "D", 1) ? "\" : ""            ;            RootDir parameter
  SplitPath, RootDir, FileName, InitialDir
  FileName := (InitialDir="::" ? "" : FileName)   ; ignore CLSID
  nMaxFile := VarSetCapacity(File, o="M" ? 65536:1024, 0), StrPut(FileName, &File)
                                                                                   
  Title := StrLen(Title) ? Title : "Select File - " . A_ScriptName    ;              Title parameter
                                                                                  
  Filter := ( ( df:=(Asc( f := Filter)=124) ) ? LTrim(f,"|") : f )    ;             Filter parameter
  n := InStr(Filter, "||",, 0)
  StrReplace( StrReplace( SubStr((f := Filter), 1, n), "||", "|" ), "|", "|", nFilterIndex )

  Loop, Parse, % ( Filter ? Filter : "All files (*.*)|Text Documents (*.txt)" ), |,  % " " . f := ""
  If ( (n := InStr( (l := A_LoopField), "(",, 0))
    && (v := StrReplace( Trim(SubStr(l, n), "( )"), " ")) )
        f .= Format(df ? "{1:}|{2:}|" : "{1:}({2:})|{2:}|", SubStr(l, 1, n-1), v)

  Filter := (f . "|"),  x := (A_IsUnicode ? 2 : 1),  Null := 0
  While ( f := InStr(Filter, "|",, 0) )            
    NumPut(Null, Filter, (f*x)-x, "Char")         

  DetectHiddenWindows, % ("On", DHW := A_DetectHiddenWindows)         ; Setting owner for the dialog
  SetWinDelay, % (0, SWD := A_WinDelay)
  SplashImage, 8:, x0 y0 w0 h0 B Hide,,, OwnDialogTest
  WinWait, OwnDialogTest ahk_class AutoHotkey2,, 10
  hOwner := DllCall("GetWindow", "Ptr",WinExist(), "Int",GW_OWNER:=4 ,"Ptr")
  SplashImage, 8:Off
  hOwner := (hOwner!=A_ScriptHwnd) ? hOwner : WinExist("ahk_id" . AltWnd)
  SetWinDelay, %SWD%
  DetectHiddenWindows, %DHW%

  P8 := (A_PtrSize=8),    VarSetCapacity(OFN, P8 ? 168 : 96, 0)       ; Creating OPENFILENAME Struct
  NumPut(P8 ? 136 : 76, OFN, "Int")
  NumPut(hOwner,       OFN, P8 ? 08 : 04, "Ptr"),     NumPut(&Filter,      OFN, P8 ? 24 : 12, "Ptr")
  NumPut(nFilterIndex, OFN, P8 ? 44 : 24, "Int"),     NumPut(&File,        OFN, P8 ? 48 : 28, "Ptr")
  NumPut(nMaxFile,     OFN, P8 ? 56 : 32, "Int"),     NumPut(&InitialDir,  OFN, P8 ? 80 : 44, "Ptr")
  NumPut(&Title,       OFN, P8 ? 88 : 48, "Ptr"),     NumPut(nFlags,       OFN, P8 ? 96 : 52, "Int")
  NumPut((StrLen(DefExt) ? &DefExt : 0), OFN, P8 ? 104 : 60, "Int")

  f := (hOwner ? DllCall("SetForegroundWindow", "Ptr",hOwner) : 0)    ;         Calling the function
  If !DllCall(GetFileName, "Ptr",&OFN)
    Return ("",  ErrorLevel := 1)

  OutputVar := "",  f := &File                                        ;      Extracting file(s) list
  While ( Line := StrGet(f) )
    OutputVar .= Line . "`n",  f += ( (StrLen(Line)+1) * x )

  DllCall("SetLastError", "Int",NumGet(OFN, P8 ? 44 : 24, "UInt"))
Return (Rtrim(OutputVar, "`n"),  ErrorLevel := 0)
}
 
0.72A_LastError will contain the index of selected Filter. (Valid only if ErrorLevel is 0). (More)
0.69 : FileSelectFile() Options enhanced to be consistent with FileSelectFile command.
0.66 : Support for Gui +OwnDialogs. Added a feature to suppress extension types from showing in dropdown list.
0.63 : Fixed a glitch that prevented Dialog from showing in 64bit AHK.
0.60 : First release
 
 
 
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

FileSelectFile() : Additional options

24 Sep 2020, 13:21

The Options parameter of FileSelectFile() can accept up to 4 values when values are delimited with Pipe character.
The 4 values should appear in following order and unneeded ones may be omitted.
 
  • Default : Default options of FileSelectFile command
  • Extension : Default file extension to be used when user doesn't (want to) type one.
    Note: Extension can only be up to 3 Chars long. For eg., .html will be used as .htm
  • Flags : Any additional flags supported by the API
    List of flags
  • Owner : Handle to the owner window
Lets try these parameters.
Whenever I want test AHK code, I usually create test.ahk in Desktop and edit it.
 
MsgBox % FileSelectFile(16, A_Desktop)
If I type test and click Save the fullpath is returned without any extension.
So, lets force .ahk extension
 
MsgBox % FileSelectFile("16|.ahk", A_Desktop)
If I type test and click Save the fullpath is returned with .ahk extension.
 
Explorer style dialog is huge for this trivial task.
So lets pass the flag OFN_ENABLETEMPLATE := 0x00000040 to show a old style compact dialog.
 
MsgBox % FileSelectFile("16|.ahk|0x40", A_Desktop)
The dialog looks like this:
 
Image
 
All OK, but Icon is missing in caption bar. Lets give the dialog an owner.
MsgBox % FileSelectFile("16|.ahk|0x40|" . A_ScriptHwnd, A_Desktop)
The dialog looks like this:
 
Image
 
 
Demo: A simple script to create/edit test.ahk on Desktop:

Code: Select all

#NoEnv
#SingleInstance, Force
F2::
  ahkFile := FileSelectFile("16|.ahk|0x40|" . A_ScriptHwnd, A_DeskTop . "\test.ahk")
  If (ErrorLevel)
    Return
  FileCopy, %A_WinDir%\ShellNew\Template.ahk, %ahkFile%, 1
  Run, edit "%ahkFile%"
Return

; Paste FileSelectFile() below
 
 
 
OPENFILENAME structure
Peaceful
Posts: 25
Joined: 20 Mar 2017, 06:28

Re: RestartExplorer() : Updated version

25 Sep 2020, 08:47

SKAN Your libraries are perfect.
Please. FileSelectFile multiple Filter function
I don't like the link function below. Please make a function
https://www.autohotkey.com/boards/viewtopic.php?t=53136
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

Re: FileSelectFile() : Supports multi-line filter

25 Sep 2020, 13:42

Peaceful wrote: I don't like the link function below. Please make a function
https://www.autohotkey.com/boards/viewtopic.php?t=53136
What exactly you don't like?
Doesn't it work correctly?
Peaceful
Posts: 25
Joined: 20 Mar 2017, 06:28

Re: FileSelectFile() : Supports multi-line filter

25 Sep 2020, 23:38

I need ChooseFile
Just because your functions are easier and more convenient. LOL
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

FileSelectFile() : Supports multi-line filter

27 Sep 2020, 10:59

Peaceful wrote:
25 Sep 2020, 23:38
your functions are easier and more convenient. LOL
:o Thanks. :) :thumbup:

Done @Peaceful !!
This topic was split from: https://www.autohotkey.com/boards/viewtopic.php?t=81252
Peaceful
Posts: 25
Joined: 20 Mar 2017, 06:28

Re: FileSelectFile() : Supports multi-line filter

27 Sep 2020, 12:44

Thank you for the wonderful function.
However, 64 bits do not work. And there's no icon.
Icon Options Note: (OwnerHwnd)
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=18939

Now this is at night, so I’ll check again in the morning.
I’m sorry for my poor English.
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

Re: FileSelectFile() : Supports multi-line filter

27 Sep 2020, 13:18

Peaceful wrote: Thank you for the wonderful function.
 
:) :thumbup:
Peaceful wrote:However, 64 bits do not work.
 
Sorry about that. A small glitch, I will fix it.
Peaceful wrote:And there's no icon.
 
That is by design. You have to do Gui +Lastfound (Gui should be visible) before calling FileSelectFile() to make that GUI
the owner window.. otherwise the dialog would be unowned.
Okay. I will use A_ScriptHwnd when there is no Lastfound window for the script.
Edit : Use Gui +OwnDialogs.
 
Peaceful wrote: Now this is at night, so I’ll check again in the morning.
I’m sorry for my poor English.
Good night. :)
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

Re: FileSelectFile() : Supports multi-line filter

27 Sep 2020, 17:05

Code fixed/updated.
Additional options now documented
Peaceful
Posts: 25
Joined: 20 Mar 2017, 06:28

Re: FileSelectFile() : Supports multi-line filter

27 Sep 2020, 23:14

Works fine
The extension is displayed. Isn't it a little weird

It should look like this
https://imgur.com/a/aiP9gsj
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

Re: FileSelectFile() : Supports multi-line filter

28 Sep 2020, 00:48

Peaceful wrote: Works fine
The extension is displayed. Isn't it a little weird
No. It is not weird. I've seen it the way it is, for a long time. :)
Its alright to avoid clutter for single extensions, but not for multiple extensions per line.
Here is a screenshot of Save As dialog of Photoshop.
 
Photoshop_SaveAs.png
Photoshop_SaveAs.png (116.12 KiB) Viewed 391 times
 
Peaceful wrote: It should look like this
Image
I've seen that in browsers.
That is okay for "Open".. But not for "Save" dialog.
Peaceful
Posts: 25
Joined: 20 Mar 2017, 06:28

Re: FileSelectFile() : Supports multi-line filter

28 Sep 2020, 02:27

You're right thank you for the great function
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

FileSelectFile() : v0.66

28 Sep 2020, 09:19

Peaceful wrote: You're right thank you for the great function
 
:thumbup: :)
 
Code updated. v0.66

FileSelecFile() can detect +OwnDialogs and set the GUI as owner automatically.
File extensions can be suppressed from being shown in dropdown list by prefixing a pipe before the filter string. (Thanks to @Peaceful)
Peaceful
Posts: 25
Joined: 20 Mar 2017, 06:28

Re: FileSelectFile() : Supports multi-line filter

28 Sep 2020, 12:49

All perfect!
SKAN you are king Thank you very much for the wonderful function
Thank you *100
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

Re: FileSelectFile() : Supports multi-line filter

29 Sep 2020, 05:52

Peaceful wrote:
28 Sep 2020, 12:49
All perfect!
SKAN you are king Thank you very much for the wonderful function
Thank you *100
 
:) :thumbup:

Code updated:

0.69 : FileSelectFile() Options enhanced to be consistent with FileSelectFile command.
Peaceful
Posts: 25
Joined: 20 Mar 2017, 06:28

Re: FileSelectFile() : Supports multi-line filter

29 Sep 2020, 11:07

very nice

Code: Select all

VideoFormat := "*.avi; *.mp4; *.mkv; *.divx; *.flv; *.mov;"
			. " *.mpeg; *.mpg; *.ts; *.m2ts; *.vob; *.webm;"
			. " *.wmv; *.pva; *.ogg; *.ogm; *.m4v; *.3gp"
AudioFormat := "*.avs; *.flac; *.dtshd; *.dts; *.thd; *.truehd;"
			. " *.aac; *.alac; *.ac3; *.eac3; *.m4a; *.mka; *.mp2;"
			. " *.mp3; *.mpa; *.opus; *.wav; *.w64"
SupportedFormat := VideoFormat "; " AudioFormat

Filters := "|All supported media types(" SupportedFormat ")"
		 . "|Video files(" VideoFormat ")"
		 . "|Audio files(" AudioFormat ")"

Gui, +OwnDialogs
MsgBox % FileSelectFile("M", , "Open", Filters)
gwarble
Posts: 461
Joined: 30 Sep 2013, 15:01

Re: FileSelectFile() : Supports multi-line filter

29 Sep 2020, 13:08

awesome! thanks for this

one obscure request... There is an undocumented (i think) trick to get file associations for files without extension to work by using HKEY_CLASSES_ROOT\.\ and I'm wondering if it would be possible to get the same effect here by using a filter like "NC Code (*.)" to get only those files without extension shown? Files with no extensions aren't very normal, but in the CNC machining world they are because of the way the machine memory works. Or maybe there is another trick to filter by empty extension?
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

Re: FileSelectFile() : Supports multi-line filter

29 Sep 2020, 14:19

Peaceful wrote:
29 Sep 2020, 11:07
very nice
Spaces between extensions makes it easy on eyes..
The windows functions don't allow it though. My function is using StrReplace() to remove all those spaces.
This feature wasn't available in the first version. I hope you're using the latest one.
:thumbup:
User avatar
SKAN
Posts: 850
Joined: 29 Sep 2013, 16:58

Re: FileSelectFile() : Supports multi-line filter

29 Sep 2020, 14:26

gwarble wrote: awesome!
 
:) :thumbup:
gwarble wrote:Files with no extensions aren't very normal, but in the CNC machining world they are because of the way the machine memory works. Or maybe there is another trick to filter by empty extension?
 
I don't think windows API will allow it.
How desperate are you? If I can imagine a trick, it would be ugly. :roll:
gwarble
Posts: 461
Joined: 30 Sep 2013, 15:01

Re: FileSelectFile() : Supports multi-line filter

29 Sep 2020, 14:53

Thanks

Well I wouldn't want you to put an ugly trick in your elegant function, but if you came up with one I'd probably use it, as we treat no extension just like any other extension usually driven by the machine tool's software and/or interface (ethernet or memory card) requirements. Its surely not desperately needed as I've gotten by without it for this long :)
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Google [Bot], nathan323 and 20 guests