autohotkey - 将脚本转换为在后台运行
问题描述
当我使用 Send 和 MouseClick 而不是 ControlSend 和 ControlClick(以及 wb.Visible = true)在前台运行该脚本时,它运行良好。在后台运行它有什么问题?
code1 := "foobar"
url := "https://app.powerbi.com/groups/me/reports/xxx"
wb := ComObjCreate("InternetExplorer.Application") ;create com object
wb.Visible := false ;true to show IE
wb.Navigate(url)
while (wb.busy) ;wait while page loads
sleep 10
ControlSend,, {LWin down}{Up down}
ControlSend,, {LWin up}{Up up}
sleep 20000
ControlClick, left, 1275, 320
sleep 3000
ControlClick, left, 1275, 510
sleep 500
ControlClick, left, 2200, 1380
sleep 3000
ControlClick, left, 2200, 1380
sleep 3000
ControlClick, left, 2200, 1380
sleep 120000
ControlClick, left, 2760, 2000
sleep 500
ControlClick, left, 2760, 2000
sleep 5000
ControlClick, left, 2760, 2000
sleep 3000
ControlClick, left, 2760, 2000
解决方案
DetectHiddenWindows, On
指令可以检测在后台运行的窗口。
击键和字符串可以传递到隐藏或非活动窗口。例如,下面的代码片段启动 notepad.exe,即使记事本在后台运行,也会发送消息“向 notepad.exe 发送字符串”。
#SingleInstance, Force
DetectHiddenWindows, On
Run, Notepad, , Hide ; run notepad in the background
if WinExist("ahk_class Notepad")
ControlSend, Edit1, sending a string to the notepad.exe, Untitled
; verify: wait for 2s to see the effect
sleep 2000
WinShow
同样对于您的问题,它有望工作(无法测试,没有 powerbi 帐户)
DetectHiddenWindows, On
code1 := "foobar"
url := "https://app.powerbi.com/groups/me/reports/xxx"
wb := ComObjCreate("InternetExplorer.Application") ;create com object
wb.Visible := false ;true to show IE
wb.Navigate(url)
while (wb.busy) ;wait while page loads
sleep 10
if WinExist("ahk_class IEFrame")
;MsgBox % "hidden window id = " . WinExist("A")
ControlSend,, {LWin down}{Up down}
ControlSend,, {LWin up}{Up up}
sleep 20000
ControlClick, left, 1275, 320
sleep 3000
ControlClick, left, 1275, 510
sleep 500
ControlClick, left, 2200, 1380
sleep 3000
ControlClick, left, 2200, 1380
sleep 3000
ControlClick, left, 2200, 1380
sleep 120000
ControlClick, left, 2760, 2000
sleep 500
ControlClick, left, 2760, 2000
sleep 5000
ControlClick, left, 2760, 2000
sleep 3000
ControlClick, left, 2760, 2000
推荐阅读
- groovy - 如何在我的 groovy 脚本的 --srcPattern 标志中使用正则表达式?
- postman - 使用 API 与 MQTT 发布者/订阅者通信
- r - 通过列的组合重塑 data.frame
- python-3.x - 如何从 str 列中提取特定文本并将其带到另一列
- javascript - 如何使用带有reactjs的single-spa渲染本地json
- python - 为什么添加用户功能在pyftpdlib中不起作用
- excel - 带有 Google-directions API 的 Excel 网络服务公式
- node.js - Google Tail 日志条目:关于数据事件未收到任何消息
- java - 路径中有空格时找不到持久性提供程序
- python - 如何使用 pydantic/fastapi 接受具有数组\单个项目的单个路由对象?