sh脚本文件

压缩多个文件夹到一个ZIP

#!/bin/bash

# 定义目标目录
target_dir="/Users/yuqiu/****/"
output_dir="$target_dir/ZIPDIR"

# 获取当前日期
current_date=$(date +"%Y-%m-%d")

# 定义压缩文件名
zip_filename="XX_${current_date}.zip"

# 检查目标目录是否存在
if [ ! -d "$target_dir" ]; then
  echo "目标目录不存在: $target_dir"
  exit 1
fi

# 检查要压缩的目录是否存在
if [ ! -d "$target_dir/needZipFolder1" ] || [ ! -d "$target_dir/needZipFolder2" ]; then
  echo "要压缩的目录不存在"
  exit 1
fi

# 检查并创建输出目录
if [ ! -d "$output_dir" ]; then
  mkdir -p "$output_dir"
  if [ $? -ne 0 ]; then
    echo "无法创建输出目录: $output_dir"
    exit 1
  fi
fi

# 切换到需要压缩文件的目录
cd "$target_dir"

# 创建压缩文件,压缩指定的两个文件夹
zip -r "$output_dir/$zip_filename" "needZipFolder1" "needZipFolder2"

# 检查压缩是否成功
if [ $? -eq 0 ]; then
  echo "压缩成功: $output_dir/$zip_filename"
else
  echo "压缩失败"
  exit 1
fi

打开文件夹

#!/bin/bash

# 获取当前日期
current_date=$(date +"%Y-%m-%d")
# 指定文件夹路径
FOLDER_PATH="/Users/yuqiu/log/${current_date}"
# 使用 open 命令打开文件夹
open "$FOLDER_PATH"

打开微信并弹出指定联系人

#!/bin/bash

# 获取当前日期
current_date=$(date +"%Y-%m-%d")
FILE_PATH="/Users/yuqiu/XX/XX_${current_date}.zip"
# 目标微信用户
TARGET_USER="userWXidNumber"

# AppleScript
osascript <<EOF
tell application "WeChat"
    activate
    delay 2

    -- 打开微信搜索并输入目标用户名字
    tell application "System Events"
        keystroke "f" using {command down}
        delay 1
        keystroke "$TARGET_USER"
        delay 1
        keystroke return
        delay 1

        -- 打开文件发送窗口
        -- keystroke "f" using {command down, shift down}
        -- delay 1

        -- 输入文件路径并发送
        -- set the clipboard to POSIX file "$FILE_PATH" as text
        -- delay 1
        -- keystroke "v" using {command down}
        -- delay 1
        -- keystroke return
        -- delay 1
        -- keystroke return
    end tell
end tell
EOF

问题

155:189: execution error: “System Events”遇到一个错误:“osascript”不允许发送按键。 (1002)

系统偏好设置-》隐私与安全性-》隐私-》辅助功能,选择应用程序(例如“Terminal”或“Script Editor”),如果脚本是作为一个单独的应用运行的,就选择那个应用。如果直接通过命令行工具如osascript执行AppleScript,添加“终端”(Terminal)。

如果这篇文章对你有用,可以关注本人微信公众号获取更多ヽ(^ω^)ノ ~

Mac常用sh文件_当前日期