![在 AutoHotkey 腳本中使用 OR 和條件](https://rvso.com/image/1642370/%E5%9C%A8%20AutoHotkey%20%E8%85%B3%E6%9C%AC%E4%B8%AD%E4%BD%BF%E7%94%A8%20OR%20%E5%92%8C%E6%A2%9D%E4%BB%B6.png)
我創建了以下 AHK 腳本來回答我自己的問題這裡:
NumpadEnter::
Process, Exist, httpd.exe
If ErrorLevel = 0
{
Run, C:\XAMPP\apache_start.bat,,Hide
Run, C:\XAMPP\mysql_start.bat,,Hide
}
Else
{
Run, C:\XAMPP\apache_stop.bat
Run, C:\XAMPP\mysql_stop.bat
Sleep, 2000
Run, C:\XAMPP\apache_start.bat,,Hide
Run, C:\XAMPP\mysql_start.bat,,Hide
}
Return
然而,該腳本並不完美——目前,它只檢查 Apache 進程是否存在httpd.exe
,但 XAMPP 會啟動 Apache 伺服器和使用該進程的 MySQL 資料庫mysqld.exe
。在 Apache 關閉但 MySQL 啟動的情況下,腳本的邏輯似乎會失敗,所以我想修復這一行:
Process, Exist, httpd.exe
....檢查是否存在任何一個 httpd.exe
或者mysqld.exe
。
AHK 有一個 OR 運算符,您可以使用or
or ||
,但執行以下操作:
Process, Exist, httpd.exe || mysqld.exe
……只是嘗試啟動伺服器(即運行第一塊程式碼,表示邏輯和/或語法失敗)。換句話說,OR 似乎不能與諸如 之類的條件結合使用Process, Exist
。
AHK 可以做到這一點嗎?
答案1
在IF
表達式中,AHK 使用運算子or
(文件)。您可以調用Process
兩次,每次儲存結果,然後檢查其中一個是否為0
。
Process, Exist, httpd.exe
errHTTPD := ErrorLevel
Process, Exist, mysqld.exe
errMYSQLD := ErrorLevel
If (errHTTPD = 0 or errMYSQLD = 0)
{
...
}
else
{
...
}
作為一種替代方案,不能直接回答您的問題,但我確實認為它適用於您的特定情況:
Process, Exist, httpd.exe
IfEqual, ErrorLevel, 0
Process, Exist, mysqld.exe
首先,檢查httpd.exe
.如果是這樣ErrorLevel
,0
那麼尋找mysqld.exe
。