将 PSD 和 TIF 转换为 JPG 的 AppleScript Droplet

AppleScript Droplet to Convert PSD and TIF to 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

你的脚本有很多错误。不清楚为什么在将文件发送到 shell 命令之前打开文件进行写入。 sips 不对打开的文件引用进行操作。 sips 需要从 POSIX 路径打开文件。我想这可能会做你想做的事(你需要实施错误检查等):

on open draggeditems
    set End_Folder to "~/Desktop/End/"
    repeat with currentFile in draggeditems
        do shell script "sips --setProperty formatOptions 10 " & quoted form of POSIX path of currentFile & " --out " & End_Folder
    end repeat
end open

混合使用 Image Eventssips 更容易混淆而不是有用,因为 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 处理程序的别名在它们包含空格的情况下被强制和引用。