接着上一篇文章,在Mac上随手把同步图片和文件的功能也做了,Windows暂时不用,就不做了。简单说明一下内容:
核心逻辑: osascript -e 'clipboard info' 2>/dev/null
:
-
文件内容处理:
osascript -e 'clipboard info' «class furl», 39, «class ut16», 22, «class utf8», 10, «class icns», 781190, Unicode text, 20, string, 10, «class 8BPS», 1068148, GIF picture, 48307, «class jp2 », 66756, JPEG picture, 53765, TIFF picture, 4198020, «class PNGf», 202292, «class BMP », 4194442, «class TPIC», 735063
-
图片内容检测:
osascript -e 'clipboard info' *** Error creating a JP2 color space: falling back to sRGB «class PNGf», 30911, «class 8BPS», 120626, GIF picture, 9586, «class jp2 », 30845, JPEG picture, 20160, TIFF picture, 1053698, «class BMP », 1052906, «class TPIC», 46827
-
文本内容检测:
osascript -e 'clipboard info' «class utf8», 96, «class ut16», 186, string, 9, Unicode text, 184
还有一个注意点是,文件处理的 «class furl»
,路径分隔是:`,所以做了一些其他处理:
osascript -e 'the clipboard as «class furl»'
file Macintosh HD:Users:kelu:Downloads:xxx.sh
以下是具体代码:
# ver 2.0
# 远端路径定义
REMOTE_DIR="/tmp/clipboard"
REMOTE_HOST="1.2.3.4"
REMOTE_USER="user"
REMOTE_PASS="password"
LOCAL_DIR="$HOME/Downloads"
TEXT_FILE="clipboard_sync.txt"
IMAGE_FILE="clipboard_content.png"
# 检查远端是否有 /tmp/clipboard 文件夹
REMOTE_EXISTS=$(sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST "[ -d $REMOTE_DIR ] && echo 'exists'")
if [ "$REMOTE_EXISTS" == "exists" ]; then
echo "# 远端文件夹存在"
if sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST "[ -f $REMOTE_DIR/$TEXT_FILE ]"; then
echo "# 拷贝文字到剪切板"
sshpass -p "$REMOTE_PASS" scp $REMOTE_USER@$REMOTE_HOST:"$REMOTE_DIR/$TEXT_FILE" "$LOCAL_DIR/$TEXT_FILE"
cat "$LOCAL_DIR/$TEXT_FILE" | pbcopy
cat "$LOCAL_DIR/$TEXT_FILE" | head -n 1
echo ""
elif sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST "[ -f $REMOTE_DIR/$IMAGE_FILE ]"; then
echo "# 下载图片到本地剪切板"
sshpass -p "$REMOTE_PASS" scp $REMOTE_USER@$REMOTE_HOST:"$REMOTE_DIR/$IMAGE_FILE" "$LOCAL_DIR/$IMAGE_FILE"
osascript -e "set the clipboard to (read (POSIX file \"$LOCAL_DIR/$IMAGE_FILE\") as «class PNGf»)"
else
echo "# 获取远端目录中的文件"
RANDOM_FILE=$(sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST "ls -1 $REMOTE_DIR | shuf -n 1")
if [ -n "$RANDOM_FILE" ]; then
echo "# 下载文件 $RANDOM_FILE"
sshpass -p "$REMOTE_PASS" scp $REMOTE_USER@$REMOTE_HOST:"$REMOTE_DIR/$RANDOM_FILE" "$LOCAL_DIR/$RANDOM_FILE"
filePath="$LOCAL_DIR/$RANDOM_FILE"
osascript <<EOF
set filePath to POSIX file "$filePath" as alias
tell application "Finder"
set the clipboard to (filePath as «class furl»)
end tell
EOF
fi
fi
echo "# 清除远端数据"
sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST "rm -rf $REMOTE_DIR"
else
sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $REMOTE_DIR"
CLIPBOARD_TYPE=$(osascript -e 'clipboard info' 2>/dev/null)
if echo "$CLIPBOARD_TYPE" | grep -q "furl"; then
# 获取剪贴板中的路径
FILE_PATH=$(osascript -e 'the clipboard as «class furl»' | tr -d '"')
# 将 Mac 路径转换为 POSIX 路径
FILE_PATH_POSIX=$(osascript -e "POSIX path of POSIX file \"$FILE_PATH\"")
# 将 POSIX 路径中的冒号替换为斜杠
FILE_PATH_POSIX=$(echo "$FILE_PATH_POSIX" | sed 's/:/\//g' | sed -e 's/^file Macintosh HD//')
echo "# 检测到文件路径: $FILE_PATH_POSIX"
sshpass -p "$REMOTE_PASS" scp "$FILE_PATH_POSIX" $REMOTE_USER@$REMOTE_HOST:"$REMOTE_DIR/"
echo "# 推送远端剪切板文件"
exit 0
fi
if echo "$CLIPBOARD_TYPE" | grep -q "TIFF"; then
echo "# 将剪切板中的图片保存为文件"
osascript -e 'set imgData to the clipboard as «class PNGf»
set imgFile to open for access POSIX file "'$LOCAL_DIR/$IMAGE_FILE'" with write permission
write imgData to imgFile
close access imgFile' 2>/dev/null
sshpass -p "$REMOTE_PASS" scp "$LOCAL_DIR/$IMAGE_FILE" $REMOTE_USER@$REMOTE_HOST:"$REMOTE_DIR/"
echo "# 推送远端剪切板图片"
rm "$LOCAL_DIR/$IMAGE_FILE"
exit 0
fi
if echo "$CLIPBOARD_TYPE" | grep -q "string\|Unicode text"; then
echo "# 检测到文本内容"
pbpaste > "$LOCAL_DIR/$TEXT_FILE"
sshpass -p "$REMOTE_PASS" scp "$LOCAL_DIR/$TEXT_FILE" $REMOTE_USER@$REMOTE_HOST:"$REMOTE_DIR/$TEXT_FILE"
echo "# 推送远端剪切板文字"
cat "$LOCAL_DIR/$TEXT_FILE" | head -n 1
rm "$LOCAL_DIR/$TEXT_FILE"
exit 0
fi
sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST "rm -rf $REMOTE_DIR"
fi