mac的automator挺好用的,我发现它适合制作各种方便的右键功能!配合AI食用简直完美!这里我随便摘几个我常用的!

PS: automator的路径在:~/Library/Services,不想要的操作直接在这删了就行

右键清除zip下的隐藏文件

这个已经在mac工具箱那篇写过了;
image

右键进行csv编码转化

这个是最近工作遇到的问题,每次下载的csv默认都是utf-8,mac-微软excel一打开就乱码,我总是vscode打开然后修改编码,我寻思整个转化键,快速转成utf-8-sig(其实vscode也有插件支持查阅csv,但是还是觉得微软excel比较熟悉,顺手;

  • 工作流程选“文件或文件夹”\位于“finder”

  • 拉入运行一个shell脚本,传递输入选择“作为自变量”,内容如下,这里我使用 uchardet 来做编码检测;她是一个编码检测器库,它会返回的编码名称,与 iconv 兼容。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    #!/bin/zsh

    # 设置 uchardet 路径
    uchardet_cmd="/usr/local/bin/uchardet"

    # 检查文件编码并转换为 utf-8-sig
    convert_encoding() {
    local file="$1"
    local result=""

    # 使用 uchardet 检测文件编码
    encoding=$("$uchardet_cmd" "$file")

    # 检查文件是否已经是 utf-8-sig
    if [[ "$encoding" == "UTF-8" ]]; then
    if head -c 3 "$file" | grep -q $'\xEF\xBB\xBF'; then
    result="${file##*/}: 编码无须转换\n"
    else
    output_file="${file%.csv}-utf-8-sig.csv"

    iconv -f utf-8 -t utf-8 "$file" | sed '1s/^\xEF\xBB\xBF//' > "${file%.csv}-converted.csv"
    if [[ -f "${file%.csv}-converted.csv" ]]; then
    (echo -ne '\xEF\xBB\xBF'; cat "${file%.csv}-converted.csv") > "$output_file"
    if [[ -f "$output_file" ]]; then
    result+="${file##*/}: 成功转换\n"
    rm "${file%.csv}-converted.csv"
    else
    result+="${file##*/}: 创建输出文件失败: ${output_file##*/}\n"
    fi
    else
    result+="${file##*/}: 创建临时文件失败: ${file%.csv}-converted.csv\n"
    fi
    fi
    elif [[ "$encoding" == "GB2312" ]] || [[ "$encoding" == "ISO-8859-1" ]] || [[ "$encoding" == "ASCII" ]]; then
    output_file="${file%.csv}-utf-8-sig.csv"

    iconv -f "$encoding" -t utf-8 "$file" | sed '1s/^\xEF\xBB\xBF//' > "${file%.csv}-converted.csv"
    if [[ -f "${file%.csv}-converted.csv" ]]; then
    (echo -ne '\xEF\xBB\xBF'; cat "${file%.csv}-converted.csv") > "$output_file"
    if [[ -f "$output_file" ]]; then
    result+="${file##*/}: 成功转换\n"
    rm "${file%.csv}-converted.csv"
    else
    result+="${file##*/}: 创建输出文件失败: $output_file\n"
    fi
    else
    result+="${file##*/}: 创建临时文件失败: ${file%.csv}-converted.csv\n"
    fi
    else
    result+="${file##*/}: 不支持的文件格式 ($encoding)\n"
    fi

    echo -e "$result"
    }

    # 初始化结果变量
    all_results=""

    # 遍历所有选中的文件并进行转换
    for file in "$@"; do
    if [[ "${file##*.}" == "csv" ]]; then
    file_result=$(convert_encoding "$file")
    all_results+="$file_result"
    else
    all_results+="${file##*/}: 不是 CSV 文件"
    fi
    done

    # 使用 osascript 显示通知
    osascript -e 'display notification "'"${all_results}"'" with title "CSV转换结果"'

    # 输出所有结果到标准输出
    # echo -e "$all_results"

在同目录快速打开Finder

这里用到的是AppleScript,说实话我不太懂这个语言,但是没关系,AI懂就行了,我只是一个prompt工程师!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
on run {input, parameters}
try
-- 获取输入文件或文件夹的路径
set theItem to item 1 of input
set theItemPath to POSIX path of (theItem as alias)

-- 获取父目录路径
do shell script "dirname " & quoted form of theItemPath
set parentFolderPath to result

-- 将 POSIX 路径转换为 HFS 路径
set hfsParentFolderPath to POSIX file parentFolderPath

-- 在 Finder 中打开父目录
tell application "Finder"
activate
open hfsParentFolderPath
end tell
on error errMsg number errNum
display dialog "Error: " & errMsg buttons {"OK"} default button 1
end try

return input
end run