我正在嘗試在 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。