applescript - AppleScript Droplet 将 PSD 和 TIF 转换为 JPG
问题描述
有很多转换为 JPG 的示例,我正在尝试根据我的需要更改一个,但我需要的帮助很少。要求是:
- 它应该是一个 AppleScript Droplet。我正在使用脚本编辑器(由于某种原因 Automator 无法为我运行简单的液滴拖放功能)。
- JPG 的输出文件夹不应由用户提示.. 而是在代码中永久设置为变量,并且易于更改。
- 转换后的 JPG 的质量(压缩)也应该可以在代码中轻松自定义。
- 如有必要,转换后的 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
解决方案
混合图像事件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
处理程序的别名在它们包含空格的事件中被强制和引用。
推荐阅读
- angular - 你如何在 Angular 中读取当前 PWA 的版本哈希?
- jquery - 如何删除或更改按钮 .btn.btn-outline-secondary bootstrap-show-password 的属性“标题”?
- pyspark - PySpark中随机森林树的可视化?
- javascript - 将 URL 参数传递给另一个 URL 的最简单方法是什么?
- android - 如何根据在 kotlin 中使用的键对 json 响应进行排序
- github - 仅当以下情况为真时,如何触发 Github 操作?
- python - Python声明一个变量列表而不初始化它们
- python - opencv轮廓上的等距采样
- sql - 如何在postgresql中使用索引选择数组中的值?
- html - HTML 电子邮件无法从网络加载自定义字体