随时更换mac 壁纸的代码 rust 加 tauri
async download1(){ this.greetMsg = await invoke("greet2", { downloadUrl: this.cur, name:Date.now()+'.png' }); },
import { invoke } from "@tauri-apps/api/tauri";
[package] name = "tauri-app" version = "0.1.0" description = "A Tauri App" authors = ["you"] edition = "2021"
See more keys and their definitions at doc.rust-lang.org/cargo/refer…
[build-dependencies] tauri-build = { version = "1", features = [] }
[dependencies]
tauri = { version = "1", features = [ "api-all"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
regex = "1.5"
chrono = "0.4.19"
dirs = "3.0.2"
reqwest = { version = "0.11.4", features = ["blocking", "json"] }
tokio = { version = "1.0", features = ["full"] }
[features]
This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
custom-protocol = ["tauri/custom-protocol"]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::env; use std::path::PathBuf; use std::process::Command; use std::fs::File; use std::io::prelude::*; use reqwest::Client; use tokio::fs; use regex::Regex; use tauri::generate_handler; #[tauri::command] async fn greet2(downloadUrl:&str,name:&str) -> Result<String, String> { // 获取用户主目录 let mut save_dir = env::var("HOME").unwrap_or_else(|_| ".".to_string()); let mut save_path = PathBuf::from(&save_dir);
save_path.push(name); // 卧槽 这个事啥意思..... 文件名称从 downloadUrl 获取 或者随机明媚
// 创建一个 reqwest 客户端
let client =Client::new();
// 下载图片 "http://127.0.0.1:8080/app.icns"
let response = client.get(downloadUrl) // change shit icns and test again i
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
// 将图片保存到文件
let mut file = File::create(&save_path).unwrap();
file.write_all(&response).unwrap();
// let desktop_command = "osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Users/shompinice/0.wzskyline-app.png"'";
// 执行 i壁纸命令
/*~
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Users/davidwalsh/Downloads/pubg-background.jpg"'
*/
let path1 = format!("/Users/shompinice/{}", name);
let applescript = format!(
"tell application \"Finder\" to set desktop picture to POSIX file \"{}\"",
path1
);
let desktop_command = "osascript"; let output = Command::new(desktop_command) .arg("-e") //.arg("tell application "Finder" to set desktop picture to POSIX file "/Users/shompinice/0.wzskyline-app.png"") .arg(applescript) .output() .expect("failed to execute osascript");
let output_str = String::from_utf8_lossy(&output.stdout).to_string();
Ok("Hello, world!".to_string())
} #[tauri::command] async fn greet(downloadUrl:&str) -> Result<String, String> { // 获取用户主目录 let mut save_dir = env::var("HOME").unwrap_or_else(|_| ".".to_string()); let mut save_path = PathBuf::from(&save_dir); save_path.push("wzskyline-app.icns"); // 创建一个 reqwest 客户端 let client =Client::new();
// 下载图片 "http://127.0.0.1:8080/app.icns"
let response = client.get(downloadUrl) // change shit icns and test again i
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
// 将图片保存到文件
let mut file = File::create(&save_path).unwrap();
file.write_all(&response).unwrap();
// 读取 Info.plist 文件内容
let content = fs::read_to_string("/Applications/Figma.app/Contents/Info.plist")
.await
.expect("Unable to read file");
// 使用正则表达式匹配 CFBundleIconFile
let re = Regex::new(r#"<key>CFBundleIconFile</key>\s*<string>(.*?)</string>"#).unwrap();
if let Some(captures) = re.captures(&content) {
if let Some(icon_file) = captures.get(1) {
println!("CFBundleIconFile: {}", icon_file.as_str());
let s1 = String::from("/Applications/Figma.app/Contents/Resources/");
let s2 = icon_file.as_str().clone();
let s3 = s1 + &s2;
println!("{}", &s3);
// 复制文件到指定目录
let output = Command::new("cp")
.arg(&save_path)
.arg(&s3)
.output()
.expect("failed to execute process");
// 创建一个空文件
Command::new("touch")
.arg("/Applications/Figma.app")
.output()
.expect("failed to execute process");
if output.status.success() {
println!("File copied successfully!");
let stdout = String::from_utf8_lossy(&output.stdout);
println!("Command output:\n{}", stdout);
} else {
println!("File copy failed with error code: {}", output.status);
}
}
}
// 执行 ipconfig 命令
let output = Command::new("ipconfig")
.output()
.expect("failed to execute ipconfig");
let output_str = String::from_utf8_lossy(&output.stdout).to_string();
Ok("Hello, world!".to_string())
}
fn main() { tauri::Builder::default() .invoke_handler(tauri::generate_handler![greet]) .invoke_handler(tauri::generate_handler![greet2]) .run(tauri::generate_context!()) .expect("error while running tauri application"); }