在 AutoIt 中将命令行参数传递给 SwitchCase



我有一个具有GUI的脚本,并且我一直使用以下代码使用开始按钮运行:

Case $StartButton

我还想尝试使用Windows TaskScheduler在每天早上8 AM EST运行。添加条件以从开始按钮开始或在 TaskScheduler 在美国东部标准时间上午 8 点(或任何特定时间)运行时的最佳方法是什么?我对只做上午 8 点的条件犹豫不决,因为它可能会增加处理量,总是看时间。

从本质上讲,我希望发生的是让我的计算机使用任务计划程序自动解锁(登录?)并运行此已编译为exe的AutoIt脚本。

FilePath 是:C:\Users\robert\OneDrive\Desktop\TempFile.exe

相关代码块如下:

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Save
SaveOptions()
Case $StartButton
;TAB1 of GUI


If WinExists("[CLASS: QT373947473845]") Then
$oBlah = WinGetHandle("[CLASS: QT373947473845]")
$BSLoc = WinGetPos ("[CLASS: QT373947473845]")
If $BSLoc[0] <> 0 or $BSLoc[1] <> 0 or $BSLoc[2] <> 800 or $BSLoc[3] <> 600 Then
WinSetState ( $oBlah, "", @SW_MINIMIZE )
sleep(500)
WinActivate($oBlah)
WinWaitActive($oBlah)
WinMove($oBlah, "", 0, 0, 800, 600)
sleep(2000)
Else
WinActivate($oBlah)
WinWaitActive($oBlah)
sleep(2000)
EndIf
Endif
EndSwitch
WEnd

我在案例中有数千行其他代码,但我试图限制它,因为该部分无关紧要

案例$StartButton是我尝试执行OR的行,如果由任务管理器运行。我读过你不能在开关案例中做一个OR功能,但如果你不中断地做2个案例,那是一回事吗?

我阅读了一些文档,看到我可以在命令行末尾添加一个"/prim1=value",它会将 prim1 参数传递给 $CmdLine[0],但我似乎无法让它正常工作。

查看文档。 特别是这部分:

因此,如果您要通过传递命令行来使用编译的可执行文件 参数:
myProg.exe param1 "This is a string parameter" 99

$CmdLine[0] ; this equals 3(因为有 3 个命令行参数)
$CmdLine[1] ; This contains "param1".$CmdLine[2] ; This contains "This is a string parameter".$CmdLine[3] ; This contains 99.


因此,只需修改代码,使其在$CmdLine[1] = something时的行为有所不同。

至于开关情况:即在GUI的消息循环内部,当按下启动按钮时,开关块Case $StartButton部分中的代码运行,$nMsg等于启动按钮的控制ID($StartButton)。

如果你想在其他时间运行这段代码,我会做的是将所有代码移动到它自己的函数中:

Func onStartClick()
; start button code here
Endfunc

然后只需在交换机块中调用onStartClick()

$nMsg = GUIGetMsg()
Switch $nMsg
Case $StartButton
onStartClick()
Case $someOtherButton
; someOtherButton code...etc
EndSwitch

然后,如果存在特定的命令行参数,您也可以调用此函数(将此代码放在 Switch之前...端开关块,不在里面):

If $CmdLine[0] >= 1 And $CmdLine[1] = "param1" then
; other code to run when started with "program.exe param1"
onStartClick()
EndIf

所以整个事情看起来像这样:

Func onStartClick()
; start button code here
Endfunc
If $CmdLine[0] >= 1 And $CmdLine[1] = "param1" then
; other code to run when started with "param1"
onStartClick()
EndIf
$nMsg = GUIGetMsg()
Switch $nMsg
Case $StartButton
onStartClick()
Case $someOtherButton
; someOtherButton code...etc
EndSwitch

最新更新