首页 > 解决方案 > 使用快速操作自动更改图像宽度和调整高度

问题描述

如果您有图像文件,则可以执行以下操作:

这看起来像这样:

在 Apples Preview 应用中调整大小

这将改变图像的宽度并自动调整高度,而不会剪切图像。

我想创建一个快速操作,它为我做这件事:将宽度更改为 100 像素并自动调整高度。

是否可以使用 Automators 快速操作编辑器来做到这一点?

标签: macosapplescriptautomator

解决方案


on run {input, parameters}
    set targetWidth to 100
    repeat with anAlias in (get contents of input)
        set hfsPath to anAlias as text
        try
            tell application "Image Events"
                launch
                set anImage to open (file hfsPath)
                set {w, h} to dimensions of anImage
                if w > h then
                    scale anImage to size targetWidth
                else
                    scale anImage to size (round (targetWidth * h / w) rounding up)
                end if
                save anImage with icon
                close anImage
            end tell
        on error error_message
            display dialog error_message
        end try
    end repeat
    return input
end run
  1. 打开自动机
  2. 选择创建快速操作
  3. 设置“工作流在 Finder.app 中接收图像文件”
  4. 使用上面的内容添加运行 AppleScript 操作。
  5. 保存(此服务)

推荐阅读