GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

fileExporter / fileImporter from MacOS File menu?

Forums > SwiftUI

I'm working on a MacOS app that saves a json file alongside an existing audio or video file. Basically, the json file will contain a list of "edits" or looping sections to play from the media file. I've got the json file loading and saving using a custom FileDocument and using fileImporter and fileExporter instance methods on buttons I show in a content view. I have lots of questions about how best to read/write files in SwiftUI!

Ultimately, I want File menu Open... Save and Save As... buttons and I know how to add those buttons. But, I don't know how to invoke the fileImporter and fileExporter methods from these buttons since they don't live in the standard ContentView hierarchy. My data model is a Class (Observable and Codable) and it contains an @Published property to hold the stringified path URL to the media file. both fileImporter and fileExporter use a binding to a Bool to determine if the interface is shown or not. The fileExporter also needs the document (or collection of documents) to export.

My load and save buttons that live in the ContentView hierarchy use a struct with @Published properties for these two Bools and for my custom document. I use an @EnvironmentObject to provide access to this state (and I do pass environmentObject down from the main ContentView (Which uses a NavigationView layout). The load and save buttons work well and I can even access the save and load functionality from the sidepanel if I add buttons there to set the Bools to import or export to true. So I'm reasonably confident I understand how this all works!

Now the issue is, buttons I had to the app menubar File menu do not have access to this state and since these buttons are not in the ContentView hierarchy, they do not get the environmentObject necessary to set the visibile state and document (for fileExport). I've read. re-read, experimented and re-experimented and can't find a solution.

Before I forget, I am not using DocumentGroup although that is how I orifinally started. I need to let the user load the JSON file and then read that file to extract the media file path URL and load that media file then show the "edit" list in the NavigationView's sidebar and the media in the main content area. I can do all of that using the fileImporter and fileExporter approach described above but only by using buttons in the View hierarchy. Using the DocumentGroup approach seemed to not be useful as it took care of loading and saving my JSON document but didn't give me a way to pull the media file path URL and load it to present in the content area.

I also explored using NSOpenPanel and NSSavePanel but ran into similar issues with accessing state from the File menu buttons.

I hope this is all clear! I'm happy to share code but I'll have to spend some time removing lots of comments and cruft I'm using for testing and attempting to understand how SwiftUI works!

Cheers, Michael

2      

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.