Get current active window's title in Java

前端 未结 6 2012
耶瑟儿~
耶瑟儿~ 2020-12-08 17:43

I am trying to write a Java program that logs what application I\'m using every 5 seconds (this is a time tracker app). I need some way to find out what the current active w

相关标签:
6条回答
  • 2020-12-08 18:13

    I've written a bash script that logs the current active window: http://www.whitelamp.com/public/active-window-logger.html It uses a patched version of wmctrl but provides details of an alternative (slower) method using xprop and xwininfo.

    The links to the wmctrl patch & source code and the script can be found above.

    0 讨论(0)
  • 2020-12-08 18:15

    I'm quite certain that you'll find there's no way to enumerate the active windows in pure Java (I've looked pretty hard before), so you'll need to code for the platforms you want to target.

    • On Mac OS X, you can launch an AppleScript using "osascript".

    • On X11, you can use xwininfo.

    • On Windows, you can probably launch some VBScript (e.g. this link looks promising).

    If you're using SWT, you may be able to find some undocumented, non-public methods in the SWT libs, since SWT provides wrappers for a lot of the OS API's (e.g. SWT on Cocoa has the org.eclipse.swt.internal.cocoa.OS#objc_msgSend() methods that can be used to access the OS). The equivalent "OS" classes on Windows and X11 may have API's you can use.

    0 讨论(0)
  • 2020-12-08 18:19

    I created this applescript while looking into similar topic - this one gets the specific window size

    global theSBounds
    
    tell application "System Events"
    	set this_info to {}
    	set theSBounds to {}
    	repeat with theProcess in processes
    		if not background only of theProcess then
    			tell theProcess
    				set processName to name
    				set theWindows to windows
    			end tell
    			set windowsCount to count of theWindows
    			
    			if processName contains "xxxxxxxx" then
    				set this_info to this_info & processName
    				
    			else if processName is not "Finder" then
    				if windowsCount is greater than 0 then
    					repeat with theWindow in theWindows
    						tell theProcess
    							tell theWindow
    								if (value of attribute "AXTitle") contains "Genymotion for personal use -" then
    									-- set this_info to this_info & (value of attribute "AXTitle")
    									set the props to get the properties of the theWindow
    									set theSBounds to {size, position} of props
    									set this_info to this_info & theSBounds
    								end if
    							end tell
    						end tell
    					end repeat
    				end if
    			end if
    		end if
    	end repeat
    end tell
    return theSBounds

    0 讨论(0)
  • 2020-12-08 18:26

    I have written a java program using user361601's script. I hope this will help others.

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class WindowAndProcessInfo4Linux {
    
    public static final String WIN_ID_CMD = "xprop -root | grep " + "\"_NET_ACTIVE_WINDOW(WINDOW)\"" + "|cut -d ' ' -f 5";
    public static final String WIN_INFO_CMD_PREFIX = "xwininfo -id ";
    public static final String WIN_INFO_CMD_MID = " |awk \'BEGIN {FS=\"\\\"\"}/xwininfo: Window id/{print $2}\' | sed \'s/-[^-]*$//g\'";
    
    public String execShellCmd(String cmd){
        try {  
    
            Runtime runtime = Runtime.getRuntime();  
            Process process = runtime.exec(new String[] { "/bin/bash", "-c", cmd });  
            int exitValue = process.waitFor();  
            System.out.println("exit value: " + exitValue);  
            BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));  
            String line = "";  
            String output = "";
            while ((line = buf.readLine()) != null) {
                output = line;
            }
            return output;
        } catch (Exception e) {  
            System.out.println(e);
            return null;
        }  
    }
    
    public String windowInfoCmd(String winId){
        if(null!=winId && !"".equalsIgnoreCase(winId)){
            return WIN_INFO_CMD_PREFIX+winId +WIN_INFO_CMD_MID;
        }
        return null;
    }
    
    public static void main (String [] args){
        WindowAndProcessInfo4Linux windowAndProcessInfo4Linux = new WindowAndProcessInfo4Linux();
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String winId = windowAndProcessInfo4Linux.execShellCmd(WIN_ID_CMD);
        String winInfoMcd = windowAndProcessInfo4Linux.windowInfoCmd(winId);
        String windowTitle = windowAndProcessInfo4Linux.execShellCmd(winInfoMcd);
        System.out.println("window title is: "+ windowTitle);
    
    }
    }
    

    // the thread.sleep is there so that you get time to switch to other window :) also, you may use quartz from spring to schedule it.

    0 讨论(0)
  • 2020-12-08 18:27

    Using SWT internals, I was able to put this together, and it seems to work nicely:

        /** @return The currently active window's title */
        public static final String getActiveWindowText() {
            long /*int*/ handle = OS.GetForegroundWindow();
            int length = OS.GetWindowTextLength(handle);
            if(length == 0) return "";
            /* Use the character encoding for the default locale */
            TCHAR buffer = new TCHAR(0, length + 1);
            OS.GetWindowText(handle, buffer, length + 1);
            return buffer.toString(0, length);
        }
    
    public static final void main(String[] args) {
        try {
            Thread.sleep(1000L);
        } catch(InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        System.out.println(getActiveWindowText());
    }
    

    Prints: user interface - Get current active window's title in Java - Stack Overflow - Google Chrome

    0 讨论(0)
  • 2020-12-08 18:28

    To find the active Window(be it a frame or a dialog) in a java swing application you can use the following recursive method:

    Window getSelectedWindow(Window[] windows) {
        Window result = null;
        for (int i = 0; i < windows.length; i++) {
            Window window = windows[i];
            if (window.isActive()) {
                result = window;
            } else {
                Window[] ownedWindows = window.getOwnedWindows();
                if (ownedWindows != null) {
                    result = getSelectedWindow(ownedWindows);
                }
            }
        }
        return result;
    }
    

    this is from here More clues on Window state here.

    0 讨论(0)
提交回复
热议问题