我正在尝试在 AHK(自动热键)中编写一个脚本来检测是否有任何外部硬盘连接,然后运行脚本中的下一个命令。假设下面是脚本。
A
B
C
D
E
我希望 A 到 C 是脚本,用于检查外部驱动器是否已连接。如果是,命令将转到行 D,否则转到行 E。我已经检查了一些脚本,但没有运气。尝试了此脚本关联作为参考,但不确定如何根据我的要求进行修改。
答案1
如果您知道外部硬盘的标签,您可以使用以下命令:
; get a list of all the hard drives. Hard drives are considered as FIXED by AHK
DriveGet, drives, list, FIXED
Loop, Parse, drives ; loop through each drive letter
{
DriveGet, DriveLabel, Label, %A_LoopField%: ; get the drive label
; IF DriveLabel not contains External HDD1 label,External HDD2 label
IF (DriveLabel != "External HDD label") ; If you want to use only one External HDD
Continue
ExternalDriveLetter := A_LoopField ; get the drive letter of the last found
; or
; get the drive letter of the first found
; ExternalDriveLetter = %A_LoopField%
; Break
}
IfExist, %ExternalDriveLetter%:
Run %ExternalDriveLetter%: ; go to line D
else
MsgBox, No External HDD is connected ; go to line E
答案2
Loop
{
WinWaitActive, DiskInDrive ; put the title in here for the dialog box to wait for indefinitely -- will need to exit from tray
; put code here to execute any time the window is active
; after code is done, program will loop and wait again
}
如果对话框默认没有变为活动状态,您也可以在上面的 WinWaitActive 语句之前使用 WinWait 和 WinActivate。