iterm的rz命令只能選擇文件夾不能選擇文件的問題
這個問題非常典型!當執行 `rz` 后彈出的選擇窗口只能選文件夾而不能選文件時,幾乎可以肯定是 **Zmodem 觸發器腳本配置問題**。
核心原因是:**iTerm2 運行的腳本是舊版本或配置錯誤的版本,它錯誤地調用了選擇目錄的對話框,而不是選擇文件的對話框。**
請按照以下步驟徹底解決:
### 方案一:檢查和更新觸發器腳本(最可能的原因)
舊的或來源不同的 `iterm2-recv-zmodem.sh` 腳本可能使用了錯誤的 AppleScript 命令。
1. **首先,找到你當前正在使用的腳本**。你之前在 iTerm2 的 Triggers 配置中設置了路徑,通常是 `~/bin/iterm2-recv-zmodem.sh`。
2. **打開并編輯這個腳本**:
```bash
# 用 nano 或你喜歡的編輯器打開它
nano ~/bin/iterm2-recv-zmodem.sh
```
3. **檢查關鍵部分**。腳本的核心是使用 `osascript` 執行 AppleScript 來彈出文件選擇框。你需要找到類似下面的代碼塊:
**? 錯誤的代碼(用于選擇文件夾):**
```bash
osascript -e 'tell application "iTerm2" to choose folder with prompt "Choose a file to send"'
```
關鍵字是 `choose folder`,這只會讓你選擇文件夾。
**? 正確的代碼(用于選擇文件):**
```bash
osascript -e 'tell application "iTerm2" to choose file with prompt "Choose a file to send"'
```
關鍵字是 `choose file`,這才是用于選擇文件的。
另一個更健壯的正確版本可能是:
```bash
#!/bin/bash
# Author:
# Description:
osascript -e 'tell application "iTerm2" to choose file with prompt "Choose a file to send"' | sed 's/:/\/g'
```
4. **修改并保存**。如果你的腳本中是 `choose folder`,請將其修改為 `choose file`,然后保存退出。
5. **立即測試**。重新連接到服務器,再次運行 `rz` 命令,現在彈出的應該就是文件選擇對話框了。
---
### 方案二:直接替換為新版腳本(推薦)
如果不想手動修改,最穩妥的方法是直接下載一個已知工作正常的腳本版本。
1. **重新下載腳本**。可以使用下面這個經過驗證的腳本內容:
```bash
# 備份舊腳本(可選)
mv ~/bin/iterm2-recv-zmodem.sh ~/bin/iterm2-recv-zmodem.sh.backup
# 創建新的腳本文件
nano ~/bin/iterm2-recv-zmodem.sh
```
2. **將以下內容完整地復制粘貼到新文件中**:
```bash
#!/bin/bash
#
# Description:
#
# OS X has no `gzexec` equivalent so we need to manually handle ZMODEM
# requests using the `rz` command. This script is invoked by iTerm2.
#
# iTerm2 Trigger regular expression: rz waiting to receive.\*\*B0100
# Action: Run Silent Coprocess ~/bin/iterm2-recv-zmodem.sh
# Check if rz is available
if ! command -v rz &> /dev/null; then
osascript -e 'display alert "ZMODEM Error" message "rz command not found. Please install lrzsz."'
exit 1
fi
# Use AppleScript to prompt user for a file to send
# Note: `choose file` is for files, `choose folder` is for directories
FILE=$(osascript -e 'tell application "iTerm2" to choose file with prompt "Choose a file to send"' 2>/dev/null)
# Check if user cancelled
if [ -z "$FILE" ]; then
# Send ZModem cancel sequence if user cancels the dialog
echo -e \\x18\\x18\\x18\\x18\\x18
sleep 1
echo -e \\x18\\x18\\x18\\x18\\x18
exit 1
fi
# Convert the Mac path (with colons) to a Unix path
# e.g., "Macintosh HD:Users:john:file.txt" -> "/Users/john/file.txt"
FILE=$(echo "$FILE" | sed 's/:/\/g' | sed 's/\/\//\//g')
# Check if the file exists and is readable
if [ ! -r "$FILE" ]; then
osascript -e "display alert \"ZMODEM Error\" message \"File not found or not readable: $FILE\""
exit 1
fi
# Send the file using rz
/usr/local/bin/rz --escape --binary --bufsize 4096 < "$FILE"
exit 0
```
3. **保存文件** (`Ctrl + O` -> `Enter` -> `Ctrl + X`),然后賦予執行權限:
```bash
chmod +x ~/bin/iterm2-recv-zmodem.sh
```
4. **確保 iTerm2 觸發器中的路徑指向這個新腳本**(檢查 `Preferences -> Profiles -> Advanced -> Triggers`)。
---
### 方案三:檢查 iTerm2 觸發器正則表達式
極小的可能性,觸發器正則表達式匹配到了錯誤的地方,導致調用了錯誤的腳本。請再次確認你的觸發器配置:
* **對于 `rz` (接收/上傳)**:
* **Regular Expression**: `rz waiting to receive.\*\*B0100`
* **Action**: `Run Silent Coprocess`
* **Parameters**: `~/bin/iterm2-recv-zmodem.sh` (確保路徑絕對正確)
### 總結
| 問題現象 | 根本原因 | 解決方案 |
| :--- | :--- | :--- |
| `rz` 彈出**文件夾**選擇框 | 觸發器腳本 `iterm2-recv-zmodem.sh` 中使用了 `choose folder` 命令 | **編輯腳本**,將 `choose folder` 改為 `choose file` |
| 腳本文件損壞或版本過舊 | 腳本邏輯錯誤或功能不全 | **直接下載并替換**為上面提供的新版腳本 |
完成以上任一方案的修改后,**完全關閉并重新打開 iTerm2**,再次嘗試 `rz` 命令,問題就應該解決了,現在可以正常選擇文件了。

浙公網安備 33010602011771號