dimusic / active-win-pos-rs Goto Github PK
View Code? Open in Web Editor NEWA small Rust library that lets you get position, size, title and a few other properties of the active window on Windows, MacOS and Linux
License: Apache License 2.0
A small Rust library that lets you get position, size, title and a few other properties of the active window on Windows, MacOS and Linux
License: Apache License 2.0
how to get app icon
PartialEq impl only checks process id, but the window title can change
fn main() {
let mut last_active_window;
loop {
let Ok(active_window) = active_win_pos_rs::get_active_window() else {
continue;
};
if active_window == last_active_window {
continue;
};
last_active_window = active_window;
// ...
}
}
If you want to detect the window title changing you have to explicitly check each property
fn main() {
let mut last_active_window;
loop {
let Ok(active_window) = active_win_pos_rs::get_active_window() else {
continue;
};
if active_window.process_id == last_active_window.process_id && active_window.title == last_active_window.title {
continue;
};
last_active_window = active_window;
// ...
}
}
Hi,
Would it be possible to just get the info of a specific process? Just by the process name/executable?
I want to be able to get the title of a specific program and the title changes depending on where you are in the program.
Kind regards,
I am running a loop and calling this function get_active_window().
The process_name is only returning the same value while I am switching through different windows.
These traits are very simple to implement and help a lot with using the data structures
Hi, I'm getting the following error on Xcode 15, which has macOS 14 SDK, Apologies if it's not related here
Caused by:
process didn't exit successfully: /Users/cyron/Documents/BeeBox/sandclock-tauri/src-tauri/target/debug/build/appkit-nsworkspace-bindings-dd1fe9d906edfc67/build-script-build
(exit status: 101)
--- stdout
cargo:rustc-link-lib=framework=AppKit
--- stderr
thread 'main' panicked at '"wait_struct_(unnamed_at_/Applications/Xcode_app/Contents/Developer/Platforms/MacOSX_platform/Developer/SDKs/MacOSX14_0_sdk/usr/include/sys/wait_h_199_2)" is not a valid Ident', /Users/cyron/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/src/fallback.rs:774:9
note: run with RUST_BACKTRACE=1
environment variable to display a backtrace
Hi, I was curious if its possible to implement subscribing
to active window change event somehow? The thing is I know how to do this on linux, have vague understanding how to make it with winapi, but have any clue how (if at all possible) to do this for OSX.
I'm asking because polling is quite expensive, especially in OSX for some reason, and I can't find any library that could do this for some reason.
Thx.
Awesome crate with a lot of potential, unfortunately unfree because of missing license.
I wasn't sure where else I should post this - feel free to close the issue.
However, if you need the macos title and search for a quick workaround, this is what we use.
// Raw data is like this: 7) "iTerm2" ASN:0x0-0x18018: (in front)
pub fn macos_get_active_window() -> String {
let mut command = shell("lsappinfo | grep front");
command.stdout(Stdio::piped());
let output = command.execute_output().unwrap();
let mut app_name : String = String::from_utf8(output.stdout).unwrap().to_string();
app_name = app_name.replace("\n", "");
app_name = extract_app_name(app_name);
return app_name;
}
// Input:
// "95) \"DayCheckin\" ASN:0x0-0x2aa3aa1: (in front) "
// Output: DayCheckin
// "79) \"Code - Insiders\" ASN:0x0-0x2a3ca3a: (in front) "
// Code - Insiders
// Test it here: https://rustexp.lpil.uk/
pub fn extract_app_name(app_name: String) -> String {
let re = Regex::new(r###""(.*)""###).unwrap();
let extracted_name = match re.captures(&app_name) {
Some(val) => { val[0].to_string().replace("\"", "") }
None => {app_name}
};
extracted_name
}
Hi,
I am new to rust and still trying to understand what's what :)
Is it possible the manipulate the active window in any way? like setting the size or position?
I found this https://tauri.app/v1/api/js/window but it seems to only be geared towards the windows that are part of the main app?
Thank you!
Hi,
I'm under "Ubuntu 22.04.1 LTS" whit X11 and I've a big 32/9 screen:
display_info DisplayInfo { id: 446, x: 0, y: 0, width: 5120, height: 1440, rotation: 0.0, scale_factor: 1.0, is_primary: true }
if I have 2 separate windows, one taking all place on left on the other on right, I get that:
What I really show on my Screen:
Now what ActiveWindow SHOW:
LEFT:
active window: ActiveWindow {
title: "syl21@XPS-8930: ~/projets/debug",
process_name: "Gnome-terminal",
window_id: "50331658",
process_id: 3663,
position: WindowPosition {
x: 140.0,
y: 14.0,
width: 2555.0,
height: 1453.0,
},
}
RIGHT:
active window: ActiveWindow {
title: "",
process_name: "firefox",
window_id: "46137405",
process_id: 4593,
position: WindowPosition {
x: 5158.0,
y: 8.0,
width: 2567.0,
height: 1465.0,
},
}
As you can see they are mistake in RIGHT window position, normaly:
Thx for your help ;-)
Hi there! I'm interested in using active-win-pos-rs
from within electron
which should be rather trivial nowadays with NAPI-RS, do you by any chance have already plans for doing this on your end or do you want to stay in rust-land and rather have somebody else do it?
Those type of only-glue-repos in some cases tend to get more attention than the actual implementation which is unfair, that's why I'm asking.
✌🏻
Focusing any UWP app (not to mention a fullscreen one), returns invalid data in several fields:
active window: ActiveWindow {
title: "Microsoft Store",
process_path: "C:\\Windows\\System32\\ApplicationFrameHost.exe",
app_name: "Application Frame Host",
window_id: "HWND(3541104)",
process_id: 15252,
position: WindowPosition {
x: 990.0,
y: 190.0,
width: 1538.0,
height: 916.0,
},
}
Fields process_path
and app_name
are affected (at least from a general look)
Using the value .process_name can result in wrong process name. For example:
STRING LANG CODE: \StringFileInfo\040904b0\FileDescription
DESCRIPTION: Visual Studio Code
DESCRIPTION_NEW: Visual Studio Code
active window: ActiveWindow {
title: "active-window.rs - active-win-pos-rs - Visual Studio Code",
process_name: "Visual Studio Code",
window_id: "HWND(1968720)",
process_id: 5576,
position: WindowPosition {
x: -7.0,
y: -7.0,
width: 2062.0,
height: 1166.0,
},
}
STRING LANG CODE: \StringFileInfo\040904e4\FileDescription
DESCRIPTION: VLC media player
DESCRIPTION_NEW: VLC media player
active window: ActiveWindow {
title: "VLC media player",
process_name: "VLC media player",
window_id: "HWND(1314976)",
process_id: 26212,
position: WindowPosition {
x: -1150.0,
y: 333.0,
width: 781.0,
height: 628.0,
},
}
The actual process names are different. I located the issue to this request:
active-win-pos-rs/src/win/platform_api.rs
Line 182 in 2a3764d
This line requests the FileDescription
, which can differ from process name. One possible fix is to change this to InternalName
, but this results in anomalies:
STRING LANG CODE: \StringFileInfo\040904b0\InternalName
DESCRIPTION: electron.exe
DESCRIPTION_NEW: electron.exe
active window: ActiveWindow {
title: "active-window.rs - active-win-pos-rs - Visual Studio Code",
process_name: "electron.exe",
window_id: "HWND(1968720)",
process_id: 5576,
position: WindowPosition {
x: -7.0,
y: -7.0,
width: 2062.0,
height: 1166.0,
},
}
VSCode is clearly not Electron.exe in the process tab.
I have a solution I will link to a PR, that is to use a completely different function to get data:
active window: ActiveWindow {
title: "active-window.rs - active-win-pos-rs - Visual Studio Code",
process_name: "Code.exe",
window_id: "HWND(1968720)",
process_id: 5576,
position: WindowPosition {
x: -7.0,
y: -7.0,
width: 2062.0,
height: 1166.0,
},
}
Love the approach and implementation on this!
I was wondering if you'd be willing to extend the capabilities to allow for fetching of additional window information like window name, class, instance, etc..
If you're interested, I prefer to incrementally add these additions to this repo instead of forking.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.