This AutoHotKey (AHK) script is intended to manage the state...

January 11, 2025 at 12:43 AM

#SingleInstance Force #Requires AutoHotKey v2.0 ; Configuration class Config { static SOUND_ENABLED := false static SOUNDS := { LARGE_MODE: "C:\Windows\Media\Windows Battery Low.wav", RECEIPT_MODE: "C:\Windows\Media\Windows Balloon.wav" } static ICONS := { LARGE: "printer_on.ico", RECEIPT: "printer_off.ico" } static TIMEOUT := 2000 ; Timeout in milliseconds for window operations static SLEEP_INTERVAL := 100 ; Sleep interval between operations static STARTUP_CHECK_INTERVAL := 5000 ; How often to check for Horizon during startup (5 seconds) } class PrinterToggle { currentState := true attempts := 0 MAIN_CLASS := "LauncherFrame_7.3.0" LAUNCHER_EXE := "ahk_exe launcher.exe" DIALOG_CLASS := "ahk_class PM_Dialog" CHECKBOX_CONTROL := "Button16" Init() { this._UpdateTrayIcon() this.WaitForHorizon() } WaitForHorizon() { this._ShowTip("Waiting for Horizon to start...") checkHorizon := this._CheckForHorizon.Bind(this) SetTimer(checkHorizon, Config.STARTUP_CHECK_INTERVAL) } _CheckForHorizon() { this.attempts++ if WinExist(this.LAUNCHER_EXE) { Sleep(1000) if WinExist("ahk_class " this.MAIN_CLASS) { SetTimer(, 0) this._HorizonReady() return } } } _HorizonReady() { this.UpdateState() } CloseOpenDialogs() { closeAttempts := 0 maxAttempts := 5 mainHwnd := 0 ; First, store the main window handle if WinExist(this.LAUNCHER_EXE) { mainHwnd := WinExist("ahk_class " this.MAIN_CLASS) } ; If we can't find the main window, return false if !mainHwnd { return false } while (closeAttempts < maxAttempts) { ; Get all windows matching the launcher executable windowList := WinGetList(this.LAUNCHER_EXE) ; If only the main window exists, we're done if windowList.Length = 1 && windowList[1] = mainHwnd { return true } ; Close each window that isn't the main window for hwnd in windowList { if (hwnd != mainHwnd) { try { WinClose("ahk_id " hwnd) Sleep(Config.SLEEP_INTERVAL) if WinExist("ahk_id " hwnd) { WinKill("ahk_id " hwnd) Sleep(Config.SLEEP_INTERVAL) } } catch as err { this._ShowError("Error closing dialog: " err.Message) } } } closeAttempts++ Sleep(Config.SLEEP_INTERVAL) } ; Verify main window still exists if !WinExist("ahk_id " mainHwnd) { this._ShowError("Main window was accidentally closed") return false } return true } Toggle() { if !this._EnsureEnvironment() { return } this._OpenOptionsDialog() this._ToggleCheckbox() this._CloseOptionsDialog() this.UpdateVisuals() } UpdateState() { if !this._EnsureEnvironment() { return } this._OpenOptionsDialog() this._GetCheckboxState() this._CloseOptionsDialog() this.UpdateVisuals() } _EnsureEnvironment() { if !this.CloseOpenDialogs() { return false } if !WinExist(this.LAUNCHER_EXE) { this._ShowError("Horizon not found! Waiting for restart...") this.WaitForHorizon() return false } WinActivate(this.LAUNCHER_EXE) return true } _OpenOptionsDialog() { MenuSelect(this.LAUNCHER_EXE, , "Tools", "1&") if !WinWait(this.DIALOG_CLASS, , Config.TIMEOUT / 1000) { throw Error("Options dialog failed to open") } WinActivate(this.DIALOG_CLASS) Sleep(100) ; Give the dialog a moment to fully activate } _ToggleCheckbox() { try { currentValue := ControlGetChecked(this.CHECKBOX_CONTROL, this.DIALOG_CLASS) ; Invert the current state newValue := !currentValue ControlSetChecked(newValue, this.CHECKBOX_CONTROL, this.DIALOG_CLASS) Sleep(50) ; Give it a moment to update this._GetCheckboxState() } catch as err { throw Error("Failed to toggle checkbox: " err.Message) } } _GetCheckboxState() { try { isChecked := ControlGetChecked(this.CHECKBOX_CONTROL, this.DIALOG_CLASS) this.currentState := isChecked ; Invert the state since checked = receipt mode } catch as err { throw Error("Failed to get checkbox state: " err.Message) } } _CloseOptionsDialog() { Send "{Enter}" Sleep(100) ; Give it a moment to close } UpdateVisuals() { this._UpdateTrayIcon() this._PlaySound() this._ShowNotification() } _UpdateTrayIcon() { if (this.currentState = false) { TraySetIcon(Config.ICONS.RECEIPT) } else { TraySetIcon(Config.ICONS.LARGE) } } _PlaySound() { if Config.SOUND_ENABLED { try { SoundPlay(this.currentState ? Config.SOUNDS.LARGE_MODE : Config.SOUNDS.RECEIPT_MODE) } catch as err { this._ShowError("Failed to play sound: " err.Message) } } } _ShowNotification() { stateText := this.currentState ? "Large Print" : "Receipts" this._ShowTip("Ready for " stateText) } _ShowTip(text) { TrayTip(text, "Horizon Printer Mode", 0x10) SetTimer(() => this._HideTrayTip(), -1500) } _ShowError(text) { TrayTip(text, "Horizon Printer Mode Error", 0x10 | 0x2) SetTimer(() => this._HideTrayTip(), -3000) } _HideTrayTip() { TrayTip() if SubStr(A_OSVersion, 1, 3) = "10." { A_IconHidden := true Sleep(200) A_IconHidden := false } } } ; Initialize global printer := PrinterToggle() printer.Init() ; Hotkey to activate the toggle ^NumpadMult:: printer.Toggle() ; Ctrl + NumpadMultiply

This AutoHotKey (AHK) script is intended to manage the state of a printer-related application called "Horizon." It uses functions to toggle between two printer modes, "Large Print" and "Receipts," and also manages visual and audible notifications, as well as interacting with the application's GUI windows.

Here’s what the script does in detail:

  1. Configuration (Config class):

    • The script sets up global parameters (like file paths for sounds and icons, timeout intervals, and other settings).
    • It specifies configuration objects for sounds, tray icons for the two printer modes, and other operational details.
  2. Printer Mode Toggle (PrinterToggle class):

    • This is the main logic encapsulated in a class named PrinterToggle.
    • The script tracks and manages the printer mode state (Large Print or Receipts) through a currentState boolean variable (true for Large Print, false for Receipts).
    • It automatically monitors whether the Horizon application is running and waits for it to start (using the WaitForHorizon and _CheckForHorizon functions).
  3. Toggle Functionality:

    • Upon toggling, the script:
      • Ensures the Horizon environment is ready by checking and managing any leftover dialogs or states.
      • Opens a configuration/options dialog from Horizon using menu navigation.
      • Toggles a checkbox that likely represents the printer mode in the application (this.CHECKBOX_CONTROL).
      • Closes the dialog and updates the stored state based on the new checkbox value.
  4. State Updating:

    • Keeps track of the current printer mode (Large Print or Receipts) by directly reading the checkbox value in Horizon.
  5. User Notifications:

    • Updates the system tray icon to reflect the state (different icons for Large Print and Receipts mode).
    • Optionally plays a sound based on the current mode, if sound is enabled.
    • Shows system tray tooltips to inform the user about the printer mode change.
  6. Error Handling:

    • Attempts to close any stray dialogs from Horizon to ensure the environment is clean for executing operations.
    • Handles missing Horizon windows by displaying error messages and re-entering a wait state.
    • Provides error notifications for failed sound playback, dialog interactions, or any toggling issues.
  7. Hotkey Functionality:

    • A hotkey (Ctrl + Numpad Multiply) is designated to trigger the toggle operation manually.
  8. Initialization and Bindings:

    • The script initializes on launch by calling the Init method of PrinterToggle, which checks for the Horizon application and sets up the starting environment.
    • A SetTimer function is used to periodically check if Horizon has started whenever it's not found running.

Key Outputs:

  • Tray Icon Updates: Visual feedback in the form of changing tray icons for different printer modes.
  • User Notifications: System tooltips inform the user of the current printer mode or any errors.
  • Optional Sound Alerts: Indicates mode changes with sound effects (disabled by default).
  • Error Messages: Warns users via tooltips if the environment is not suitable for toggling (e.g., Horizon is not running or errors occur while interacting with windows).

Example Use Case:

This script could be used in an environment where operators frequently switch between two different printer modes (e.g., printing large documents vs. receipts for a point-of-sale or ticket system). It improves workflow by automating interactions with the Horizon application through direct GUI control.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node