首页 > 解决方案 > 将脚本转换为在后台运行

问题描述

当我使用 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

标签: autohotkey

解决方案


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

推荐阅读