Skip to content

Instantly share code, notes, and snippets.

@starwing
Last active December 16, 2019 08:06
Show Gist options
  • Save starwing/b6615a09a0e80e55990a151f4dbd3327 to your computer and use it in GitHub Desktop.
Save starwing/b6615a09a0e80e55990a151f4dbd3327 to your computer and use it in GitHub Desktop.
Single file to use zmodem in iTerm2
#!/bin/bash
# vim: ft=sh nu et sw=2 fdc=2
select_one() {
local cmd; read -d '' cmd <<EOF
tell application "iTerm2" to activate
tell application "iTerm2" to set thefile to choose $1
do shell script "echo " & (quoted form of POSIX path of thefile as Unicode text)
EOF
osascript -e "$cmd" 2>/dev/null
}
check_cancel() {
if [[ "$1" == "" ]]; then
echo -e \\x18\\x18\\x18\\x18\\x18
sleep 1; echo; echo \# Cancelled transfer
exit 0
fi
}
do_sz() {
local FILE=$(select_one file)
check_cancel "$FILE"
local tmpfile=$(mktemp); rm "$tmpfile"; mkdir "$tmpfile"
exec 3>"$tmpfile/iterm2-zmodem.log"
if /usr/local/bin/sz -eb "$FILE" 2>&3; then
sleep 1; echo; echo \# Received $FILE
rm -r "$tmpfile"
else
sleep 1; echo; echo \# Received $FILE failed
open "$tmpfile"
fi
}
do_rz() {
local DIR=$(select_one folder)
check_cancel "$DIR"
local tmpfile=$(mktemp); rm "$tmpfile"; mkdir "$tmpfile"
exec 3>"$tmpfile/iterm2-zmodem.log"
cd "$DIR"
if /usr/local/bin/rz -eb -B4096 2>&3; then
sleep 1; echo; echo \# Received =\> $DIR
rm -r "$tmpfile"
else
sleep 1; echo; echo \# Received failed
open "$tmpfile"
fi
}
case $1 in
sz) do_sz;;
rz) do_rz;;
esac
# Usage: Preferences->Profiles->Advanced->Trigger
# Regular expression: rz waiting to receive.\*\*B0100
# Action: Run Silent Coprocess
# Parameters: /usr/local/bin/iterm2-zmodem.sh sz
# Instant: checked
# Regular expression: \*\*B00000000000000
# Action: Run Silent Coprocess
# Parameters: /usr/local/bin/iterm2-zmodem.sh rz
# Instant: checked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment