use autoitx::{AutoIt, Selector};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let filter = std::env::args().nth(1);
let ai = AutoIt::new()?;
let pattern = filter
.as_deref()
.map_or_else(|| "(.*)".to_owned(), |f| format!("(.*){f}(.*)"));
let any = Selector::from(format!("[REGEXPTITLE:{pattern}]").as_str());
match ai.win_get_title(&any) {
Ok(title) => {
let pid = ai.win_get_process(&any)?;
let rect = ai.win_get_pos(&any)?;
println!("first match\n");
println!(" title {title:?}");
println!(" pid {pid}");
println!(" rect {rect:?}");
println!("\nA selector that finds it again:");
println!(" Selector::title({:?})", first_words(&title));
}
Err(e) => {
println!("nothing matched: {e}");
if filter.is_some() {
println!("\nTry without a filter to see whether anything matches at all.");
}
}
}
Ok(())
}
fn first_words(title: &str) -> String {
title
.split_whitespace()
.take(2)
.collect::<Vec<_>>()
.join(" ")
}