ANimator120
ANimator120

Reputation: 3421

With Rust, Open Explorer on a File

If one wanted to show a file in a File Explorer or use the similar "Reveal in Finder" feature found on OSX, how could you do that in rust? Is there a crate that could help?

fn main(){
   reveal_file("tmp/my_file.jpg")
   //would bring up the file in a File Explorer Window
}

I'm looking for something similar to this python solution.

Upvotes: 6

Views: 4518

Answers (2)

jnnnnn
jnnnnn

Reputation: 4383

Add this to your Cargo.toml

[dependencies]
open = "3"

…and open something using…

open::that(".");

Upvotes: 4

WBuck
WBuck

Reputation: 5503

You can use Command to open the finder process.

macOS

use std::process::Command;

fn main( ) {
    println!( "Opening" );
    Command::new( "open" )
        .arg( "." ) // <- Specify the directory you'd like to open.
        .spawn( )
        .unwrap( );
}

Windows

use std::process::Command;

fn main( ) {
    println!( "Opening" );
    Command::new( "explorer" )
        .arg( "." ) // <- Specify the directory you'd like to open.
        .spawn( )
        .unwrap( );
}

EDIT:

As per @hellow's comment.

Linux

use std::process::Command;

fn main( ) {
    println!( "Opening" );
    Command::new( "xdg-open" )
        .arg( "." ) // <- Specify the directory you'd like to open.
        .spawn( )
        .unwrap( );
}

Upvotes: 8

Related Questions