FileDocument - open another file in the same directory as selected file

I'm working on a macOS app where my file format can include other files (think #include in C/C++). When opening a file with SwiftUI's document-based APIs (i.e., FileDocument), is there a way to get access to those other files? Alternatively, is there a way I could "open" the file's directory, similar to how Xcode opens the directory that a .xcodeproj is located?

I don't mind falling back to older Cocoa APIs if this is too obscure for the shiny new stuff :)

Answered by jmsharvey771 in 792890022

I decided to disable the App Sandbox here - my app was built off a legacy system that depends on being able to access relative paths.

Use the FileWrapper class to store your document as a package of files and folders that appears as a single file in the Finder. The following article provides an introduction to working with file wrappers in a SwiftUI app:

https://www.swiftdevjournal.com/using-file-wrappers-in-a-swiftui-app/

Accepted Answer

I decided to disable the App Sandbox here - my app was built off a legacy system that depends on being able to access relative paths.

my app was built off a legacy system that depends on being able to access relative paths.

Yeah, that’s tricky. If you control the document format then you can use documented-scope bookmarks to track access to these dependencies. However, if you’re working with a file format that only has relatives paths then there’s no great solution available to you. If you can disable the sandbox, because you distribute your app directly, then that’s an easy path forward. If not, none of the options are ideal [1].

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] The best one I can think of is to collect together all the relative paths, work out the ‘deepest’ directory that encompasses them all, and then present an open panel and ask the user to select that directory )-:

FileDocument - open another file in the same directory as selected file
 
 
Q