@@ -79,7 +79,8 @@ extension NSDocumentController {
7979 onCancel: @escaping ( ) -> Void
8080 ) {
8181
82- // 1. Configure NSSavePanel ------------------------------------------------
82+ // 1 ────────────────────────────────────────────────────────────────
83+ // Configure the NSSavePanel
8384 let panel = NSSavePanel ( )
8485 panel. prompt = configuration. prompt
8586 panel. title = configuration. title
@@ -96,23 +97,54 @@ extension NSDocumentController {
9697 case . folder:
9798 panel. nameFieldStringValue =
9899 URL ( fileURLWithPath: configuration. defaultFileName)
99- . deletingPathExtension ( ) . lastPathComponent
100- panel. allowedContentTypes = [ ] // treat as folder
100+ . deletingPathExtension ( )
101+ . lastPathComponent
102+ panel. allowedContentTypes = [ ] // treat as plain folder
101103 }
102104
103105 DispatchQueue . main. async { onDialogPresented ( ) }
106+
104107 guard panel. runModal ( ) == . OK,
105- let baseURL = panel. url else { onCancel ( ) ; return }
108+ let baseURL = panel. url // e.g. …/ProjectName
109+ else {
110+ onCancel ( )
111+ return
112+ }
113+
114+ // 2 ────────────────────────────────────────────────────────────────
115+ // For a *folder* document, create the workspace directory up front.
116+ if mode == . folder {
117+ do {
118+ try FileManager . default. createDirectory (
119+ at: baseURL,
120+ withIntermediateDirectories: true ,
121+ attributes: nil
122+ )
123+ } catch {
124+ NSAlert ( error: error) . runModal ( )
125+ onCancel ( )
126+ return
127+ }
128+ }
129+
130+ // 3 ────────────────────────────────────────────────────────────────
131+ // Derive the final URL of the actual NSDocument header file.
132+ // …/ProjectName/ProjectName.circuitproj (folder mode)
133+ // …/SomeFile.circuitproj (file mode)
134+ let ext = configuration
135+ . defaultFileType
136+ . preferredFilenameExtension ?? " file "
106137
107- // 2. Derive the final document file URL ----------------------------------
108- let ext = configuration. defaultFileType. preferredFilenameExtension ?? " file "
109138 let finalURL = ( mode == . folder)
110139 ? baseURL. appendingPathComponent ( " \( baseURL. lastPathComponent) . \( ext) " )
111140 : baseURL
112141
113- // 3. Create, write, and open the NSDocument ------------------------------
142+ // 4 ────────────────────────────────────────────────────────────────
143+ // Create, write and open the document.
114144 do {
115- let document = try makeUntitledDocument ( ofType: configuration. defaultFileType. identifier)
145+ let document = try makeUntitledDocument (
146+ ofType: configuration. defaultFileType. identifier
147+ )
116148 document. fileURL = finalURL
117149
118150 try document. write (
0 commit comments