微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AppleScript Droplet 将 PSD 和 TIF 转换为 JPG

如何解决AppleScript Droplet 将 PSD 和 TIF 转换为 JPG

有很多 JPG 转换器的例子,我正在尝试根据我的需要更改一个,但我几乎不需要帮助。 要求是:

  1. 它应该是一个 AppleScript Droplet。我正在使用脚本编辑器(出于某种原因,Automator 无法为我运行简单的 Droplet 拖放功能)。
  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

解决方法

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 处理程序的别名在它们包含空格的情况下会被强制和引用。

,

你的脚本有很多错误。不清楚为什么在将文件发送到 shell 命令之前打开文件进行写入。 sips 不对打开的 FILE 引用进行操作。 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。