#NoEnv #SingleInstance Force SetTitleMatchMode, 2 CoordMode, Mouse, Screen SetWinDelay,…
- User
#NoEnv #SingleInstance Force SetTitleMatchMode, 2 CoordMode, Mouse, Screen SetWinDelay, 50 #WinActivateForce
; 全局变量 global g_bStop := false global g_WPSClass := "OpusApp" ; WPS的窗口类
; === 辅助函数 === ActivateWindow(title) { SetTitleMatchMode, 2 if WinExist(title) { WinActivate WinWaitActive, %title%, , 3 if !ErrorLevel { Sleep, 300 return true } } MsgBox, 无法激活窗口: %title% return false }
ShowToolTip(message, duration) { ToolTip, %message%, A_ScreenWidth//2, A_ScreenHeight//2 SetTimer, RemoveToolTip, %duration% }
RemoveToolTip: ToolTip SetTimer, RemoveToolTip, Off return
; === 主热键 === ^!r:: ; Ctrl+Alt+R g_bStop := false Hotkey, Escape, On
; 第一步 if (!g_bStop) { MouseMove, 1042, 104 Sleep, 300 Click Sleep, 500 Send, ^c } ; 第二步 - 跳转到Invoicing log if (!g_bStop) { if (!ActivateWindow("Invoicing log")) { g_bStop := true } else { Sleep, 300 Send, ^1 Sleep, 500 MouseMove, 320, 242 Sleep, 300 Click Sleep, 500 Send, ^v Sleep, 500 Send, ^l Sleep, 700 MouseMove, 29, 273 Sleep, 300 Click Sleep, 500 MouseMove, 64, 322 Sleep, 300 Click Sleep, 500 MouseMove, 27, 165 Sleep, 300 Click Sleep, 500 MouseMove, 51, 208 Sleep, 300 Click Sleep, 500 MouseMove, 240, 230 Sleep, 300 Click, 2 Sleep, 500 Send, {Left} } } ; 第三步 - 等到excel/wps打开后跳转到Print if (!g_bStop) { ; 等待WPS窗口激活 WinWaitActive, ahk_class %g_WPSClass%, , 5 if ErrorLevel { ShowToolTip("等待WPS超时", 1500) g_bStop := true } else { Sleep, 800 ; 等待WPS完全打开 if (!ActivateWindow("Print customer invoice journal")) { g_bStop := true } else { Sleep, 300 Send, ^1 Sleep, 500 Send, ^l Sleep, 700 MouseMove, 97, 20 Sleep, 300 Click } } } ; 第四步 - 跳转到Invoice basis - Sales if (!g_bStop) { if (!ActivateWindow("Invoice basis - Sales")) { g_bStop := true } else { Sleep, 300 Send, ^1 Sleep, 500 Send, ^l Sleep, 700 MouseMove, 30, 166 Sleep, 300 Click Sleep, 500 MouseMove, 60, 207 Sleep, 300 Click Sleep, 500 MouseMove, 238, 219 Sleep, 300 Click, 2 Sleep, 500 Send, {Left} } } ; 第五步 - 跳转到Voucher list if (!g_bStop) { ; 等待WPS窗口激活 WinWaitActive, ahk_class %g_WPSClass%, , 5 if ErrorLevel { ShowToolTip("等待WPS超时", 1500) g_bStop := true } else { Sleep, 800 ; 等待WPS完全打开 if (!ActivateWindow("Voucher list | Historical year 2022 July")) { g_bStop := true } else { Sleep, 300 Send, ^1 Sleep, 500 Send, ^l Sleep, 700 MouseMove, 26, 161 Sleep, 300 Click Sleep, 500 MouseMove, 66, 208 Sleep, 300 Click Sleep, 500 MouseMove, 213, 265 Sleep, 300 Click, 2 Sleep, 500 Send, {Left} } } } ; 完成提示 if (!g_bStop) { ShowToolTip("操作完成", 1500) } Hotkey, Escape, Offreturn
; 取消热键 Escape:: g_bStop := true ToolTip, 操作已取消, A_ScreenWidth//2, A_ScreenHeight//2 SetTimer, RemoveToolTip, 2000 return
以上是需要修改的ahk脚本,主要原因是EXCEL / WPS还没完全打开就跳转到了PRINT SUPPLIER INVOICE JOURNAL,导致当EXCEL / WPS真的完全打开的时候,又从PRINT SUPPLIER INVOICE JOURNAL跳转回到了EXCEL / WPS。真正要在PRINT SUPPLIER INVOICE JOURNAL执行的操作没法执行。以下是成功的AHK 脚本,需要按照成功脚本修改以上脚本。务必让所有操作在正确的页面进行
#NoEnv #SingleInstance Force SetTitleMatchMode, 2 CoordMode, Mouse, Screen SetWinDelay, 50 #WinActivateForce
; 全局变量 global g_bStop := false global g_WPSClass := "OpusApp" ; WPS的窗口类
; Windows 消息常量 global WM_KEYDOWN := 0x100 global WM_KEYUP := 0x101 global WM_CHAR := 0x102 global WM_SETTEXT := 0x000C global WM_PASTE := 0x302
; Windows API 常量 global SPI_SETFOREGROUNDLOCKTIMEOUT := 0x2000 global SPIF_SENDCHANGE := 2 global WS_EX_TOPMOST := 0x8 global HWND_TOPMOST := -1 global HWND_NOTOPMOST := -2 global SWP_NOMOVE := 0x2 global SWP_NOSIZE := 0x1
; === 辅助函数 === LockWindowSwitching() { DllCall("SystemParametersInfo", "UInt", SPI_SETFOREGROUNDLOCKTIMEOUT, "UInt", 0, "UInt", 0, "UInt", SPIF_SENDCHANGE) }
UnlockWindowSwitching() { DllCall("SystemParametersInfo", "UInt", SPI_SETFOREGROUNDLOCKTIMEOUT, "UInt", 0, "UInt", 200000, "UInt", SPIF_SENDCHANGE) }
SetWindowAlwaysOnTop(hwnd, onTop := true) { flag := onTop ? HWND_TOPMOST : HWND_NOTOPMOST return DllCall("SetWindowPos", "Ptr", hwnd, "Ptr", flag, "Int", 0, "Int", 0, "Int", 0, "Int", 0, "UInt", SWP_NOMOVE | SWP_NOSIZE) }
DisableWindowActivation(hwnd) { WinSet, ExStyle, +0x80, ahk_id %hwnd% ; 添加 WS_EX_TOOLWINDOW 样式 }
EnableWindowActivation(hwnd) { WinSet, ExStyle, -0x80, ahk_id %hwnd% ; 移除 WS_EX_TOOLWINDOW 样式 }
ProcessPrintWindow(wpsHwnd) { if WinExist("Print supplier invoice journal") { WinGet, printHwnd, ID
; 先禁用WPS窗口的激活能力 WinSet, ExStyle, +0x80, ahk_id %wpsHwnd% ; 添加 WS_EX_TOOLWINDOW 样式 Sleep, 500 ; 保持500ms等待时间 ; 激活Print窗口 WinActivate, ahk_id %printHwnd% WinWaitActive, ahk_id %printHwnd%, , 3 if !ErrorLevel { ; 设置Print窗口始终置顶 WinSet, AlwaysOnTop, On, ahk_id %printHwnd% Sleep, 500 ; 保持500ms等待时间 ; 执行Print窗口的操作 Send, ^l Sleep, 800 MouseMove, 96, 20 Sleep, 1000 Click Sleep, 1000 ; 发送Enter并等待 Send, {Enter} Sleep, 800 ; 减少到800ms ; 等待Print窗口关闭 WinWaitClose, ahk_id %printHwnd%, , 5 Sleep, 500 ; 快速恢复窗口状态 WinSet, AlwaysOnTop, Off, ahk_id %printHwnd% WinSet, ExStyle, -0x80, ahk_id %wpsHwnd% ; 移除 WS_EX_TOOLWINDOW 样式 ; 返回WPS窗口 WinActivate, ahk_id %wpsHwnd% WinWaitActive, ahk_id %wpsHwnd%, , 2 Sleep, 300 return true } } ShowToolTip("无法找到或激活Print窗口", 1500) return false}
SendMessageToWindow(title, msg, wParam := 0, lParam := 0) { if WinExist(title) { SendMessage, %msg%, %wParam%, %lParam% return ErrorLevel } return false }
SimulateKeyPress(hwnd, key) { PostMessage, WM_KEYDOWN, %key%, 0,, ahk_id %hwnd% Sleep, 50 PostMessage, WM_KEYUP, %key%, 0,, ahk_id %hwnd% Sleep, 50 }
SafeActivateWindow(title) { if WinExist(title) { WinGet, hwnd, ID WinGet, state, MinMax if (state = -1) ; 如果窗口最小化 WinRestore, ahk_id %hwnd%
; 使用 PostMessage 来激活窗口 PostMessage, 0x0086 ; WM_NCACTIVATE Sleep, 50 return hwnd } return 0}
WaitForWPSStable() { Sleep, 200 WinWaitActive, ahk_class %g_WPSClass%, , 2 Sleep, 100 }
; === 热键定义 === Escape:: g_bStop := true ToolTip, 操作已取消, A_ScreenWidth//2, A_ScreenHeight//2 SetTimer, RemoveToolTip, 2000 return
^!p:: ; 初始化 g_bStop := false Hotkey, Escape, On CoordMode, Mouse, Screen
; === 原始步骤完整保留 === ; 第1步 if (!g_bStop) { Send, ^r Sleep, 800 } ; 第2步 if (!g_bStop) { MouseMove, 94, 103 Sleep, 800 Click } ; 第4步 if (!g_bStop) { Sleep, 700 Send, ^c } ; 第5步 if (!g_bStop) { Sleep, 800 Send, ^s Sleep, 800 } ; 激活Supplier窗口 if (!g_bStop) { if (!ActivateWindow("Supplier invoice log")) { g_bStop := true } } ; 继续后续步骤 if (!g_bStop) { MouseMove, 69, 88 Sleep, 800 Click } if (!g_bStop) { MouseMove, 238, 281 Sleep, 800 Click } if (!g_bStop) { Sleep, 700 Send, ^v Sleep, 500 Send, {Tab} Sleep, 1000 Send, ^l Sleep, 1000 } ; 更多步骤... if (!g_bStop) { MouseMove, 23, 252 Sleep, 1000 Click } if (!g_bStop) { MouseMove, 53, 273 Sleep, 900 Click } if (!g_bStop) { Sleep, 800 Send, {Enter} } if (!g_bStop) { MouseMove, 29, 168 Sleep, 800 Click } if (!g_bStop) { MouseMove, 55, 216 Sleep, 800 Click }if (!g_bStop) { MouseMove, 313, 308 Sleep, 1000 Click Sleep, 79 Click }
if (!g_bStop) { Sleep, 100 Send, {Left} Sleep, 800 Send, {Enter} } ; 等待WPS窗口并获取句柄 if (!g_bStop) { WinWaitActive, Supplier invoice log-List.xlsx - WPS Office ahk_class %g_WPSClass% WinGet, wpsHwnd, ID, A Sleep, 800 } ; 处理Print窗口 if (!g_bStop) { if (!ProcessPrintWindow(wpsHwnd)) { g_bStop := true ShowToolTip("无法找到Print窗口", 1500) } } ; 激活Voucher窗口 if (!g_bStop) { if (!ActivateWindow("Voucher list | Historical year 2022 July")) { g_bStop := true } } if (!g_bStop) { Sleep, 1000 Send, ^l Sleep, 800 } ; 最终步骤 if (!g_bStop) { MouseMove, 22, 165 Sleep, 1000 Click } if (!g_bStop) { MouseMove, 56, 208 Sleep, 100 Click } if (!g_bStop) { MouseMove, 254, 328 Sleep, 1000 Click Sleep, 79 Click } if (!g_bStop) { Sleep, 100 Send, {Left} Sleep, 800 Send, {Enter} } if (!g_bStop) { WinWaitActive, Voucher list-List.xlsx - WPS Office ahk_class %g_WPSClass% Sleep, 800 } if (!g_bStop) { Send, {Alt down} Sleep, 100 Send, 6 Sleep, 100 Send, f Sleep, 100 Send, {Alt up} } ; 只有所有步骤完成才显示 if (!g_bStop) { ShowToolTip("操作完成", 1500) } Hotkey, Escape, Offreturn
^!m:: ; 初始化 g_bStop := false Hotkey, Escape, On CoordMode, Mouse, Screen
; === 完整保留所有原始步骤 === ; 第2步 if (!g_bStop) { MouseMove, 23, 252 Sleep, 1000 Click } ; 第4步 if (!g_bStop) { MouseMove, 53, 273 Sleep, 900 Click } ; 第6步 if (!g_bStop) { Sleep, 800 Send, {Enter} } ; 第7步 if (!g_bStop) { MouseMove, 29, 168 Sleep, 800 Click } ; 第9步 if (!g_bStop) { MouseMove, 55, 216 Sleep, 800 Click }if (!g_bStop) { MouseMove, 313, 308 Sleep, 1000 Click Sleep, 79 Click }
if (!g_bStop) { Sleep, 100 Send, {Left} Sleep, 800 Send, {Enter} } ; 等待WPS窗口并获取句柄 if (!g_bStop) { WinWaitActive, Supplier invoice log-List.xlsx - WPS Office ahk_class %g_WPSClass% WinGet, wpsHwnd, ID, A Sleep, 800 } ; 处理Print窗口 if (!g_bStop) { if (!ProcessPrintWindow(wpsHwnd)) { g_bStop := true ShowToolTip("无法找到Print窗口", 1500) } } ; 激活Voucher窗口 if (!g_bStop) { if (!ActivateWindow("Voucher list | Historical year 2022 July")) { g_bStop := true } } if (!g_bStop) { Sleep, 1000 Send, ^l Sleep, 800 } ; 后续步骤 if (!g_bStop) { MouseMove, 22, 165 Sleep, 1000 Click } if (!g_bStop) { MouseMove, 56, 208 Sleep, 100 Click } if (!g_bStop) { MouseMove, 254, 328 Sleep, 1000 Click Sleep, 79 Click } if (!g_bStop) { Sleep, 100 Send, {Left} Sleep, 800 Send, {Enter} } ; 等待窗口激活 if (!g_bStop) { WinWaitActive, Voucher list-List.xlsx - WPS Office ahk_class %g_WPSClass% Sleep, 800 } ; Alt组合键操作 if (!g_bStop) { Send, {Alt down} Sleep, 100 Send, 6 Sleep, 100 Send, f Sleep, 100 Send, {Alt up} } ; 最后一步 if (!g_bStop) { MouseMove, 550, 263 Sleep, 200 Click } ; 只有所有步骤完成才显示 if (!g_bStop) { ShowToolTip("操作完成", 1500) } Hotkey, Escape, Offreturn
; === 辅助函数 === ActivateWindow(title) { SetTitleMatchMode, 2 if WinExist(title) { WinActivate WinWaitActive, %title%, , 3 if !ErrorLevel { Sleep, 800 return true } }
if WinExist("ahk_class " . g_WPSClass) { WinActivate WinWaitActive, ahk_class %g_WPSClass%, , 3 if !ErrorLevel { Sleep, 800 return true } } MsgBox, 无法激活窗口: %title% return false}
ShowToolTip(message, duration) { ToolTip, %message%, A_ScreenWidth//2, A_ScreenHeight//2 SetTimer, RemoveToolTip, %duration% }
RemoveToolTip: ToolTip SetTimer, RemoveToolTip, Off return
Assistant
根据你提供的成功脚本,我来修改第一个脚本。主要问题是需要正确处理WPS和Print窗口之间的切换。以下是修改后的脚本:
#NoEnv #SingleInstance Force SetTitleMatchMode, 2 CoordMode, Mouse, Screen SetWinDelay, 50 #WinActivateForce ; 全局变量 global g_bStop := false global g_WPSClass := "OpusApp" ; WPS的窗口类 ; Windows 消息常量 global WM_KEYDOWN := 0x100 global WM_KEYUP := 0x101 global WM_CHAR := 0x102 global WM_SETTEXT := 0x000C global WM_PASTE := 0x302 ; Windows API 常量 global SPI_SETFOREGROUNDLOCKTIMEOUT := 0x2000 global SPIF_SENDCHANGE := 2 global WS_EX_TOPMOST := 0x8 global HWND_TOPMOST := -1 global HWND_NOTOPMOST := -2 global SWP_NOMOVE := 0x2 global SWP_NOSIZE := 0x1 ; === 核心处理函数 === ProcessPrintWindow(wpsHwnd) { if WinExist("Print customer invoice journal") { WinGet, printHwnd, ID ; 先禁用WPS窗口的激活能力 WinSet, ExStyle, +0x80, ahk_id %wpsHwnd% ; 添加 WS_EX_TOOLWINDOW 样式 Sleep, 500 ; 保持500ms等待时间 ; 激活Print窗口 WinActivate, ahk_id %printHwnd% WinWaitActive, ahk_id %printHwnd%, , 3 if !ErrorLevel { ; 设置Print窗口始终置顶 WinSet, AlwaysOnTop, On, ahk_id %printHwnd% Sleep, 500 ; 保持500ms等待时间 ; 执行Print窗口的操作 Send, ^1 Sleep, 500 Send, ^l Sleep, 700 MouseMove, 97, 20 Sleep, 300 Click Sleep, 500 ; 等待Print窗口关闭或操作完成 Sleep, 800 ; 快速恢复窗口状态 WinSet, AlwaysOnTop, Off, ahk_id %printHwnd% WinSet, ExStyle, -0x80, ahk_id %wpsHwnd% ; 移除 WS_EX_TOOLWINDOW 样式 ; 返回WPS窗口 WinActivate, ahk_id %wpsHwnd% WinWaitActive, ahk_id %wpsHwnd%, , 2 Sleep, 300 return true } } ShowToolTip("无法找到或激活Print窗口", 1500) return false } ; === 辅助函数 === ActivateWindow(title) { SetTitleMatchMode, 2 if WinExist(title) { WinActivate WinWaitActive, %title%, , 3 if !ErrorLevel { Sleep, 300 return true } } MsgBox, 无法激活窗口: %title% return false } ShowToolTip(message, duration) { ToolTip, %message%, A_ScreenWidth//2, A_ScreenHeight//2 SetTimer, RemoveToolTip, %duration% } RemoveToolTip: ToolTip SetTimer, RemoveToolTip, Off return ; === 主热键 === ^!r:: ; Ctrl+Alt+R g_bStop := false Hotkey, Escape, On ; 第一步 if (!g_bStop) { MouseMove, 1042, 104 Sleep, 300 Click Sleep, 500 Send, ^c } ; 第二步 - 跳转到Invoicing log if (!g_bStop) { if (!ActivateWindow("Invoicing log")) { g_bStop := true } else { Sleep, 300 Send, ^1 Sleep, 500 MouseMove, 320, 242 Sleep, 300 Click Sleep, 500 Send, ^v Sleep, 500 Send, ^l Sleep, 700 MouseMove, 29, 273 Sleep, 300 Click Sleep, 500 MouseMove, 64, 322 Sleep, 300 Click Sleep, 500 MouseMove, 27, 165 Sleep, 300 Click Sleep, 500 MouseMove, 51, 208 Sleep, 300 Click Sleep, 500 MouseMove, 240, 230 Sleep, 300 Click, 2 Sleep, 500 Send, {Left} } } ; 第三步 - 等待excel/wps完全打开后处理Print窗口 if (!g_bStop) { ; 等待WPS窗口激活并获取句柄 WinWaitActive, ahk_class %g_WPSClass%, , 5 if ErrorLevel { ShowToolTip("等待WPS超时", 1500) g_bStop := true } else { WinGet, wpsHwnd, ID, A ; 获取当前活动的WPS窗口句柄 Sleep, 800 ; 等待WPS完全打开 ; 使用专门的函数处理Print窗口 if (!ProcessPrintWindow(wpsHwnd)) { g_bStop := true } } } ; 第四步 - 跳转到Invoice basis - Sales if (!g_bStop) { if (!ActivateWindow("Invoice basis - Sales")) { g_bStop := true } else { Sleep, 300 Send, ^1 Sleep, 500 Send, ^l Sleep, 700 MouseMove, 30, 166 Sleep, 300 Click Sleep, 500 MouseMove, 60, 207 Sleep, 300 Click Sleep, 500 MouseMove, 238, 219 Sleep, 300 Click, 2 Sleep, 500 Send, {Left} } } ; 第五步 - 等待WPS打开后跳转到Voucher list if (!g_bStop) { ; 等待WPS窗口激活 WinWaitActive, ahk_class %g_WPSClass%, , 5 if ErrorLevel { ShowToolTip("等待WPS超时", 1500) g_bStop := true } else { Sleep, 800 ; 等待WPS完全打开 if (!ActivateWindow("Voucher list | Historical year 2022 July")) { g_bStop := true } else { Sleep, 300 Send, ^1 Sleep, 500 Send, ^l Sleep, 700 MouseMove, 26, 161 Sleep, 300 Click Sleep, 500 MouseMove, 66, 208 Sleep, 300 Click Sleep, 500 MouseMove, 213, 265 Sleep, 300 Click, 2 Sleep, 500 Send, {Left} } } } ; 完成提示 if (!g_bStop) { ShowToolTip("操作完成", 1500) } Hotkey, Escape, Off return ; 取消热键 Escape:: g_bStop := true ToolTip, 操作已取消, A_ScreenWidth//2, A_ScreenHeight//2 SetTimer, RemoveToolTip, 2000 return主要修改点:
-
添加了Windows API常量和消息常量 - 这些是处理窗口切换问题所必需的
-
添加了
ProcessPrintWindow(wpsHwnd)函数 - 这是核心解决方案:- 先禁用WPS窗口的激活能力,防止自动切换回去
- 激活Print窗口并设置置顶
- 执行Print窗口的操作
- 完成后恢复窗口状态
-
修改了第三步的逻辑:
- 等待WPS窗口完全打开
- 获取WPS窗口句柄:
WinGet, wpsHwnd, ID, A - 使用
ProcessPrintWindow(wpsHwnd)来处理Print窗口操作
-
保持了原有的所有操作步骤,只是改进了窗口切换的处理方式
这样修改后,脚本会正确等待WPS完全打开,然后在正确的窗口中执行每个操作,避免了窗口自动切换导致的操作错误。
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.