6

I have an app that should open certain file types. From the app itself, it's quite straightforward with file_picker plugin. But how to open a file trough the file manager? In other words, trough the "Open with" context menu.

I tried checking the arguments passed to main, but they are always empty. Registering a universal link wouldn't make much sense either since I only need to pass the file - nothing more.

Benjamin Dean
  • 709
  • 1
  • 6
  • 15
  • i'm looking for same thing! what i've just found is that classic url_launcher can do similar thing. but i couldn't have found the way opening files through "open with" option. one thing clear is that we can launch files like .xlsx simply through using url_launcher(tested in 6.0.10)! i might add an answer if you'd like me to do [: – tsitixe Sep 07 '21 at 07:03
  • 1
    That would be great! And I found a way to add your app to "Open with" menu on MacOS. I'll add my part of the answer asap. – Benjamin Dean Sep 08 '21 at 09:22
  • 1
    Also, launching files is one thing, what I wanted is to be able to open a file with the app, which might not be possible atm, since FlutterAppDelegate only references the window and not the app itself. – Benjamin Dean Sep 08 '21 at 09:27
  • Looking forward to see how to implement open with option. that would be really cool stuff for me! my simple answer is here btw [: – tsitixe Sep 09 '21 at 02:00
  • in this case, you can await for file_picker, then assign the path to local value, then finally use the answer's snippet, i guess. but we need to check if the file extension in use is working with `file://`method through `canLaunch`, or to use another method instead of `file://` [: – tsitixe Sep 09 '21 at 02:04

3 Answers3

0

The Mac problem is related to the fact that the development sandboxes your program. I encountered a similar problem and tried to summarize it on

Entitlement issue

you can find the setting in the entitlements (debug and release) at

<key>com.apple.security.app-sandbox</key><true/>
baeckerg
  • 9
  • 2
0

The relatively new package uni_links_desktop looks like it provides a partial solution to what you're asking for. I haven't tried it, but looking through the source, it looks like you can probably get it to give you the file path, when someone does "open with."

I say "partial solution" because it looks like you'd have to give your app permission to read every file anywhere in the filesystem. Proper support for opening "open with" files would involve a more limited permission request, as is done with the the standard file picker from within a desktop app.

If you're willing to live with this limitation, it looks like it ought to work on MacOS and Windows, but not Linux. I hope that proper desktop support for "open with" from a file chooser is on the Flutter Desktop to do list.

Anyway, see https://pub.dev/packages/uni_links_desktop

Bill Foote
  • 361
  • 3
  • 3
-1
if (await canLaunch("file://..path...xlsx")) {
  await launch("file://..path...xlsx");
} else {
  print("cannot launch url ]:");
}

this simple snippet basically works both on windows and mac os! If you can deal with path with classic path_provider, you'd be able to use the same feature on web, ios, android also. In my test, web downloaded the file when i typed and entered the file:// url.

So, i guess the uri scheme is the one who does these tricks. I tried a few types of files and as i commented, excel works just fine and the finder(or file explorer) also work. In my test, pptx even worked with the file url. Yes, it opens the native app. I couldn't have found out "open with.." option, but it meant that it automatically open xlsx file in excel(ie. ATM i don't have any option to launch "open with.." pop-up tho).

Similarly, i could have even found that instagram:// also works with url launcher. One thing unexpectedly tough was to set a proper path on mac os desktop. Since the app is being debugged somewhere assigned previously by Mr. Apple, simply calling methods from path_provider returned a path far away from desktop path, the home screen, since the app is running on somewhere else(like /Users/${MacOsUser}/Library/Containers/${fullPackagePath}/Data/Documents for mac os, which would be different in the distribution stage).

Hope this would help. FYI, i couldn't launch battle.net and league of legends with this method, which don't show their extensions in the file explorer ]: Have a wonderful day [:

tsitixe
  • 1,851
  • 3
  • 14
  • 24