others - AutoHotKey脚本如何启动批处理脚本?

这个脚本将我从Windows切换到Linux(VM)


Pause::vmStart()


return



runVM := false


linux := false



vmStart()


{


 If (!runVM and !linux) {


 Run, C:Userspatrickdev-vmPS.cmd


 runVM := true


 sleep, 18000


 }


 If (!linux and !WinExist("DevVM - 127.0.0.1:23389 - RDP")) {


 Run, C:Userspatrickdev-vmRDP.cmd


 }


 if (!linux) {


 WinShow, DevVM - 127.0.0.1:23389 - RDP


 WinActivate, DevVM - 127.0.0.1:23389 - RDP


 }


 Send ^!{CtrlBreak}


 linux := !linux


}



时间: 原作者:


首先,你定义的变量无法访问。


Pause::vmStart()


return



runVM := false


linux := false



代码执行在遇到的第一个热键处停止,
此外,还有一个Return,它会停止代码执行,

这不是实际的问题,但仍然需要修复,


要定义全局变量,请将函数的第一行定义为:


vmStart()


{


 global runVM, linux


 ...



要将它们定义为超级全局变量,可以在函数(在脚本的最上面)之外用关键字global定义变量,如下所示:


global runVM := false


global linux := false


0::vmStart()


...




这是成品:


Pause::vmStart()


;the 'return' here did nothing for us, removed



vmStart()


{


 ;using the static keyword to make the function assume


 ;all variables are static, and also skipped even 


 ;declaring the variables, not needed due to how forgiving AHK is


 static



 ;got rid of the 'and' keyword in your if statements


 ;that's legacy syntax, big ew, it's not 2005


 If (!runVM && !linux) {


 Run, C:Userspatrickdev-vmPS.cmd


 runVM := true


 sleep, 18000


 }


 ;removed braces, one-liner statments don't need them


 ;just personal preference though, of course


 If (!linux && !WinExist("DevVM - 127.0.0.1:23389 - RDP")) 


 Run, C:Userspatrickdev-vmRDP.cmd


 if (!linux) {


 WinShow, DevVM - 127.0.0.1:23389 - RDP


 WinActivate, DevVM - 127.0.0.1:23389 - RDP


 }


 ;switched to SendInput, it's faster and more reliable


 SendInput, ^!{CtrlBreak}


 linux := !linux


}



原作者:
...