5

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.

ANimator120
  • 2,556
  • 1
  • 20
  • 52

2 Answers2

6

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( );
}
WBuck
  • 5,162
  • 2
  • 25
  • 36
2

Add this to your Cargo.toml

[dependencies]
open = "3"

…and open something using…

open::that(".");
jnnnnn
  • 3,889
  • 32
  • 37