/lib/CFileDialog.ahk

http://github.com/Skiouros/Macro · AutoHotKey · 92 lines · 33 code · 10 blank · 49 comment · 3 complexity · 16dc14f7d1335b9186999a6c9515231a MD5 · raw file

  1. /*
  2. Class: CFileDialog
  3. This class is used for open/save file dialogs.
  4. */
  5. Class CFileDialog
  6. {
  7. /*
  8. Variable: Mode
  9. Possible values:
  10. - Open: Shows a dialog to select a file which is opened.
  11. - Save: Shows a dialog to select a file which is saved.
  12. */
  13. ;Variable: Multi
  14. ;If true, multiple files can be selected.
  15. ;Variable: FileMustExist
  16. ;If true, the dialog requires that the file exists. Often used to open files.
  17. ;Variable: PathMustExist
  18. ;If true, the dialog requires that the path exists.
  19. ;Variable: CreateNewFilePrompt
  20. ;If true, the dialog will ask the user to create a new file if it does not exist.
  21. ;Variable: OverwriteFilePrompt
  22. ;If true, the dialog asks the user to overwrite an existing file.
  23. ;Variable: FollowShortcuts
  24. ;If true(default), shortcuts are followed. Otherwise the shortcut file itself will be used.
  25. ;Variable: InitialDirectory
  26. ;Initial directory of the dialog.
  27. ;Variable: Filename
  28. ;Initial filename in the filename edit field.
  29. ;Variable: Title
  30. ;Title of the dialog window.
  31. ;Variable: Filter
  32. ;File extension filter. AHK only supports a single user defined entry, but it may have different file extensions. An example would be "Audio files (*.mp3;*.wav)".
  33. /*
  34. Constructor: __New
  35. Creates the instance but does not show the dialog. It can be used to store a configuration of the dialog that can be reused.
  36. Parameters:
  37. Mode - Possible values:
  38. - Open: Shows a dialog to select a file which is opened.
  39. - Save: Shows a dialog to select a file which is saved.
  40. Returns:
  41. An instance of the <CFileDialog> class.
  42. */
  43. __New(Mode="")
  44. {
  45. this.Mode := Mode ? Mode : "Open"
  46. this.Multi := 0
  47. this.FileMustExist := 0
  48. this.PathMustExist := 0
  49. this.CreateNewFilePrompt := 0
  50. this.OverwriteFilePrompt := 0
  51. this.FollowShortcuts := 1
  52. this.InitialDirectory := ""
  53. this.Filename := ""
  54. this.Title := ""
  55. this.Filter := ""
  56. }
  57. __Delete()
  58. {
  59. }
  60. /*
  61. Function: Show
  62. Shows a file dialog window. To show a modal window, set OwnDialogs := 1 for the GUI thread that calls this.
  63. Returns:
  64. 1 if the user confirmed the selection and pressed OK, 0 if the file selection was cancelled.
  65. */
  66. Show()
  67. {
  68. FileSelectFile, result, % (this.Multi ? "M" : "" ) (this.Mode = "Open" ? "" : "S") ((this.FileMustExist > 0) + (this.PathMustExist>0) * 2 + (this.CreateNewFilePrompt > 0) * 8 + (this.OverwriteFilePrompt > 0) * 16 + (this.FollowShortcuts = 0) * 32), % this.InitialDirectory (this.InitialDirectory && this.Filename ? "\" : "") this.Filename, % this.Title, % this.Filter
  69. if(Multi)
  70. {
  71. this.Filenames := Array()
  72. Loop, Parse, result, `n
  73. this.Filenames.Insert(A_LoopField)
  74. }
  75. else
  76. this.Filename := result
  77. return result != ""
  78. }
  79. }