AppleScript Droplet 将 PSD 和 TIF 转换为 JPG

问题描述 投票:0回答:3

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

  1. 应该是AppleScript Droplet。我正在使用脚本编辑器(出于某种原因,Automator 无法为我运行简单的液滴拖放功能)。
  2. JPG 的输出文件夹不应由用户提示 .. 而是在代码中永久设置为变量并轻松更改。
  3. 转换后的 JPG 的质量(压缩)也应该可以在代码中轻松定制。
  4. 如有必要,转换后的 JPG 文件必须转换为颜色配置文件 Adobe 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
applescript jpeg converters droplet
3个回答
2
投票

混合使用 Image Events

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


1
投票

你的剧本有很多错误。不清楚为什么在将文件发送到 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

0
投票

我已经编辑了 red_menace 的代码,使其将 JPEG 保存到与您拖动到它的文件相同的文件夹中。 “运行中”功能已被删除,因此请将其另存为应用程序并将文件拖放到其中。

这比通过 Photoshop 更有效率!

on open draggeditems
    --DEFINE DESTINATION FOLDER
    tell application "System Events"
        --set destination to (((path to desktop folder) as text) & "End:") -- folder path (trailing delimiter)
        --set destination to ((path to downloads folder) as text)
        --MAKE IT SAME AS SOURCE FILES
        set thisFile to item 1 of draggeditems
        set theFolder to (container of thisFile)
        set destination to path of theFolder as alias as text
    end tell
    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)
        --log convertedFile
        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
© www.soinside.com 2019 - 2024. All rights reserved.