'Use program to open file' in Rust

Hi,
How do I make a rust program that, when installed, comes up in the list of programs that can be used to open a specific file type? Then how do I access the file path that was opened by the user - perhaps command line args? I'd also need this to work cross platform.
Thanks!

Eh, ignore me, I misunderstood the question.

I cannot vouch for this crate, but it exists. If nothing else you can see what they do. As you can see from the summary, there is no standard way to do this on linux (but are a number of desktop-specific or aspirational possibilities).

In general, this relates to how you package your application, in addition to what its code does.

For example, on macOS, you put this information in your application's Info.plist inside its .app bundle directory, and the application needs to receive an event after it is launched. On Linux, you have to create and install a .desktop file that specifies how to run your executable.

2 Likes

How is the file path supplied to the application?
Also, can you link to any resources which explain how to implement this? I don't know what to google search to find anything, becuase whenever I do, google misunderstands the question to be 'how to open a file from inside rust'

How is the file path supplied to the application?

On most platforms, it is passed through std::env::args() (also known as argv outside of Rust).

macOS is an exception because under normal conditions it runs at most one process per application (like iOS and Android, but predating those designs), rather than one new process per document, so you need to receive “open file” events after the application has started. For a Rust example, if you were using tao (a fork of winit that is more complete in real-desktop-application matters) it looks like you'd use Event::Opened for that.

Also, can you link to any resources which explain how to implement this?

I don't have any, sorry. Development of serious, properly platform-integrated desktop applications, that work with user documents rather than just having internally managed state files, is unpopular these days, and while I consider this unfortunate, I haven't yet personally dug into the ways to do it anyway.

3 Likes

Thanks for this. I'm using bevy (uses winit) to create an editor application which edits a certain file format. Seems like winit doesn't support this yet, but this PR looks like it will add that functionality.
How do I make my application come up in the list of programs that can be used on windows?
Also, how do I set a custom icon for files which have been set to be opened with my app?
Shame about lack of documentation on how to do any of this.

I don't develop for Windows so I can't tell you in any detail. It's the sort of thing that at least used to be in the registry, I think, and it should be done by the installer/package for your application.

In general, that is done as part of setting up the file association; you get to specify an icon at the same time. The icon will need to be converted to the icon format the platform uses. (Icon file formats have the special feature of including several different sizes, so the icon can be tweaked to be legible even when very small.)

At least traditionally on Windows this is in (to simplify) HKEY_CLASSES_ROOT, which is a somewhat complicated beast. If you can't get your packager to deal with it, best step carefully with the documentation:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.