Skip to content

Instantly share code, notes, and snippets.

@ayanamist
Created May 26, 2025 04:27
Show Gist options
  • Save ayanamist/d28e2191e98a485730677e84e4f14a68 to your computer and use it in GitHub Desktop.
Save ayanamist/d28e2191e98a485730677e84e4f14a68 to your computer and use it in GitHub Desktop.
Run a console application "proxy.exe" and show a tray icon, provide show/hide console window+restart process ability.
; Script settings
#Persistent
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
; Process and window title variables
ProcessName := "proxy.exe"
ProxyPID := 0
WindowVisible := false
; Set up custom tray menu
Menu, Tray, NoStandard
if (A_IsCompiled) {
Menu, Tray, Icon, %A_ScriptFullPath%
} else {
Menu, Tray, Icon, %A_ScriptDir%\clash.ico
}
Menu, Tray, Tip, Clash
Menu, Tray, Add, Show Window, ToggleWindow
Menu, Tray, Add, Restart Proxy, RestartApp
Menu, Tray, Add
Menu, Tray, Add, Exit, CleanExit
Menu, Tray, Default, Show Window
; Initialize the application
InitApp()
; Start monitoring the proxy process
MonitorProxyProcess()
; Initialize the application
InitApp() {
global ProcessName, ProxyPID
; Check if the process is already running
Process, Exist, %ProcessName%
; If running, store the PID
if (ErrorLevel) {
ProxyPID := ErrorLevel
} else {
; If not running, start it
ProxyPID := RunProxy()
}
; Update menu text based on current state
UpdateToggleMenuText()
}
; Monitor the proxy process and exit when it terminates
MonitorProxyProcess() {
global ProxyPID
if (ProxyPID) {
; Start the monitoring in a separate thread
SetTimer, WaitForProxyExit, -100
}
}
; Wait for proxy process to exit and then exit the script
WaitForProxyExit() {
global ProxyPID
; Wait for the process to exit
Process, WaitClose, %ProxyPID%
; When the process exits, exit this script too
ExitApp
}
; Run the proxy application
RunProxy() {
global ProcessName, WindowVisible
Run, %ProcessName%, %A_ScriptDir%, Hide, NewPID
Sleep, 200
if (WindowVisible) {
WinShow, ahk_pid %NewPID%
WinActivate, ahk_pid %NewPID%
} else {
WinHide, ahk_pid %NewPID%
}
return NewPID
}
; Toggle the proxy console window
ToggleWindow() {
global ProxyPID, WindowVisible
if (ProxyPID) {
DetectHiddenWindows, On
WinGet, style, Style, ahk_pid %ProxyPID%
isVisible := (style & 0x10000000) ; WS_VISIBLE
if (isVisible) {
WinHide, ahk_pid %ProxyPID%
WindowVisible := false
} else {
WinShow, ahk_pid %ProxyPID%
WinActivate, ahk_pid %ProxyPID%
WindowVisible := true
}
DetectHiddenWindows, Off
UpdateToggleMenuText()
}
}
; Update the toggle window menu text
UpdateToggleMenuText() {
global ProxyPID
if (ProxyPID) {
DetectHiddenWindows, On
WinGet, style, Style, ahk_pid %ProxyPID%
isVisible := (style & 0x10000000) ; WS_VISIBLE
if (isVisible) {
try {
Menu, Tray, Rename, Show Window, Hide Window
Menu, Tray, Default, Hide Window
} catch {
; Menu item doesn't exist or can't be renamed, ignore the error
}
} else {
try {
Menu, Tray, Rename, Hide Window, Show Window
Menu, Tray, Default, Show Window
} catch {
; Menu item doesn't exist or can't be renamed, ignore the error
}
}
DetectHiddenWindows, Off
}
}
; Restart the proxy application
RestartApp() {
global ProxyPID
; Kill the current process if it exists
if (ProxyPID) {
Process, Close, %ProxyPID%
; Wait for the process to fully terminate
Process, WaitClose, %ProxyPID%, 5
}
; Start a new instance
ProxyPID := RunProxy()
}
; Exit handler - clean up when script exits
CleanExit() {
global ProxyPID
; Close the proxy process if it exists
if (ProxyPID) {
Process, Close, %ProxyPID%
}
ExitApp ; Now uses the built-in command to exit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment