Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing "Open file with" in Swift Cocoa App

Tags:

macos

swift

cocoa

I'm working on a macOS cocoa-app in Swift where I import several different file types into the app for the user to interact with.

I'm currently trying to determine if it's possible to implement the "Open file with" feature, so that the user could open those files in a different program if they wanted to:

enter image description here

I've found a few different SO questions that seem tangentially related to what I'm trying to do:

Swift: How to open file with associated application?

Launch OSX Finder window with specific files selected

...but so far nothing to indicate if it's possible to implement right-click Finder/file (?) access in the way I had in mind.

Apologies if this is too vague of a question; any help / guidance appreciated!

like image 496
narner Avatar asked Oct 17 '25 23:10

narner


1 Answers

Without going into details, it's pretty straight forward:

  1. Get the list of all known applications that can open a specific file type (see LSCopyApplicationURLsForURL, a Core Foundation C function).
  2. Build the menu. You can use NSWorkspace (and probably URL) to get the application icons.
  3. Use NSWorkspace.openFile(_:withApplication:) to tell the application to open the given document.
like image 149
James Bucanek Avatar answered Oct 20 '25 14:10

James Bucanek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!