Repository: beeware/toga

Issue:
initial_directory  ignored in cocoa dialogs
**Describe the bug**
initial_directory for file_open_dialog and select_folder_dialog ignored in cocoa

**To Reproduce**
Steps to reproduce the behavior:

```
fname = self.main_window.select_folder_dialog(
                "Save File Here",
                initial_directory="/User/directory/directory" ,
            )
```

initial_directory argument has no effect

**Expected behavior**
Dialog would open in directory as instructed.


**Environment:**
 - Operating System: MacOSX 10.15.7
 - Python version:3.8
 - Software versions:
   - Briefcase:0.3.3
   - Toga:0.3.3dev25
   - ...

**Additional context**
I will pull and try and fix



Discussion:

Resolving patch:
diff --git a/examples/dialogs/dialogs/app.py b/examples/dialogs/dialogs/app.py
--- a/examples/dialogs/dialogs/app.py
+++ b/examples/dialogs/dialogs/app.py
@@ -1,4 +1,5 @@
 import toga
+from pathlib import Path
 from toga.constants import COLUMN
 from toga.style import Pack
 
@@ -72,6 +73,20 @@ def action_open_file_dialog_multi(self, widget):
         except ValueError:
             self.label.text = "Open file dialog was canceled"
 
+    def action_open_file_dialog_with_inital_folder(self, widget):
+        try:
+            fname = self.main_window.open_file_dialog(
+                title="Open file with Toga in home folder",
+                initial_directory=Path.home(),
+                multiselect=False
+            )
+            if fname is not None:
+                self.label.text = "File to open:" + fname
+            else:
+                self.label.text = "No file selected!"
+        except ValueError:
+            self.label.text = "Open file dialog was canceled"
+
     def action_select_folder_dialog(self, widget):
         try:
             path_names = self.main_window.select_folder_dialog(
@@ -91,6 +106,16 @@ def action_select_folder_dialog_multi(self, widget):
         except ValueError:
             self.label.text = "Folders select dialog was canceled"
 
+    def action_select_folder_dialog_with_initial_folder(self, widget):
+        try:
+            path_names = self.main_window.select_folder_dialog(
+                title="Select folder with Toga in current folder",
+                initial_directory=Path.cwd(),
+            )
+            self.label.text = "Folder selected:" + ','.join([path for path in path_names])
+        except ValueError:
+            self.label.text = "Folder select dialog was canceled"
+
     def action_save_file_dialog(self, widget):
         fname = 'Toga_file.txt'
         try:
@@ -191,6 +216,11 @@ def startup(self):
             on_press=self.action_open_file_dialog_multi,
             style=btn_style
         )
+        btn_open_init_folder = toga.Button(
+            'Open File In Current Folder',
+            on_press=self.action_open_file_dialog_with_inital_folder,
+        )
+
         btn_save = toga.Button('Save File', on_press=self.action_save_file_dialog, style=btn_style)
         btn_select = toga.Button('Select Folder', on_press=self.action_select_folder_dialog, style=btn_style)
         btn_select_multi = toga.Button(
@@ -198,6 +228,12 @@ def startup(self):
             on_press=self.action_select_folder_dialog_multi,
             style=btn_style
         )
+        btn_select_init_folder = toga.Button(
+            'Select Folder In Current Folder ',
+            on_press=self.action_select_folder_dialog_with_initial_folder,
+            style=btn_style
+        )
+
         btn_open_secondary_window = toga.Button(
             'Open Secondary Window',
             on_press=self.action_open_secondary_window,
@@ -220,10 +256,12 @@ def startup(self):
                 btn_error,
                 btn_open,
                 btn_open_filtered,
+                btn_open_multi,
+                btn_open_init_folder,
                 btn_save,
                 btn_select,
                 btn_select_multi,
-                btn_open_multi,
+                btn_select_init_folder,
                 btn_open_secondary_window,
                 btn_close_secondary_window,
                 btn_clear,
diff --git a/src/cocoa/toga_cocoa/dialogs.py b/src/cocoa/toga_cocoa/dialogs.py
--- a/src/cocoa/toga_cocoa/dialogs.py
+++ b/src/cocoa/toga_cocoa/dialogs.py
@@ -1,3 +1,4 @@
+from pathlib import Path
 from .libs import (
     NSAlert,
     NSAlertFirstButtonReturn,
@@ -9,7 +10,8 @@
     NSOpenPanel,
     NSSavePanel,
     NSScrollView,
-    NSTextView
+    NSTextView,
+    NSURL
 )
 
 
@@ -113,7 +115,7 @@ def save_file(window, title, suggested_filename, file_types=None):
     return None
 
 
-def open_file(window, title, file_types, multiselect):
+def open_file(window, title, initial_directory, file_types, multiselect):
     """Cocoa open file dialog implementation.
 
     We restrict the panel invocation to only choose files. We also allow
@@ -122,6 +124,7 @@ def open_file(window, title, file_types, multiselect):
     Args:
         window: The window this dialog belongs to.
         title: Title of the modal.
+        initial_directory: directory where modal shall open with
         file_types: Ignored for now.
         multiselect: Flag to allow multiple file selection.
     Returns:
@@ -131,6 +134,8 @@ def open_file(window, title, file_types, multiselect):
     # Initialize and configure the panel.
     panel = NSOpenPanel.alloc().init()
     panel.title = title
+    if initial_directory is not None:
+        panel.directoryURL = NSURL.URLWithString(str(Path(initial_directory).as_uri()))
     panel.allowedFileTypes = file_types
     panel.allowsMultipleSelection = multiselect
     panel.canChooseDirectories = False
@@ -146,19 +151,21 @@ def open_file(window, title, file_types, multiselect):
         return filename_or_filenames
 
 
-def select_folder(window, title, multiselect):
+def select_folder(window, title, initial_directory, multiselect):
     """Cocoa select folder dialog implementation.
 
     Args:
         window: Window dialog belongs to.
         title: Title of the dialog.
+        initial_directory: directory where modal shall open with
         multiselect: Flag to allow multiple folder selection.
     Returns:
         (list) A list of folder paths.
     """
     dialog = NSOpenPanel.alloc().init()
     dialog.title = title
-
+    if initial_directory is not None:
+        dialog.directoryURL = NSURL.URLWithString(str(Path(initial_directory).as_uri()))
     dialog.canChooseFiles = False
     dialog.canChooseDirectories = True
     dialog.resolvesAliases = True
diff --git a/src/cocoa/toga_cocoa/window.py b/src/cocoa/toga_cocoa/window.py
--- a/src/cocoa/toga_cocoa/window.py
+++ b/src/cocoa/toga_cocoa/window.py
@@ -293,7 +293,7 @@ def save_file_dialog(self, title, suggested_filename, file_types):
         return dialogs.save_file(self.interface, title, suggested_filename, file_types)
 
     def open_file_dialog(self, title, initial_directory, file_types, multiselect):
-        return dialogs.open_file(self.interface, title, file_types, multiselect)
+        return dialogs.open_file(self.interface, title, initial_directory, file_types, multiselect)
 
     def select_folder_dialog(self, title, initial_directory, multiselect):
-        return dialogs.select_folder(self.interface, title, multiselect)
+        return dialogs.select_folder(self.interface, title, initial_directory, multiselect)


Test patch: