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号