Permission error occurs when I use setDefaultApplication(at:toOpen:completion:)

I'd like to set my macOS app written in Swift as default app when opening .mp4 file. I think I can do it with setDefaultApplication(at:toOpen:completion:).
https://developer.apple.com/documentation/appkit/nsworkspace/3753002-setdefaultapplication

However, permission error occurs when I use it.
The error is:
Error Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened." UserInfo={NSUnderlyingError=0x6000031d0150 {Error Domain=NSOSStatusErrorDomain Code=-54 "permErr: permissions error (on file open)"}}

I tried to give my app full-disk access, but it didn't work.
I also tried to use setDefaultApplication(at:toOpenFileAt:completion:), then it works with no error, but it effects on only one file.

What I want to do is to set my app as default app of all .mp4 files.
How do I achieve this?

My code is like below: let bundleUrl = Bundle.main.bundleURL NSWorkspace.shared.setDefaultApplication(at: bundleUrl, toOpen: .mpeg4Movie) { error in
print(error)
}

Thank you.

Answered by DTS Engineer in 868377022

Sounds like either it's not working right or the documentation needs to be updated to explain how to get it working.

Our engineering teams need to investigate this issue, as resolution may involve changes to Apple's software. Please file a bug report, include a small Xcode project and some directions that can be used to reproduce the problem, and post the Feedback number here once you do. If you post the Feedback number here I'll check the status next time I do a sweep of forums posts where I've suggested bug reports.

Bug Reporting: How and Why? has tips on creating your bug report.

Hi @wakame did you figure out what the issue is? I have the same issue. I also found a tool on GitHub which I tried, but ended up also with exactly the same error message.

Sounds like either it's not working right or the documentation needs to be updated to explain how to get it working.

Our engineering teams need to investigate this issue, as resolution may involve changes to Apple's software. Please file a bug report, include a small Xcode project and some directions that can be used to reproduce the problem, and post the Feedback number here once you do. If you post the Feedback number here I'll check the status next time I do a sweep of forums posts where I've suggested bug reports.

Bug Reporting: How and Why? has tips on creating your bug report.

Is your app sandboxed?

If so, this is not expected to work. The App Sandbox prevents apps from changing application bindings. You can see evidence for this if you dig into the error. The underlying error, -54, aka permErr, is being rendered as permissions error (on file open) but that’s just an accident of history. Launch Services is using it in the more generic sense of permission denied.

And, yeah, please do file a bug against the docs. We should update both the NSWorkspace API documentation and the Review functionality that is incompatible with App Sandbox section of Protecting user data with App Sandbox.

Share and Enjoy

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

Permission error occurs when I use setDefaultApplication(at:toOpen:completion:)
 
 
Q