我的 AutoHotkey 脚本显示正在运行,但没有发生任何预期的任务。

huangapple go评论48阅读模式
英文:

My AutoHotkey script is showing as running but none of its intended task is happening

问题

我需要帮助我的AHK脚本。

我编写了一个AHK脚本,用于搜索标题为“Google Chrome”的窗口,从列表中选择一个随机窗口,通过将其置于前台来激活它,并在切换到另一个Google Chrome窗口之前设置一个随机的休眠时间,介于8到15秒之间。

执行代码后,似乎它正在运行,因为它出现在我的PC的托盘部分,但实际上什么都没有发生,再次强调,预期的任务是搜索所有已打开的Google Chrome窗口,随机选择其中一个并使其保持在前台,然后等待8到15秒后切换到另一个Google Chrome窗口。

我的AHK脚本如下:

#Persistent
SetTitleMatchMode, 2

Loop
{
    ; 用实际应用程序窗口名称替换“Google Chrome”
    WinGet, hWndList, List, Google Chrome

    if (ErrorLevel != "ERROR")
    {
        ; 从列表中选择一个随机窗口
        Random, index, 1, % hWndList.MaxIndex()
        
        ; 获取所选窗口的句柄
        hWnd := hWndList%index%
        
        ; 激活所选窗口
        WinActivate, ahk_id %hWnd%
        
        ; 定义切换窗口的时间间隔(毫秒)
        Random, sleepTime, 8000, 15000 ; 根据需要调整这些值
        Sleep, %sleepTime%
    }
    else
    {
        ; 未找到匹配的窗口,等待后再次检查
        Sleep, 1000
    }
}

希望这有助于解决您的问题。

英文:

I need help with my AHK script

I wrote an AHK script that searches for windows with the title "Google Chrome," selects a random window from the list, activates it by bringing it to the foreground, and sets a random sleep time between 8 and 15 seconds before switching to another window of Google Chrome.

After executing the code, it seems it is running as it appeared on my tray section of my PC, but nothing actually happened, again, the intended task is to search for all Google Chrome windows opened and should randomly pick any of them and bring to the foreground to stay within 8 to 15 seconds, before switching to another Google Chrome window.

My AHK Script below:

#Persistent
SetTitleMatchMode, 2

Loop
{
	; Replace "App Name" with the common name of the application windows name
	WinGet, hWndList, List, Google Chrome

	if (ErrorLevel != "ERROR")
	{
		; Select a random window from the list
		Random, index, 1, % hWndList.MaxIndex()
		
		; Get the window handle of the selected window
		hWnd := hWndList%index%
		
		; Activate the selected window
		WinActivate, ahk_id %hWnd%
		
		; Define the time interval for switching windows (in milliseconds)
		Random, sleepTime, 8000, 15000 ; Adjust the values as needed
		Sleep, %sleepTime%
	}
	else
	{
		; No matching windows found, wait before checking again
		Sleep, 1000
	}
}

答案1

得分: 1

以下是您要翻译的内容:

没有名为“ERROR”的[ErrorLevel](https://www.autohotkey.com/docs/v1/misc/ErrorLevel.htm),而且`WinGet`<sup>[(文档)](https://www.autohotkey.com/docs/v1/lib/WinGet.htm)</sup>根本不设置ErrorLevel。

而且由于`WinGet`是一个非常老旧的命令,它返回一个[伪数组](https://www.autohotkey.com/docs/v1/misc/Arrays.htm#pseudo)。因此,您不能在其上调用`.MaxIndex()`。伪数组中的元素数量在输出变量中指定,如`WinGet, , List`文档中所述:

> 每个ID号存储在以OutputVar自己的名称开头的变量中(以形成伪数组),而OutputVar本身设置为检索到的项目数(如果没有则为0)。

因此,您的修复代码将如下所示:

SetTitleMatchMode, 2

Loop
{
    ; 用应用程序窗口的常见名称替换“App Name”
    WinGet, hWndList, List, Google Chrome

    if (hWndList != 0) ;可以替换为(!hWndList)
    {
        ; 从列表中选择一个随机窗口
        Random, index, 1, % hWndList

        ; 获取所选窗口的句柄
        hWnd := hWndList%index%

        ; 激活所选窗口
        WinActivate, % "ahk_id " hWnd

        ; 定义切换窗口的时间间隔(毫秒)
        Random, sleepTime, 8000, 15000 ;根据需要调整值
        Sleep, % sleepTime
    }
    else
    {
        ; 未找到匹配的窗口,在下次检查之前等待
        Sleep, 1000
    }
}
英文:

There is no such ErrorLevel as &quot;ERROR&quot; and WinGet<sup>(docs)</sup> doesn't set an ErrorLevel anyway.

And since WinGet is a very legacy command, it returns a pseudo array. Because of that, you can't call .MaxIndex() on it. The number of elements in the pseudo array is specified in the output variable, as stated in the WinGet, , List documentation:

> Each ID number is stored in a variable whose name begins with OutputVar's own name (to form a pseudo-array), while OutputVar itself is set to the number of retrieved items (0 if none).

So your fixed code would be:

SetTitleMatchMode, 2

Loop
{
    ; Replace &quot;App Name&quot; with the common name of the application windows name
    WinGet, hWndList, List, Google Chrome

    if (hWndList != 0) ;can be replaced with just (!hWndList)
    {
        ; Select a random window from the list
        Random, index, 1, % hWndList

        ; Get the window handle of the selected window
        hWnd := hWndList%index%

        ; Activate the selected window
        WinActivate, % &quot;ahk_id &quot; hWnd

        ; Define the time interval for switching windows (in milliseconds)
        Random, sleepTime, 8000, 15000 ; Adjust the values as needed
        Sleep, % sleepTime
    }
    else
    {
        ; No matching windows found, wait before checking again
        Sleep, 1000
    }
}

huangapple
  • 本文由 发表于 2023年6月26日 10:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定