返回

Mac OS 中图片自动压缩上传至腾讯云 COS

后端

前言

在日常工作或生活中,我们经常需要处理大量的图片,无论是图片编辑、图片压缩,还是图片上传,都需要花费大量的时间和精力。为了提高效率,我们可以利用一些工具或脚本来自动化处理这些任务。

本文将介绍如何在Mac OS中使用AppleScript来自动压缩PNG图片并上传至腾讯云COS。这种方法可以让你轻松实现图片的自动处理,节省时间和精力。

技术准备

在开始之前,我们需要准备好以下技术:

  • Mac OS X 10.11或更高版本
  • AppleScript
  • pngquan(一种无损PNG压缩工具)
  • 腾讯云COS(对象存储服务)
  • 苹果自带的“脚本编辑器”

步骤

1. 安装 pngquan

首先,我们需要在Mac OS中安装pngquan。你可以从pngquant的官方网站下载最新的版本。

2. 安装 AppleScript

如果你还没有安装AppleScript,可以从苹果的官方网站下载安装。

3. 准备 AppleScript 脚本

接下来,我们需要创建一个AppleScript脚本来实现图片的自动处理。你可以使用苹果自带的“脚本编辑器”来创建脚本。

在“脚本编辑器”中,输入以下脚本:

-- 获取选定的文件
set theFiles to (get the selection) as alias list

-- 检查是否有选定的文件
if theFiles is {} then
    display dialog "请选择要处理的图片文件!"
    return
end if

-- 获取腾讯云 COS 的配置信息
set theAccessKeyID to "你的腾讯云 AccessKeyID"
set theSecretKeyID to "你的腾讯云 SecretKeyID"
set theBucketName to "你的腾讯云 COS BucketName"
set theRegion to "你的腾讯云 COS 区域"

-- 创建腾讯云 COS 的客户端
set theCOSClient to (current application's NSURLSession sharedSession)'s configuration's HTTPAdditionalHeaders's objectForKey:"X-TC-Region"

-- 循环处理选定的文件
repeat with i from 1 to (count theFiles)
    -- 获取当前正在处理的文件
    set theFile to item i of theFiles

    -- 获取文件的扩展名
    set theExtension to (path extension of theFile as string)'s lowercase

    -- 检查文件的扩展名是否为 PNG
    if theExtension is "png" then
        -- 使用 pngquant 压缩 PNG 图片
        do shell script "pngquant --quality=90 --ext .png " & (quoted form of (POSIX path of theFile))

        -- 获取压缩后的 PNG 图片
        set theCompressedFile to (POSIX path of theFile)'s text item delimiters:"." & theExtension & ".png"
    else
        -- 如果文件不是 PNG 图片,则跳过
        continue
    end if

    -- 获取压缩后的图片的文件名
    set theNewFileName to (name of theFile) & "_compressed" & ".png"

    -- 将压缩后的图片移动到目标文件夹
    set theTargetFolder to (path to desktop folder from user domain)'s text item delimiters:":" & "Pictures"

    -- 检查目标文件夹是否存在
    if not (exists folder theTargetFolder) then
        -- 如果目标文件夹不存在,则创建它
        do shell script "mkdir -p " & (quoted form of theTargetFolder)
    end if

    -- 移动文件到目标文件夹
    move file theCompressedFile to folder theTargetFolder

    -- 将压缩后的图片上传到腾讯云 COS
    set theCOSClient's HTTPHeaderFields's objectForKey:"authorization" to (get Authorization theAccessKeyID theSecretKeyID)
    set theCOSClient's HTTPHeaderFields's objectForKey:"Content-Type" to "image/png"
    set theCOSClient's POST "/bucket/" & theBucketName & "/COS/" & theNewFileName HTTPBody:(read theCompressedFile)
    set theResponse to (theCOSClient's dataTaskWithRequest:(theCOSClient's requestWithURL:(NSURL URLWithString:"https://cos." & theRegion & ".myqcloud.com/bucket/" & theBucketName & "/COS/" & theNewFileName)) completionHandler:(script theResponse with result response error errorResponse) as NSData)
    if theResponse is not missing value then
        if (theResponse's statusCode() is 200) then
            display dialog "图片上传成功!"
        else
            display dialog "图片上传失败!"
        end if
    end if
end repeat

-- 获取授权信息
on get Authorization(theAccessKeyID, theSecretKeyID)
    -- 使用HMAC-SHA1算法生成签名
    script theSign
        property algorithm : "HmacSHA1"
        property encoding : "UTF-8"
    end script

    -- 将请求头中的日期和内容类型转换为RFC1123格式
    set theDate to (current date)'s ISO8601String()'s text item delimiters:{";", ":", "-"}
    set theDate to (theDate's string 1 thru (length theDate) - 5)'s text item delimiters:"." & "Z" & "T"
    set theContentType to "image/png"

    -- 构造签名字符串
    set theStringToSign to "PUT\n\n" & theContentType & "\n" & theDate & "\n/bucket/" & theBucketName & "/COS/" & theNewFileName

    -- 使用 HMAC-SHA1 算法生成签名
    tell theSign
        set theKey to (base64Decode theSecretKeyID)'s reversed()
        set theDigest to HMAC(theStringToSign, theKey, algorithm)
        set theSignature to base64Encode theDigest
        return "TC3 " & theAccessKeyID & ":" & theSignature
    end tell
end get Authorization

4. 保存 AppleScript 脚本

将AppleScript脚本保存为“.scpt”文件。

5. 设置自动处理规则

接下来,我们需要设置自动处理规则。你可以使用“脚本编辑器”中的“运行”菜单中的“在登录时运行”选项来设置自动处理规则。

6. 测试脚本

现在,你可以测试脚本是否正常工作。选择要处理的图片文件,然后运行脚本。脚本会自动压缩图片并上传至腾讯云COS。

结语

通过使用AppleScript,我们可以轻松实现图片的自动处理,包括图片压缩、图片重命名和图片上传。这种方法可以让你节省时间和精力,提高工作效率。