| Crates.io | sys-file-manager-path |
| lib.rs | sys-file-manager-path |
| version | 0.1.1 |
| created_at | 2026-01-18 17:41:37.344675+00 |
| updated_at | 2026-01-18 17:45:27.541198+00 |
| description | 获取 Mac Finder、Windows Explore 窗口的路径 |
| homepage | |
| repository | https://github.com/miniocean404-rust/sys-explore |
| max_upload_size | |
| id | 2052732 |
| size | 47,028 |
跨平台获取系统文件管理器当前窗口路径的 Rust 库。
在 Cargo.toml 中添加依赖:
[dependencies]
sys-file-manager-path = "0.1.1"
use sys_file_manager_path::export::dir::get_os_explore_info;
fn main() -> anyhow::Result<()> {
unsafe {
let info = get_os_explore_info()?;
println!("窗口标题: {}", info.title);
println!("当前路径: {}", info.dir);
println!("执行程序: {}", info.exec);
println!("是否激活: {}", info.is_active);
println!("平台: {}", info.platform.as_ref());
#[cfg(target_os = "windows")]
println!("窗口句柄: {}", info.hwnd_id);
#[cfg(target_os = "macos")]
println!("Bundle ID: {}", info.bundle_id);
}
Ok(())
}
#[cfg(target_os = "windows")]
use sys_file_manager_path::windows::index::get_explore_info;
#[cfg(target_os = "windows")]
fn main() -> anyhow::Result<()> {
unsafe {
let info = get_explore_info()?;
println!("资源管理器路径: {}", info.dir);
println!("窗口句柄: {}", info.hwnd_id);
}
Ok(())
}
#[cfg(target_os = "macos")]
use sys_file_manager_path::macos::index::get_finder_info;
#[cfg(target_os = "macos")]
fn main() -> anyhow::Result<()> {
unsafe {
let info = get_finder_info()?;
println!("Finder 路径: {}", info.dir);
println!("Bundle ID: {}", info.bundle_id);
}
Ok(())
}
AppInfo包含文件管理器窗口的完整信息:
pub struct AppInfo {
pub hwnd_id: isize, // Windows 窗口句柄
pub bundle_id: String, // macOS Bundle ID
pub title: String, // 窗口标题
pub is_active: bool, // 是否为激活窗口
pub dir: String, // 当前目录路径
pub exec: String, // 执行程序路径
pub platform: Platform, // 当前平台
}
Platform平台枚举:
pub enum Platform {
Unknown,
Windows,
MacOS,
}
get_os_explore_info()跨平台统一接口,自动根据操作系统调用对应实现:
pub unsafe fn get_os_explore_info() -> anyhow::Result<AppInfo>
get_explore_info()get_finder_info()通过 Windows API 实现:
CoCreateInstance 创建 ShellWindows COM 对象GetForegroundWindow 获取当前激活窗口IShellBrowser 接口获取窗口路径关键技术:
通过 Objective-C 运行时和 AppleScript 实现:
NSWorkspace 获取前台应用信息com.apple.finder)osascript 执行 AppleScript 获取 Finder 路径关键技术:
objc crate)⚠️ 重要:本库的核心函数使用 unsafe 标记,因为涉及:
使用时需要注意:
anyhow - 错误处理tokio - 异步运行时urlencoding - URL 编码strum / strum_macros - 枚举工具windows - Windows API 绑定cocoa - Cocoa 框架绑定objc / objc2 - Objective-C 运行时MIT License
欢迎提交 Issue 和 Pull Request!