java-access-bridge-wrapper

2.0.0 · active · verified Wed Apr 15

java-access-bridge-wrapper is a Python wrapper for the Windows Java Access Bridge (JAB). It enables Python applications to interact with and automate Java user interfaces on the Windows operating system. This library, currently at version 2.0.0, is primarily designed for 64-bit Windows environments and Java Development Kit (JDK) or Java Runtime Environment (JRE) versions 8 or higher.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `JavaAccessBridgeWrapper` by setting up the necessary Windows message pump in a separate thread, which is crucial for the Java Access Bridge to receive UI events. It then attempts to list active Java windows and interact with the first detected Java application by switching to its context. It also shows an optional step to build a `ContextTree` to explore the UI elements. Before running, ensure a 64-bit Java application is open and the Java Access Bridge is enabled on your Windows system.

import ctypes
import queue
import threading
import time
from ctypes import wintypes, byref

from JABWrapper.jab_wrapper import JavaAccessBridgeWrapper
from JABWrapper.context_tree import ContextTree # Optional, for element interaction

def pump_background(pipe: queue.Queue):
    """Runs the Windows message pump in a background thread."""
    try:
        jab_wrapper = JavaAccessBridgeWrapper()
        pipe.put(jab_wrapper)
        message = byref(wintypes.MSG())
        # The message pump is essential for JAB to receive events
        while ctypes.windll.user32.GetMessageW(message, 0, 0, 0) > 0:
            ctypes.windll.user32.TranslateMessage(message)
            ctypes.windll.user32.DispatchMessageW(message)
    except Exception as err:
        pipe.put(None)
        print(f"Error in message pump: {err}")

def main():
    pipe = queue.Queue()
    # Start the message pump in a separate thread
    thread = threading.Thread(target=pump_background, daemon=True, args=[pipe])
    thread.start()

    # Retrieve the initialized JABWrapper object from the background thread
    jab_wrapper = pipe.get()
    if not jab_wrapper:
        raise Exception("Failed to initialize Java Access Bridge Wrapper")

    print("Java Access Bridge Wrapper initialized.")

    # Give JAB a moment to process initial messages
    time.sleep(0.5)

    # Example: List available Java windows (requires a Java app running)
    try:
        windows = jab_wrapper.get_windows()
        if windows:
            print("\nDiscovered Java Windows:")
            for i, window in enumerate(windows):
                print(f"  [{i+1}] Title: {window.title}, PID: {window.pid}")

            # Example: Attach to the first Java window found (if any)
            if len(windows) > 0:
                first_window = windows[0]
                print(f"\nAttempting to switch to: {first_window.title}")
                jab_wrapper.switch_window_by_title(first_window.title)
                print(f"Successfully switched to window: {jab_wrapper.get_main_frame_title()}")

                # Optional: Get and print the context tree of the active window
                print("\nBuilding context tree...")
                context_tree = ContextTree(jab_wrapper)
                # This can be very verbose, only print a few levels or specific elements
                # For a full tree, consider pretty-printing or specific searches
                # print(context_tree.get_tree_string())
                print(f"Root element role: {context_tree.get_root().role}, name: {context_tree.get_root().name}")

        else:
            print("No Java windows found. Please ensure a Java application is running.")

    except Exception as e:
        print(f"An error occurred during interaction: {e}")

    finally:
        print("\nShutting down Java Access Bridge Wrapper.")
        if jab_wrapper:
            jab_wrapper.shutdown()
        thread.join(timeout=1) # Give the thread a moment to finish cleanly

if __name__ == "__main__":
    # Ensure Java Access Bridge is enabled and a Java application is running.
    # For testing, you might need a simple Swing/AWT app.
    main()

view raw JSON →