首页 > 解决方案 > AppleScript Droplet 将 PSD 和 TIF 转换为 JPG

问题描述

有很多转换为 JPG 的示例,我正在尝试根据我的需要更改一个,但我需要的帮助很少。要求是:

  1. 它应该是一个 AppleScript Droplet。我正在使用脚本编辑器(由于某种原因 Automator 无法为我运行简单的液滴拖放功能)。
  2. JPG 的输出文件夹不应由用户提示.. 而是在代码中永久设置为变量,并且易于更改。
  3. 转换后的 JPG 的质量(压缩)也应该可以在代码中轻松自定义。
  4. 如有必要,转换后的 JPG 文件必须转换为颜色配置文件 Adob​​e RGB 1998。

我知道图像事件允许我们设置 JPG 压缩,如:

save openedFile as JPEG with compression level (low|medium|high)

但不幸的是我需要更多的定制。

shell 脚本将帮助我将级别设置为 10 到 100,但不幸的是我无法正确实现 shell 脚本。请对第 3 点和第 4 点提供一点帮助。谢谢!

on run
    display dialog "Please drag image files to this script to turn them into JPEGs"
end run
on open draggeditems
    set End_Folder to "Macintosh HD:Users:zzz:Desktop:End"
    
    repeat with currentFile in draggeditems
        tell application "Image Events"
            set openedFile to open (currentFile as alias)
            set fileLocation to the location of openedFile
            set fileName to the name of openedFile
            set Path_to_Converted_File to (End_Folder & ":" & text 1 thru -5 of fileName & ".jpg")

            do shell script "sips --setProperty formatOptions 10 " & openedFile
            
    save openedFile as JPEG in Path_to_Converted_File
            --save openedFile as JPEG  with compression level low  in Path_to_Converted_File (low|medium|high)
            
            close openedFile
        end tell
    end repeat
end open

标签: applescriptjpegconvertersdroplet

解决方案


混合图像事件sips比有用更令人困惑,并且由于可以sips执行您正在寻找的各种选项(例如 3 和 4),因此将它用于所有事情更有意义。为各种选项设置一些变量将使您可以根据需要更改它们,或者添加首选项等。sips手册页将为您提供有关各种选项的更多详细信息;我为以下脚本中使用的注释添加了注释:

on run
    open (choose file with prompt "Select image files to turn into JPEGs:" with multiple selections allowed)
end run

on open draggeditems
    set destination to (((path to desktop folder) as text) & "End:") -- folder path (trailing delimiter)
    set format to "jpeg" -- jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga
    set extension to ".jpg" -- extension to match format
    set compression to 10 -- low | normal | high | best | <percent>
    set profile to quoted form of "/System/Library/ColorSync/Profiles/AdobeRGB1998.icc" -- POSIX path to color profile
    repeat with thisFile in draggeditems
        set theName to justTheName for thisFile
        set originalFile to quoted form of POSIX path of thisFile
        set convertedFile to quoted form of POSIX path of (destination & theName & extension)
        do shell script "sips -s format " & format & " -s formatOptions " & compression & " -m " & profile & space & originalFile & " --out " & convertedFile
    end repeat
end open

on justTheName for filePath
    tell application "System Events" to tell disk item (filePath as text)
        set {fileName, extension} to {name, name extension}
    end tell
    if extension is not "" then set fileName to text 1 thru -((count extension) + 2) of fileName -- just the name part
    return fileName
end justTheName

编辑添加:shell 脚本需要 POSIX 路径,因此传递给open处理程序的别名在它们包含空格的事件中被强制和引用。


推荐阅读